Skip to content

Commit 262d908

Browse files
committed
wip
1 parent 0643eb3 commit 262d908

9 files changed

Lines changed: 146 additions & 1 deletion

CHANGELOG.md

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

55
---
66

7+
## 1.6.0 - 2021-07-07
8+
9+
- all function call, static method call, method call nodes have an `args` property containing the value node(s) of the parsed arguments.
10+
- assignment nodes now have a `value` property and `value()` method.
11+
- strings and numbers are converted to `StringNode` and `NumberNode` nodes, respectively.
12+
- most values converted to Node classes that implement either `ResultNode`, `ValueNode`, or both.
13+
- operations (addition, concat, etc.) converted to Node classes that implement `OperationNode`.
14+
- fixed several bugs related to non-matches being returned as matches.
15+
716
## 1.5.3 - 2021-07-07
817

918
- fix issues with `Assignment` nodes

src/Results/Nodes/ArrayItemNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class ArrayItemNode implements ValueNode, ResultNode
88
{
9-
/** @var mixed|int|string */
9+
/** @var mixed|int|string|null */
1010
public $key;
1111

1212
/** @var array|mixed|ResultNode|ValueNode */
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+
use Permafrost\PhpCodeSearch\Support\Transformer;
6+
use PhpParser\Node\Expr\AssignOp;
7+
8+
class AssignmentOperationNode implements ResultNode, ValueNode
9+
{
10+
/** @var string */
11+
public $name;
12+
13+
/** @var mixed|ResultNode|ValueNode */
14+
public $value;
15+
16+
public function __construct(AssignOp $node)
17+
{
18+
$this->name = $node->var->name;
19+
$this->value = Transformer::parserNodeToResultNode($node->expr);
20+
}
21+
22+
public function name(): string
23+
{
24+
return $this->name;
25+
}
26+
27+
public function value()
28+
{
29+
return $this->value;
30+
}
31+
32+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Permafrost\PhpCodeSearch\Results\Nodes;
4+
5+
use Permafrost\PhpCodeSearch\Support\Transformer;
6+
use PhpParser\Node\Expr\BinaryOp;
7+
8+
class BinaryOperationNode implements OperationNode
9+
{
10+
/** @var string */
11+
public $symbol;
12+
13+
/** @var mixed|ResultNode|ValueNode */
14+
public $left;
15+
16+
/** @var mixed|ResultNode|ValueNode */
17+
public $right;
18+
19+
public function __construct(BinaryOp $node)
20+
{
21+
$this->symbol = $node->getOperatorSigil();
22+
$this->left = Transformer::parserNodeToResultNode($node->left);
23+
$this->right = Transformer::parserNodeToResultNode($node->right);
24+
}
25+
26+
public function symbol(): string
27+
{
28+
return $this->symbol;
29+
}
30+
31+
public function left()
32+
{
33+
return $this->left;
34+
}
35+
36+
public function right()
37+
{
38+
return $this->right;
39+
}
40+
41+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
4+
namespace Permafrost\PhpCodeSearch\Results\Nodes;
5+
6+
7+
interface OperationNode
8+
{
9+
public function symbol(): string;
10+
11+
/** @var mixed|ResultNode|ValueNode|OperationNode */
12+
public function left();
13+
14+
/** @var mixed|ResultNode|ValueNode|OperationNode */
15+
public function right();
16+
}

src/Support/Transformer.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Permafrost\PhpCodeSearch\Results\Nodes\ArrayItemNode;
66
use Permafrost\PhpCodeSearch\Results\Nodes\ArrayNode;
77
use Permafrost\PhpCodeSearch\Results\Nodes\AssignmentNode;
8+
use Permafrost\PhpCodeSearch\Results\Nodes\AssignmentOperationNode;
9+
use Permafrost\PhpCodeSearch\Results\Nodes\BinaryOperationNode;
810
use Permafrost\PhpCodeSearch\Results\Nodes\FunctionCallNode;
911
use Permafrost\PhpCodeSearch\Results\Nodes\MethodCallNode;
1012
use Permafrost\PhpCodeSearch\Results\Nodes\PropertyAccessNode;
@@ -16,6 +18,8 @@
1618
use PhpParser\Node\Expr\Array_;
1719
use PhpParser\Node\Expr\ArrayItem;
1820
use PhpParser\Node\Expr\Assign;
21+
use PhpParser\Node\Expr\AssignOp;
22+
use PhpParser\Node\Expr\BinaryOp;
1923
use PhpParser\Node\Expr\FuncCall;
2024
use PhpParser\Node\Expr\MethodCall;
2125
use PhpParser\Node\Expr\PropertyFetch;
@@ -93,6 +97,14 @@ public static function parserNodeToResultNode($node)
9397
return new PropertyAccessNode($value->var->name, $value->name->toString());
9498
}
9599

100+
if ($value instanceof BinaryOp) {
101+
return new BinaryOperationNode($value);
102+
}
103+
104+
if ($value instanceof AssignOp) {
105+
return new AssignmentOperationNode($value);
106+
}
107+
96108
return $node;
97109
}
98110
}

tests/SearcherTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,33 @@ public function it_finds_complex_assignments()
165165
$this->assertMatchesSnapshot($results->results);
166166
}
167167

168+
/** @test */
169+
public function it_finds_binary_operations()
170+
{
171+
$results = (new Searcher())
172+
->assignments(['obj'])
173+
->searchCode('<?' . "php
174+
\$obj = 1 + 3;
175+
");
176+
177+
$this->assertCount(1, $results->results);
178+
$this->assertMatchesSnapshot($results->results);
179+
}
180+
181+
/** @test */
182+
public function it_finds_assign_operations()
183+
{
184+
$results = (new Searcher())
185+
->assignments(['obj'])
186+
->searchCode('<?' . "php
187+
\$obj = 'hello ' . 'world';
188+
");
189+
190+
$this->assertCount(1, $results->results);
191+
$this->assertMatchesSnapshot($results->results);
192+
}
193+
194+
168195
/** @test */
169196
public function it_finds_variables()
170197
{
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-
2+
location: { column: 0, endLine: 2, startLine: 2 }
3+
node: { variableName: obj, value: { symbol: ., left: { value: 'hello ' }, right: { value: world } }, name: obj }
4+
snippet: { code: { 1: '<?php', 2: ' $obj = ''hello '' . ''world'';' } }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-
2+
location: { column: 0, endLine: 2, startLine: 2 }
3+
node: { variableName: obj, value: { symbol: +, left: { value: 1 }, right: { value: 3 } }, name: obj }
4+
snippet: { code: { 1: '<?php', 2: ' $obj = 1 + 3;' } }

0 commit comments

Comments
 (0)