Skip to content

Commit 8a07fd7

Browse files
authored
Merge pull request #7 from permafrost-dev/add-location-to-nodes
Add location to nodes, internal refactoring, add static property support
2 parents cd85d67 + d7218a8 commit 8a07fd7

File tree

47 files changed

+367
-138
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+367
-138
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to `php-code-search` will be documented in this file.
44

55
---
66

7+
## 1.8.0 - 2021-07-27
8+
9+
- `static()` supports searching for static property accesses like `SomeClass::$myProperty` or `myProperty`
10+
- major internal refactoring of nodes
11+
712
## 1.7.0 - 2021-07-25
813

914
- use latest version of `code-snippets`

src/Code/GenericCodeLocation.php

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

33
namespace Permafrost\PhpCodeSearch\Code;
44

5+
use PhpParser\Node;
6+
57
class GenericCodeLocation implements CodeLocation
68
{
79
/** @var int */
@@ -24,6 +26,11 @@ public static function create(int $startLine, int $endLine): self
2426
return new static($startLine, $endLine);
2527
}
2628

29+
public static function createFromNode(Node $node): self
30+
{
31+
return new static($node->getStartLine(), $node->getEndLine());
32+
}
33+
2734
public function column(): int
2835
{
2936
return $this->column;

src/Results/FileSearchResults.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct($file, bool $withSnippets = true)
3333

3434
public function add(ResultNode $resultNode, CodeLocation $location): self
3535
{
36-
$snippet = $this->makeSnippet($location->startLine(), $location->endLine());
36+
$snippet = $this->makeSnippet($resultNode->location()->startLine(), $resultNode->location()->endLine());
3737

3838
$this->results[] = new SearchResult($resultNode, $location, $snippet, $this->file);
3939

src/Results/Nodes/ArrayItemNode.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@
22

33
namespace Permafrost\PhpCodeSearch\Results\Nodes;
44

5+
use Permafrost\PhpCodeSearch\Code\GenericCodeLocation;
6+
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\HasLocation;
57
use Permafrost\PhpCodeSearch\Support\Transformer;
8+
use PhpParser\Node;
69

710
class ArrayItemNode implements ValueNode, ResultNode
811
{
12+
use HasLocation;
13+
914
/** @var mixed|int|string|null */
1015
public $key;
1116

1217
/** @var array|mixed|ResultNode|ValueNode */
1318
public $value;
1419

15-
public function __construct($key, $value)
20+
public function __construct(Node $node)
1621
{
17-
$this->key = Transformer::parserNodeToResultNode($key);
18-
$this->value = Transformer::parserNodeToResultNode($value);
22+
$this->key = Transformer::parserNodeToResultNode($node->key);
23+
$this->value = Transformer::parserNodeToResultNode($node->value);
24+
$this->location = GenericCodeLocation::createFromNode($node);
1925
}
2026

2127
public function name(): string

src/Results/Nodes/ArrayNode.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,31 @@
33

44
namespace Permafrost\PhpCodeSearch\Results\Nodes;
55

6+
use Permafrost\PhpCodeSearch\Code\CodeLocation;
7+
use Permafrost\PhpCodeSearch\Code\GenericCodeLocation;
8+
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\HasLocation;
69
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\TransformsArguments;
10+
use PhpParser\Node;
711
use PhpParser\Node\Expr\Array_;
812

913
class ArrayNode implements ValueNode
1014
{
15+
use HasLocation;
1116
use TransformsArguments;
1217

1318
/** @var array|ValueNode[]|ResultNode[] */
1419
public $value;
1520

16-
public function __construct($value)
21+
public function __construct(Node $node)
1722
{
18-
if ($value instanceof Array_) {
19-
$value = $value->items;
23+
$value = $node;
24+
25+
if ($node instanceof Array_) {
26+
$value = $node->items;
2027
}
2128

2229
$this->value = $this->transformArgumentsToNodes($value);
30+
$this->location = GenericCodeLocation::createFromNode($node);
2331
}
2432

2533
public function value()

src/Results/Nodes/AssignmentNode.php

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

33
namespace Permafrost\PhpCodeSearch\Results\Nodes;
44

5+
use Permafrost\PhpCodeSearch\Code\GenericCodeLocation;
6+
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\HasLocation;
57
use Permafrost\PhpCodeSearch\Support\Transformer;
8+
use PhpParser\Node;
69

710
class AssignmentNode implements ResultNode, ValueNode
811
{
12+
use HasLocation;
13+
914
/** @var string */
1015
public $variableName;
1116

@@ -15,14 +20,15 @@ class AssignmentNode implements ResultNode, ValueNode
1520
/** @var string */
1621
public $name;
1722

18-
public function __construct(string $variableName, $value)
23+
public function __construct(Node\Expr\Assign $node)
1924
{
20-
$this->variableName = $variableName;
21-
$this->value = Transformer::parserNodeToResultNode($value);
25+
$this->variableName = $node->var->name;
26+
$this->value = Transformer::parserNodeToResultNode($node->expr);
27+
$this->location = GenericCodeLocation::createFromNode($node);
2228
$this->name = $this->name();
2329
}
2430

25-
public static function create(string $variableName, $value): self
31+
public static function create(Node\Expr\Assign $node): self
2632
{
2733
return new static(...func_get_args());
2834
}

src/Results/Nodes/AssignmentOperationNode.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
namespace Permafrost\PhpCodeSearch\Results\Nodes;
44

5+
use Permafrost\PhpCodeSearch\Code\CodeLocation;
6+
use Permafrost\PhpCodeSearch\Code\GenericCodeLocation;
7+
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\HasLocation;
58
use Permafrost\PhpCodeSearch\Support\Transformer;
69
use PhpParser\Node\Expr\AssignOp;
710

811
class AssignmentOperationNode implements ResultNode, ValueNode
912
{
13+
use HasLocation;
14+
1015
/** @var string */
1116
public $name;
1217

@@ -17,6 +22,7 @@ public function __construct(AssignOp $node)
1722
{
1823
$this->name = $node->var->name;
1924
$this->value = Transformer::parserNodeToResultNode($node->expr);
25+
$this->location = GenericCodeLocation::createFromNode($node);
2026
}
2127

2228
public function name(): string

src/Results/Nodes/BinaryOperationNode.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
namespace Permafrost\PhpCodeSearch\Results\Nodes;
44

5+
use Permafrost\PhpCodeSearch\Code\GenericCodeLocation;
6+
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\HasLocation;
57
use Permafrost\PhpCodeSearch\Support\Transformer;
68
use PhpParser\Node\Expr\BinaryOp;
79

810
class BinaryOperationNode implements OperationNode, ValueNode
911
{
12+
use HasLocation;
13+
1014
/** @var string */
1115
public $symbol;
1216

@@ -31,6 +35,8 @@ public function __construct(BinaryOp $node)
3135
$rightValue = '';
3236

3337
$this->value = Transformer::binaryOperationNodeToValue($this);
38+
39+
$this->location = GenericCodeLocation::createFromNode($node);
3440
}
3541

3642
public function symbol(): string

src/Results/Nodes/CommentNode.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@
22

33
namespace Permafrost\PhpCodeSearch\Results\Nodes;
44

5+
use Permafrost\PhpCodeSearch\Code\GenericCodeLocation;
6+
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\HasLocation;
7+
use PhpParser\Comment;
8+
59
class CommentNode implements ResultNode
610
{
11+
use HasLocation;
12+
713
/** @var string */
814
public $value;
915

10-
public function __construct(string $value)
16+
public function __construct(Comment $node)
1117
{
12-
$this->value = $value;
18+
$this->value = $node->getText();
19+
$this->location = GenericCodeLocation::createFromNode($node);
1320
}
1421

15-
public static function create(string $value): self
22+
public static function create(Comment $node): self
1623
{
1724
return new static(...func_get_args());
1825
}

src/Results/Nodes/FunctionCallNode.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
namespace Permafrost\PhpCodeSearch\Results\Nodes;
44

5+
use Permafrost\PhpCodeSearch\Code\GenericCodeLocation;
6+
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\HasLocation;
57
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\TransformsArguments;
8+
use PhpParser\Node;
69

710
class FunctionCallNode implements ResultNode
811
{
12+
use HasLocation;
913
use TransformsArguments;
1014

1115
/** @var string */
@@ -14,13 +18,14 @@ class FunctionCallNode implements ResultNode
1418
/** @var array|ResultNode[]|ValueNode[] */
1519
public $args;
1620

17-
public function __construct(string $name, $args)
21+
public function __construct(Node\Expr\FuncCall $node)
1822
{
19-
$this->name = $name;
20-
$this->args = $this->transformArgumentsToNodes($args);
23+
$this->name = $node->name->toString();
24+
$this->args = $this->transformArgumentsToNodes($node->args);
25+
$this->location = GenericCodeLocation::createFromNode($node);
2126
}
2227

23-
public static function create(string $name, $args): self
28+
public static function create(Node\Expr\FuncCall $node): self
2429
{
2530
return new static(...func_get_args());
2631
}

0 commit comments

Comments
 (0)