-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathParserHelpers.php
116 lines (107 loc) Β· 4 KB
/
ParserHelpers.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
declare(strict_types=1);
namespace LanguageServer\ParserHelpers;
use Microsoft\PhpParser;
use Microsoft\PhpParser\Node;
function isConstantFetch(Node $node) : bool
{
$parent = $node->parent;
return
(
$node instanceof Node\QualifiedName &&
(
$parent instanceof Node\Expression ||
$parent instanceof Node\DelimitedList\ExpressionList ||
$parent instanceof Node\ArrayElement ||
($parent instanceof Node\Parameter && $node->parent->default === $node) ||
$parent instanceof Node\StatementNode ||
$parent instanceof Node\CaseStatementNode
) &&
!(
$parent instanceof Node\Expression\MemberAccessExpression ||
$parent instanceof Node\Expression\CallExpression ||
$parent instanceof Node\Expression\ObjectCreationExpression ||
$parent instanceof Node\Expression\ScopedPropertyAccessExpression ||
$parent instanceof PhpParser\FunctionLike ||
(
$parent instanceof Node\Expression\BinaryExpression &&
$parent->operator->kind === PhpParser\TokenKind::InstanceOfKeyword
)
));
}
function getFunctionLikeDeclarationFromParameter(Node\Parameter $node)
{
return $node->parent->parent;
}
function isBooleanExpression($expression) : bool
{
if (!($expression instanceof Node\Expression\BinaryExpression)) {
return false;
}
switch ($expression->operator->kind) {
case PhpParser\TokenKind::InstanceOfKeyword:
case PhpParser\TokenKind::GreaterThanToken:
case PhpParser\TokenKind::GreaterThanEqualsToken:
case PhpParser\TokenKind::LessThanToken:
case PhpParser\TokenKind::LessThanEqualsToken:
case PhpParser\TokenKind::AndKeyword:
case PhpParser\TokenKind::AmpersandAmpersandToken:
case PhpParser\TokenKind::LessThanEqualsGreaterThanToken:
case PhpParser\TokenKind::OrKeyword:
case PhpParser\TokenKind::BarBarToken:
case PhpParser\TokenKind::XorKeyword:
case PhpParser\TokenKind::ExclamationEqualsEqualsToken:
case PhpParser\TokenKind::ExclamationEqualsToken:
case PhpParser\TokenKind::CaretToken:
case PhpParser\TokenKind::EqualsEqualsEqualsToken:
case PhpParser\TokenKind::EqualsToken:
return true;
}
return false;
}
/**
* Tries to get the parent property declaration given a Node
* @param Node $node
* @return Node\PropertyDeclaration|null $node
*/
function tryGetPropertyDeclaration(Node $node)
{
if ($node instanceof Node\Expression\Variable &&
(($propertyDeclaration = $node->parent->parent) instanceof Node\PropertyDeclaration ||
($propertyDeclaration = $propertyDeclaration->parent) instanceof Node\PropertyDeclaration)
) {
return $propertyDeclaration;
}
return null;
}
/**
* Tries to get the parent ConstDeclaration or ClassConstDeclaration given a Node
* @param Node $node
* @return Node\Statement\ConstDeclaration|Node\ClassConstDeclaration|null $node
*/
function tryGetConstOrClassConstDeclaration(Node $node)
{
if (
$node instanceof Node\ConstElement && (
($constDeclaration = $node->parent->parent) instanceof Node\ClassConstDeclaration ||
$constDeclaration instanceof Node\Statement\ConstDeclaration )
) {
return $constDeclaration;
}
return null;
}
/**
* Returns true if the node is a usage of `define`.
* e.g. define('TEST_DEFINE_CONSTANT', false);
* @param Node $node
* @return bool
*/
function isConstDefineExpression(Node $node): bool
{
return $node instanceof Node\Expression\CallExpression
&& $node->callableExpression instanceof Node\QualifiedName
&& strtolower($node->callableExpression->getText()) === 'define'
&& isset($node->argumentExpressionList->children[0])
&& $node->argumentExpressionList->children[0]->expression instanceof Node\StringLiteral
&& isset($node->argumentExpressionList->children[2]);
}