Skip to content

Commit 8cf180c

Browse files
committed
rename FunctionCallLocation to GenericCodeLocation and remove the name prop
1 parent 4e64e6e commit 8cf180c

14 files changed

+39
-53
lines changed

src/Code/FunctionCallLocation.php renamed to src/Code/GenericCodeLocation.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22

33
namespace Permafrost\PhpCodeSearch\Code;
44

5-
class FunctionCallLocation implements CodeLocation
5+
class GenericCodeLocation implements CodeLocation
66
{
7-
/** @var string */
8-
public $name;
9-
107
/** @var int */
118
public $column = 0;
129

@@ -16,16 +13,15 @@ class FunctionCallLocation implements CodeLocation
1613
/** @var int */
1714
public $startLine = -1;
1815

19-
public function __construct(string $name, int $startLine, int $endLine)
16+
public function __construct(int $startLine, int $endLine)
2017
{
21-
$this->name = $name;
2218
$this->startLine = $startLine;
2319
$this->endLine = $endLine;
2420
}
2521

26-
public static function create(string $name, int $startLine, int $endLine): self
22+
public static function create(int $startLine, int $endLine): self
2723
{
28-
return new static($name, $startLine, $endLine);
24+
return new static($startLine, $endLine);
2925
}
3026

3127
public function column(): int

src/Results/SearchResult.php

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

55
use Permafrost\PhpCodeSearch\Code\CodeLocation;
66
use Permafrost\PhpCodeSearch\Code\CodeSnippet;
7-
use Permafrost\PhpCodeSearch\Code\FunctionCallLocation;
7+
use Permafrost\PhpCodeSearch\Code\GenericCodeLocation;
88
use Permafrost\PhpCodeSearch\Code\StaticMethodCallLocation;
99
use Permafrost\PhpCodeSearch\Results\Nodes\FunctionCallNode;
1010
use Permafrost\PhpCodeSearch\Results\Nodes\ResultNode;
@@ -14,7 +14,7 @@
1414

1515
class SearchResult
1616
{
17-
/** @var CodeLocation|FunctionCallLocation|StaticMethodCallLocation */
17+
/** @var CodeLocation|GenericCodeLocation|StaticMethodCallLocation */
1818
public $location;
1919

2020
/** @var ResultNode|FunctionCallNode|StaticMethodCallNode|VariableNode */
@@ -27,10 +27,10 @@ class SearchResult
2727
protected $file;
2828

2929
/**
30-
* @param ResultNode $node
31-
* @param CodeLocation $location
32-
* @param CodeSnippet|null $snippet
33-
* @param File|string $file
30+
* @param \Permafrost\PhpCodeSearch\Results\Nodes\ResultNode $node
31+
* @param \Permafrost\PhpCodeSearch\Code\CodeLocation $location
32+
* @param \Permafrost\PhpCodeSearch\Code\CodeSnippet|null $snippet
33+
* @param \Permafrost\PhpCodeSearch\Support\File|string $file
3434
*/
3535
public function __construct(ResultNode $node, CodeLocation $location, ?CodeSnippet $snippet, $file)
3636
{

src/Visitors/FunctionCallVisitor.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Permafrost\PhpCodeSearch\Visitors;
44

5-
use Permafrost\PhpCodeSearch\Code\FunctionCallLocation;
5+
use Permafrost\PhpCodeSearch\Code\GenericCodeLocation;
66
use Permafrost\PhpCodeSearch\Code\StaticMethodCallLocation;
77
use Permafrost\PhpCodeSearch\Results\FileSearchResults;
88
use Permafrost\PhpCodeSearch\Results\Nodes\FunctionCallNode;
@@ -35,8 +35,7 @@ public function enterNode(Node $node)
3535
if (Arr::matches($node->name, $this->functionNames, true)) {
3636
$resultNode = FunctionCallNode::create($node->name->toString());
3737

38-
$location = FunctionCallLocation::create(
39-
$node->name->parts[0],
38+
$location = GenericCodeLocation::create(
4039
$node->getStartLine(),
4140
$node->getEndLine()
4241
);
@@ -61,8 +60,7 @@ public function enterNode(Node $node)
6160
if ($node instanceof Node\Expr\MethodCall) {
6261
$resultNode = FunctionCallNode::create($node->name->toString());
6362

64-
$location = FunctionCallLocation::create(
65-
$node->name->toString(),
63+
$location = GenericCodeLocation::create(
6664
$node->getStartLine(),
6765
$node->getEndLine()
6866
);
@@ -74,8 +72,7 @@ public function enterNode(Node $node)
7472
if (Arr::matches($node->name, $this->variableNames, true)) {
7573
$resultNode = VariableNode::create($node->name);
7674

77-
$location = FunctionCallLocation::create(
78-
$node->name,
75+
$location = GenericCodeLocation::create(
7976
$node->getStartLine(),
8077
$node->getEndLine()
8178
);
@@ -87,8 +84,7 @@ public function enterNode(Node $node)
8784
if ($node instanceof Node\Expr\New_) {
8885
$resultNode = VariableNode::create($node->class->toString());
8986

90-
$location = FunctionCallLocation::create(
91-
$node->class->parts[0],
87+
$location = GenericCodeLocation::create(
9288
$node->getStartLine(),
9389
$node->getEndLine()
9490
);
@@ -97,13 +93,9 @@ public function enterNode(Node $node)
9793
}
9894

9995
if ($node instanceof Node\Expr\Assign) {
100-
// print_r($node->expr->getSubNodeNames());
101-
// print_r($node->var->getSubNodeNames());
102-
// print_r([$node->var->name]);
10396
$resultNode = FunctionCallNode::create($node->var->name);
10497

105-
$location = FunctionCallLocation::create(
106-
$node->var->name,
98+
$location = GenericCodeLocation::create(
10799
$node->getStartLine(),
108100
$node->getEndLine()
109101
);

tests/Code/FunctionCallLocationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Permafrost\PhpCodeSearch\Tests\Code;
44

5-
use Permafrost\PhpCodeSearch\Code\FunctionCallLocation;
5+
use Permafrost\PhpCodeSearch\Code\GenericCodeLocation;
66
use PHPUnit\Framework\TestCase;
77
use Spatie\Snapshots\MatchesSnapshots;
88

@@ -13,7 +13,7 @@ class FunctionCallLocationTest extends TestCase
1313
/** @test */
1414
public function it_creates_an_object_with_the_correct_properties()
1515
{
16-
$location = new FunctionCallLocation('my_test_func', 1, 3);
16+
$location = new GenericCodeLocation(1, 3);
1717

1818
$this->assertMatchesObjectSnapshot($location);
1919
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
name: my_test_func
21
column: 0
32
endLine: 3
43
startLine: 1

tests/Results/SearchResultTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Permafrost\PhpCodeSearch\Tests\Results;
44

55
use Permafrost\PhpCodeSearch\Code\CodeSnippet;
6-
use Permafrost\PhpCodeSearch\Code\FunctionCallLocation;
6+
use Permafrost\PhpCodeSearch\Code\GenericCodeLocation;
77
use Permafrost\PhpCodeSearch\Results\Nodes\VariableNode;
88
use Permafrost\PhpCodeSearch\Results\SearchResult;
99
use Permafrost\PhpCodeSearch\Support\File;
@@ -18,7 +18,7 @@ class SearchResultTest extends TestCase
1818
public function it_creates_the_object_with_correct_properties()
1919
{
2020
$file = new File(tests_path('data/file2.txt'));
21-
$location = new FunctionCallLocation('my_func', 1, 1);
21+
$location = new GenericCodeLocation(1, 1);
2222
$snippet = (new CodeSnippet())->fromFile($file);
2323
$resultNode = new VariableNode('myVar');
2424
$result = new SearchResult($resultNode, $location, $snippet, $file);

tests/Results/__snapshots__/SearchResultTest__it_creates_the_object_with_correct_properties__1.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
location:
2-
name: my_func
32
column: 0
43
endLine: 1
54
startLine: 1

tests/SearcherTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,14 @@ public function it_only_returns_the_functions_being_searched_for()
117117
->searchCode('<?' . "php \n\$myVar = strtolower(strtoupper('test'));\n");
118118

119119
$this->assertCount(1, $results->results);
120-
$this->assertEquals('strtolower', $results->results[0]->location->name);
120+
$this->assertEquals('strtolower', $results->results[0]->node->name());
121121

122122
$results = (new Searcher())
123123
->functions(['strtoupper'])
124124
->searchCode('<?' . "php \n\$myVar = strtolower(strtoupper('test'));\n");
125125

126126
$this->assertCount(1, $results->results);
127-
$this->assertEquals('strtoupper', $results->results[0]->location->name);
127+
$this->assertEquals('strtoupper', $results->results[0]->node->name());
128128
}
129129

130130
/** @test */
@@ -135,7 +135,7 @@ public function it_finds_methods()
135135
->searchCode('<?' . "php \n\$myVar = \$obj->methodOne('one'); \$obj->methodTwo('two');\n");
136136

137137
$this->assertCount(1, $results->results);
138-
$this->assertEquals('methodOne', $results->results[0]->location->name);
138+
$this->assertEquals('methodOne', $results->results[0]->node->name());
139139
}
140140

141141
/** @test */
@@ -146,8 +146,8 @@ public function it_finds_variables()
146146
->searchCode('<?' . "php \n\$myVar = \$obj->methodOne('one'); \$obj->methodTwo('two');\n");
147147

148148
$this->assertCount(2, $results->results);
149-
$this->assertEquals('obj', $results->results[0]->location->name);
150-
$this->assertEquals('obj', $results->results[1]->location->name);
149+
$this->assertEquals('obj', $results->results[0]->node->name());
150+
$this->assertEquals('obj', $results->results[1]->node->name());
151151
}
152152

153153
/** @test */
@@ -158,7 +158,7 @@ public function it_finds_variables_using_regex()
158158
->searchCode('<?' . "php \n\$objC = \$objA->methodOne('one'); \$objB->methodTwo('two');\n");
159159

160160
$this->assertCount(2, $results->results);
161-
$this->assertEquals('objA', $results->results[0]->location->name);
162-
$this->assertEquals('objB', $results->results[1]->location->name);
161+
$this->assertEquals('objA', $results->results[0]->node->name());
162+
$this->assertEquals('objB', $results->results[1]->node->name());
163163
}
164164
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
-
2-
location: { name: strtolower, column: 0, endLine: 2, startLine: 2 }
2+
location: { column: 0, endLine: 2, startLine: 2 }
33
node: { name: strtolower }
44
snippet: { code: { 1: '<?php', 2: '$myVar = strtolower(''test'');' } }
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
-
2-
location: { name: MyClass, column: 0, endLine: 13, startLine: 13 }
2+
location: { column: 0, endLine: 13, startLine: 13 }
33
node: { name: MyClass }
44
snippet: { code: { 9: ' {', 10: ' Ray::rateLimiter()->count(5);', 11: ' }', 12: '', 13: ' $obj = new MyClass();', 14: '', 15: ' $obj->withData([123])->send();', 16: '' } }

0 commit comments

Comments
 (0)