-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathArrayCreationExpressionParser.php
51 lines (41 loc) · 1.48 KB
/
ArrayCreationExpressionParser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
namespace App\Parsers;
use App\Contexts\AbstractContext;
use App\Contexts\ArrayValue;
use Microsoft\PhpParser\MissingToken;
use Microsoft\PhpParser\Node;
use Microsoft\PhpParser\Node\Expression\ArrayCreationExpression;
use Microsoft\PhpParser\Node\Expression\CallExpression;
use Microsoft\PhpParser\Node\Expression\ObjectCreationExpression;
class ArrayCreationExpressionParser extends AbstractParser
{
/**
* @var ArrayValue
*/
protected AbstractContext $context;
private function isParentNode(Node $node, array $nodeClasses): bool
{
if ($node->getParent() !== null) {
if (in_array(get_class($node->getParent()), $nodeClasses)) {
return true;
}
return $this->isParentNode($node->getParent(), $nodeClasses);
}
return false;
}
public function parse(ArrayCreationExpression $node)
{
// If array is inside a method, for example Validator::validate(['
// then we need to ignore findable for ArrayValue because
// priority is given to App\Contexts\MethodCall or App\Contexts\ObjectValue
if (!$this->isParentNode($node, [CallExpression::class, ObjectCreationExpression::class])) {
$this->context->findable = true;
}
$this->context->autocompleting = $node->closeParenOrBracket instanceof MissingToken;
return $this->context;
}
public function initNewContext(): ?AbstractContext
{
return new ArrayValue;
}
}