Skip to content

Commit dd7364f

Browse files
authored
Merge pull request #4 from permafrost-dev/call-args
Call arguments, value transformations (v1.6)
2 parents d90db1f + 96f6abd commit dd7364f

34 files changed

+556
-78
lines changed

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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Permafrost\PhpCodeSearch\Results\Nodes;
4+
5+
use Permafrost\PhpCodeSearch\Support\Transformer;
6+
7+
class ArrayItemNode implements ValueNode, ResultNode
8+
{
9+
/** @var mixed|int|string|null */
10+
public $key;
11+
12+
/** @var array|mixed|ResultNode|ValueNode */
13+
public $value;
14+
15+
public function __construct($key, $value)
16+
{
17+
$this->key = Transformer::parserNodeToResultNode($key);
18+
$this->value = Transformer::parserNodeToResultNode($value);
19+
}
20+
21+
public function name(): string
22+
{
23+
return $this->key;
24+
}
25+
26+
public function value()
27+
{
28+
return $this->value;
29+
}
30+
}

src/Results/Nodes/ArrayNode.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
4+
namespace Permafrost\PhpCodeSearch\Results\Nodes;
5+
6+
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\TransformsArguments;
7+
use PhpParser\Node\Expr\Array_;
8+
9+
class ArrayNode implements ValueNode
10+
{
11+
use TransformsArguments;
12+
13+
/** @var array|ValueNode[]|ResultNode[] */
14+
public $value;
15+
16+
public function __construct($value)
17+
{
18+
if ($value instanceof Array_) {
19+
$value = $value->items;
20+
}
21+
22+
$this->value = $this->transformArgumentsToNodes($value);
23+
}
24+
25+
public function value()
26+
{
27+
return $this->value;
28+
}
29+
}

src/Results/Nodes/AssignmentNode.php

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

33
namespace Permafrost\PhpCodeSearch\Results\Nodes;
44

5-
class AssignmentNode implements ResultNode
5+
use Permafrost\PhpCodeSearch\Support\Transformer;
6+
7+
class AssignmentNode implements ResultNode, ValueNode
68
{
79
/** @var string */
810
public $variableName;
@@ -16,7 +18,7 @@ class AssignmentNode implements ResultNode
1618
public function __construct(string $variableName, $value)
1719
{
1820
$this->variableName = $variableName;
19-
$this->value = $value;
21+
$this->value = Transformer::parserNodeToResultNode($value);
2022
$this->name = $this->name();
2123
}
2224

@@ -29,4 +31,9 @@ public function name(): string
2931
{
3032
return $this->variableName;
3133
}
34+
35+
public function value()
36+
{
37+
return $this->value;
38+
}
3239
}
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+
}

src/Results/Nodes/FunctionCallNode.php

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

33
namespace Permafrost\PhpCodeSearch\Results\Nodes;
44

5+
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\TransformsArguments;
6+
57
class FunctionCallNode implements ResultNode
68
{
9+
use TransformsArguments;
10+
711
/** @var string */
812
public $name;
913

10-
public function __construct(string $name)
14+
/** @var array|ResultNode[]|ValueNode[] */
15+
public $args;
16+
17+
public function __construct(string $name, $args)
1118
{
1219
$this->name = $name;
20+
$this->args = $this->transformArgumentsToNodes($args);
1321
}
1422

15-
public static function create(string $name): self
23+
public static function create(string $name, $args): self
1624
{
1725
return new static(...func_get_args());
1826
}
@@ -21,4 +29,6 @@ public function name(): string
2129
{
2230
return $this->name;
2331
}
32+
33+
2434
}

src/Results/Nodes/MethodCallNode.php

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

33
namespace Permafrost\PhpCodeSearch\Results\Nodes;
44

5+
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\TransformsArguments;
6+
57
class MethodCallNode implements ResultNode
68
{
9+
use TransformsArguments;
10+
711
/** @var string */
812
public $variableName;
913

@@ -13,14 +17,19 @@ class MethodCallNode implements ResultNode
1317
/** @var string */
1418
public $name;
1519

16-
public function __construct(string $variableName, string $methodName)
20+
/** @var array|ResultNode[]|ValueNode[] */
21+
public $args;
22+
23+
24+
public function __construct(string $variableName, string $methodName, $args)
1725
{
1826
$this->variableName = $variableName;
1927
$this->methodName = $methodName;
28+
$this->args = $this->transformArgumentsToNodes($args);
2029
$this->name = $this->name();
2130
}
2231

23-
public static function create(string $variableName, string $methodName): self
32+
public static function create(string $variableName, string $methodName, $args): self
2433
{
2534
return new static(...func_get_args());
2635
}

src/Results/Nodes/OperationNode.php

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+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Permafrost\PhpCodeSearch\Results\Nodes;
4+
5+
class PropertyAccessNode implements ValueNode, ResultNode
6+
{
7+
/** @var string */
8+
public $objectName;
9+
10+
/** @var string */
11+
public $propertyName;
12+
13+
public function __construct(string $objectName, string $propertyName)
14+
{
15+
$this->objectName = $objectName;
16+
$this->propertyName = $propertyName;
17+
}
18+
19+
public function name(): string
20+
{
21+
return $this->objectName;
22+
}
23+
24+
public function value()
25+
{
26+
return $this->propertyName;
27+
}
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Permafrost\PhpCodeSearch\Results\Nodes\Scalar;
4+
5+
use Permafrost\PhpCodeSearch\Results\Nodes\ValueNode;
6+
use PhpParser\Node\Scalar\DNumber;
7+
use PhpParser\Node\Scalar\LNumber;
8+
9+
class NumberNode implements ValueNode
10+
{
11+
/** @var int|float */
12+
public $value;
13+
14+
public function __construct($value)
15+
{
16+
if ($value instanceof LNumber || $value instanceof DNumber) {
17+
$value = $value->value;
18+
}
19+
20+
$this->value = $value;
21+
}
22+
23+
public function value()
24+
{
25+
return $this->value;
26+
}
27+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Permafrost\PhpCodeSearch\Results\Nodes\Scalar;
4+
5+
use PhpParser\Node\Scalar\String_;
6+
7+
class StringNode implements \Permafrost\PhpCodeSearch\Results\Nodes\ValueNode
8+
{
9+
/** @var string */
10+
public $value;
11+
12+
public function __construct($value)
13+
{
14+
if ($value instanceof String_) {
15+
$value = $value->value;
16+
}
17+
18+
$this->value = $value;
19+
}
20+
21+
22+
public function value()
23+
{
24+
return $this->value;
25+
}
26+
}

src/Results/Nodes/StaticMethodCallNode.php

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

33
namespace Permafrost\PhpCodeSearch\Results\Nodes;
44

5+
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\TransformsArguments;
6+
57
class StaticMethodCallNode implements ResultNode
68
{
9+
use TransformsArguments;
10+
711
/** @var string */
812
public $className;
913

@@ -13,14 +17,18 @@ class StaticMethodCallNode implements ResultNode
1317
/** @var string */
1418
public $name;
1519

16-
public function __construct(string $className, string $methodName)
20+
/** @var array|ResultNode[]|ValueNode[] */
21+
public $args;
22+
23+
public function __construct(string $className, string $methodName, $args)
1724
{
1825
$this->className = $className;
1926
$this->methodName = $methodName;
27+
$this->args = $this->transformArgumentsToNodes($args);
2028
$this->name = $this->name();
2129
}
2230

23-
public static function create(string $className, string $methodName): self
31+
public static function create(string $className, string $methodName, $args): self
2432
{
2533
return new static(...func_get_args());
2634
}

0 commit comments

Comments
 (0)