Skip to content

Commit 4e64e6e

Browse files
authored
Merge pull request #3 from permafrost-dev/add-result-nodes
Add Result Nodes
2 parents 74ae2ff + 5c3e0e9 commit 4e64e6e

19 files changed

+162
-17
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ All notable changes to `php-code-search` will be documented in this file.
77
## 1.4.0 - 2021-07-05
88

99
- allow searching for static method calls like `MyClass` or `MyClass::someMethod`
10+
- add `ResultNode` class
11+
- add `node` property to `SearchResult` class
1012

1113
## 1.3.2 - 2021-07-05
1214

README.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ To search a file, use the `search` method. Its only parameter may be either a s
2727

2828
To search a string instead, use the `searchCode` method.
2929

30+
The search methods return an instance of `Permafrost\PhpCodeSearch\Results\FileSearchResults`, which has a `results` property.
31+
32+
Each `result` is an instance of `Permafrost\PhpCodeSearch\Results\SearchResult` with the following properties:
33+
34+
- `node` - the specific item that was found
35+
- `node->name(): string`
36+
- `location` - the location in the file that the item was found
37+
- `location->startLine(): int`
38+
- `location->endLine(): int`
39+
- `snippet` - a snippet of code lines from the file with the result line in the middle
40+
- `snippet->getCode(): string`
41+
- `file()` _(method)_ - provides access to the file that was searched
42+
3043
### Variable names
3144

3245
To search for variables by name, use the `variables` method before calling `search`. To use regular expressions, surround the values with `/`.
@@ -41,7 +54,7 @@ $results = $searcher
4154
->searchCode('<?php $oneA = "1a"; $oneB = "1b"; $oneC = "1c"; $twoA = "2a"; $twoB = "2b";');
4255

4356
foreach($results as $result) {
44-
echo "Found '{$result->location->name}' on line {$result->location->startLine}" . PHP_EOL;
57+
echo "Found '{$result->node->name()}' on line {$result->location->startLine}" . PHP_EOL;
4558
}
4659
```
4760

@@ -59,7 +72,7 @@ $results = $searcher
5972
->search('./file1.php');
6073

6174
foreach($results as $result) {
62-
echo "Found '{$result->location->name}' on line {$result->location->startLine}" . PHP_EOL;
75+
echo "Found '{$result->node->name()}' on line {$result->location->startLine}" . PHP_EOL;
6376
}
6477
```
6578

@@ -80,7 +93,7 @@ $results = $searcher
8093
'');
8194

8295
foreach($results as $result) {
83-
echo "Found '{$result->location->name}' on line {$result->location->startLine}" . PHP_EOL;
96+
echo "Found '{$result->node->name()}' on line {$result->location->startLine}" . PHP_EOL;
8497
}
8598
```
8699

@@ -100,7 +113,7 @@ $results = $searcher
100113
->search('./app/Http/Controllers/MyController.php');
101114

102115
foreach($results as $result) {
103-
echo "Found '{$result->location->name}' on line {$result->location->startLine}" . PHP_EOL;
116+
echo "Found '{$result->node->name()}' on line {$result->location->startLine}" . PHP_EOL;
104117
}
105118
```
106119

@@ -118,7 +131,7 @@ $results = $searcher
118131
->search('./app/Http/Controllers/MyController.php');
119132

120133
foreach($results as $result) {
121-
echo "Found '{$result->location->name}' on line {$result->location->startLine}" . PHP_EOL;
134+
echo "Found '{$result->node->name()}' on line {$result->location->startLine}" . PHP_EOL;
122135
}
123136
```
124137

@@ -136,7 +149,7 @@ $results = $searcher
136149
->search('./app/Http/Controllers/MyController.php');
137150

138151
foreach($results as $result) {
139-
echo "Found '{$result->location->name}' on line {$result->location->startLine}" . PHP_EOL;
152+
echo "Found '{$result->node->name()}' on line {$result->location->startLine}" . PHP_EOL;
140153
}
141154
```
142155

@@ -154,7 +167,7 @@ $results = $searcher
154167
->searchCode('<?php $str = strtolower("TEST");');
155168

156169
foreach($results as $result) {
157-
echo "Found '{$result->location->name}' on line {$result->location->startLine}" . PHP_EOL;
170+
echo "Found '{$result->node->name()}' on line {$result->location->startLine}" . PHP_EOL;
158171
}
159172
```
160173

src/Results/FileSearchResults.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Permafrost\PhpCodeSearch\Code\CodeLocation;
66
use Permafrost\PhpCodeSearch\Code\CodeSnippet;
7+
use Permafrost\PhpCodeSearch\Results\Nodes\ResultNode;
78
use Permafrost\PhpCodeSearch\Support\File;
89

910
class FileSearchResults
@@ -26,11 +27,11 @@ public function __construct(File $file, bool $withSnippets = true)
2627
$this->withSnippets = $withSnippets;
2728
}
2829

29-
public function addLocation(CodeLocation $location): self
30+
public function add(ResultNode $resultNode, CodeLocation $location): self
3031
{
3132
$snippet = $this->makeSnippet($location->startLine());
3233

33-
$this->results[] = new SearchResult($location, $snippet, $this->file);
34+
$this->results[] = new SearchResult($resultNode, $location, $snippet, $this->file);
3435

3536
return $this;
3637
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Permafrost\PhpCodeSearch\Results\Nodes;
4+
5+
class FunctionCallNode implements ResultNode
6+
{
7+
/** @var string */
8+
public $name;
9+
10+
public function __construct(string $name)
11+
{
12+
$this->name = $name;
13+
}
14+
15+
public static function create(string $name): self
16+
{
17+
return new static(...func_get_args());
18+
}
19+
20+
public function name(): string
21+
{
22+
return $this->name;
23+
}
24+
}

src/Results/Nodes/ResultNode.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Permafrost\PhpCodeSearch\Results\Nodes;
4+
5+
interface ResultNode
6+
{
7+
public function name(): string;
8+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Permafrost\PhpCodeSearch\Results\Nodes;
4+
5+
class StaticMethodCallNode implements ResultNode
6+
{
7+
/** @var string */
8+
public $className;
9+
10+
/** @var string */
11+
public $methodName;
12+
13+
/** @var string */
14+
public $name;
15+
16+
public function __construct(string $className, string $methodName)
17+
{
18+
$this->className = $className;
19+
$this->methodName = $methodName;
20+
$this->name = $this->name();
21+
}
22+
23+
public static function create(string $className, string $methodName): self
24+
{
25+
return new static(...func_get_args());
26+
}
27+
28+
public function name(): string
29+
{
30+
return "{$this->className}::{$this->methodName}";
31+
}
32+
}

src/Results/Nodes/VariableNode.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Permafrost\PhpCodeSearch\Results\Nodes;
4+
5+
class VariableNode implements ResultNode
6+
{
7+
/** @var string */
8+
public $name;
9+
10+
public function __construct(string $name)
11+
{
12+
$this->name = $name;
13+
}
14+
15+
public static function create(string $name): self
16+
{
17+
return new static(...func_get_args());
18+
}
19+
20+
public function name(): string
21+
{
22+
return $this->name;
23+
}
24+
}

src/Results/SearchResult.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,35 @@
66
use Permafrost\PhpCodeSearch\Code\CodeSnippet;
77
use Permafrost\PhpCodeSearch\Code\FunctionCallLocation;
88
use Permafrost\PhpCodeSearch\Code\StaticMethodCallLocation;
9+
use Permafrost\PhpCodeSearch\Results\Nodes\FunctionCallNode;
10+
use Permafrost\PhpCodeSearch\Results\Nodes\ResultNode;
11+
use Permafrost\PhpCodeSearch\Results\Nodes\StaticMethodCallNode;
12+
use Permafrost\PhpCodeSearch\Results\Nodes\VariableNode;
913
use Permafrost\PhpCodeSearch\Support\File;
1014

1115
class SearchResult
1216
{
1317
/** @var CodeLocation|FunctionCallLocation|StaticMethodCallLocation */
1418
public $location;
1519

20+
/** @var ResultNode|FunctionCallNode|StaticMethodCallNode|VariableNode */
21+
public $node;
22+
1623
/** @var CodeSnippet|null */
1724
public $snippet;
1825

1926
/** @var File */
2027
protected $file;
2128

2229
/**
30+
* @param ResultNode $node
2331
* @param CodeLocation $location
2432
* @param CodeSnippet|null $snippet
2533
* @param File|string $file
2634
*/
27-
public function __construct(CodeLocation $location, ?CodeSnippet $snippet, $file)
35+
public function __construct(ResultNode $node, CodeLocation $location, ?CodeSnippet $snippet, $file)
2836
{
37+
$this->node = $node;
2938
$this->location = $location;
3039
$this->snippet = $snippet;
3140
$this->file = is_string($file) ? new File($file) : $file;

src/Visitors/FunctionCallVisitor.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use Permafrost\PhpCodeSearch\Code\FunctionCallLocation;
66
use Permafrost\PhpCodeSearch\Code\StaticMethodCallLocation;
77
use Permafrost\PhpCodeSearch\Results\FileSearchResults;
8+
use Permafrost\PhpCodeSearch\Results\Nodes\FunctionCallNode;
9+
use Permafrost\PhpCodeSearch\Results\Nodes\StaticMethodCallNode;
10+
use Permafrost\PhpCodeSearch\Results\Nodes\VariableNode;
811
use Permafrost\PhpCodeSearch\Support\Arr;
912
use PhpParser\Node;
1013
use PhpParser\Node\Expr\FuncCall;
@@ -30,71 +33,82 @@ public function enterNode(Node $node)
3033
{
3134
if ($node instanceof FuncCall) {
3235
if (Arr::matches($node->name, $this->functionNames, true)) {
36+
$resultNode = FunctionCallNode::create($node->name->toString());
37+
3338
$location = FunctionCallLocation::create(
3439
$node->name->parts[0],
3540
$node->getStartLine(),
3641
$node->getEndLine()
3742
);
3843

39-
$this->results->addLocation($location);
44+
$this->results->add($resultNode, $location);
4045
}
4146
}
4247

4348
if ($node instanceof Node\Expr\StaticCall) {
49+
$resultNode = StaticMethodCallNode::create($node->class->toString(), $node->name->toString());
50+
4451
$location = StaticMethodCallLocation::create(
4552
$node->class->parts[0],
4653
$node->name->toString(),
4754
$node->getStartLine(),
4855
$node->getEndLine()
4956
);
5057

51-
$this->results->addLocation($location);
58+
$this->results->add($resultNode, $location);
5259
}
5360

5461
if ($node instanceof Node\Expr\MethodCall) {
62+
$resultNode = FunctionCallNode::create($node->name->toString());
63+
5564
$location = FunctionCallLocation::create(
5665
$node->name->toString(),
5766
$node->getStartLine(),
5867
$node->getEndLine()
5968
);
6069

61-
$this->results->addLocation($location);
70+
$this->results->add($resultNode, $location);
6271
}
6372

6473
if ($node instanceof Node\Expr\Variable) {
6574
if (Arr::matches($node->name, $this->variableNames, true)) {
75+
$resultNode = VariableNode::create($node->name);
76+
6677
$location = FunctionCallLocation::create(
6778
$node->name,
6879
$node->getStartLine(),
6980
$node->getEndLine()
7081
);
7182

72-
$this->results->addLocation($location);
83+
$this->results->add($resultNode, $location);
7384
}
7485
}
7586

7687
if ($node instanceof Node\Expr\New_) {
88+
$resultNode = VariableNode::create($node->class->toString());
89+
7790
$location = FunctionCallLocation::create(
7891
$node->class->parts[0],
7992
$node->getStartLine(),
8093
$node->getEndLine()
8194
);
8295

83-
$this->results->addLocation($location);
96+
$this->results->add($resultNode, $location);
8497
}
8598

8699
if ($node instanceof Node\Expr\Assign) {
87100
// print_r($node->expr->getSubNodeNames());
88101
// print_r($node->var->getSubNodeNames());
89102
// print_r([$node->var->name]);
103+
$resultNode = FunctionCallNode::create($node->var->name);
90104

91105
$location = FunctionCallLocation::create(
92106
$node->var->name,
93107
$node->getStartLine(),
94108
$node->getEndLine()
95109
);
96110

97-
$this->results->addLocation($location);
111+
$this->results->add($resultNode, $location);
98112
}
99113
}
100114
}

tests/Results/SearchResultTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Permafrost\PhpCodeSearch\Code\CodeSnippet;
66
use Permafrost\PhpCodeSearch\Code\FunctionCallLocation;
7+
use Permafrost\PhpCodeSearch\Results\Nodes\VariableNode;
78
use Permafrost\PhpCodeSearch\Results\SearchResult;
89
use Permafrost\PhpCodeSearch\Support\File;
910
use PHPUnit\Framework\TestCase;
@@ -19,7 +20,8 @@ public function it_creates_the_object_with_correct_properties()
1920
$file = new File(tests_path('data/file2.txt'));
2021
$location = new FunctionCallLocation('my_func', 1, 1);
2122
$snippet = (new CodeSnippet())->fromFile($file);
22-
$result = new SearchResult($location, $snippet, $file);
23+
$resultNode = new VariableNode('myVar');
24+
$result = new SearchResult($resultNode, $location, $snippet, $file);
2325

2426
$this->assertMatchesObjectSnapshot($result);
2527
}

0 commit comments

Comments
 (0)