2017-04-19 05:48:26 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace LanguageServer;
|
|
|
|
|
|
|
|
use Microsoft\PhpParser as Tolerant;
|
|
|
|
|
2017-05-19 20:39:16 +00:00
|
|
|
class ParserHelpers {
|
2017-04-19 05:48:26 +00:00
|
|
|
public static function isConstantFetch(Tolerant\Node $node) : bool {
|
2017-04-20 07:58:26 +00:00
|
|
|
$parent = $node->parent;
|
2017-04-19 05:48:26 +00:00
|
|
|
return
|
|
|
|
(
|
|
|
|
$node instanceof Tolerant\Node\QualifiedName &&
|
|
|
|
(
|
|
|
|
// $node->parent instanceof Tolerant\Node\Statement\ExpressionStatement ||
|
2017-04-20 07:58:26 +00:00
|
|
|
$parent instanceof Tolerant\Node\Expression ||
|
|
|
|
$parent instanceof Tolerant\Node\DelimitedList\ExpressionList ||
|
|
|
|
$parent instanceof Tolerant\Node\ArrayElement ||
|
|
|
|
($parent instanceof Tolerant\Node\Parameter && $node->parent->default === $node) ||
|
|
|
|
$parent instanceof Tolerant\Node\StatementNode ||
|
|
|
|
$parent instanceof Tolerant\Node\CaseStatementNode
|
2017-04-19 05:48:26 +00:00
|
|
|
) &&
|
|
|
|
!(
|
2017-04-20 07:58:26 +00:00
|
|
|
$parent instanceof Tolerant\Node\Expression\MemberAccessExpression ||
|
|
|
|
$parent instanceof Tolerant\Node\Expression\CallExpression ||
|
|
|
|
$parent instanceof Tolerant\Node\Expression\ObjectCreationExpression ||
|
|
|
|
$parent instanceof Tolerant\Node\Expression\ScopedPropertyAccessExpression ||
|
|
|
|
self::isFunctionLike($parent) ||
|
2017-04-19 05:48:26 +00:00
|
|
|
(
|
2017-04-20 07:58:26 +00:00
|
|
|
$parent instanceof Tolerant\Node\Expression\BinaryExpression &&
|
|
|
|
$parent->operator->kind === Tolerant\TokenKind::InstanceOfKeyword
|
2017-04-19 05:48:26 +00:00
|
|
|
)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getFunctionLikeDeclarationFromParameter(Tolerant\Node\Parameter $node) {
|
|
|
|
return $node->parent->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function isFunctionLike(Tolerant\Node $node) {
|
|
|
|
return
|
|
|
|
$node instanceof Tolerant\Node\Statement\FunctionDeclaration ||
|
|
|
|
$node instanceof Tolerant\Node\MethodDeclaration ||
|
|
|
|
$node instanceof Tolerant\Node\Expression\AnonymousFunctionCreationExpression;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function isBooleanExpression($expression) : bool {
|
|
|
|
if (!($expression instanceof Tolerant\Node\Expression\BinaryExpression)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
switch ($expression->operator->kind) {
|
|
|
|
case Tolerant\TokenKind::InstanceOfKeyword:
|
|
|
|
case Tolerant\TokenKind::GreaterThanToken:
|
|
|
|
case Tolerant\TokenKind::GreaterThanEqualsToken:
|
|
|
|
case Tolerant\TokenKind::LessThanToken:
|
|
|
|
case Tolerant\TokenKind::LessThanEqualsToken:
|
|
|
|
case Tolerant\TokenKind::AndKeyword:
|
|
|
|
case Tolerant\TokenKind::AmpersandAmpersandToken:
|
|
|
|
case Tolerant\TokenKind::LessThanEqualsGreaterThanToken:
|
|
|
|
case Tolerant\TokenKind::OrKeyword:
|
|
|
|
case Tolerant\TokenKind::BarBarToken:
|
|
|
|
case Tolerant\TokenKind::XorKeyword:
|
|
|
|
case Tolerant\TokenKind::ExclamationEqualsEqualsToken:
|
|
|
|
case Tolerant\TokenKind::ExclamationEqualsToken:
|
|
|
|
case Tolerant\TokenKind::CaretToken:
|
|
|
|
case Tolerant\TokenKind::EqualsEqualsEqualsToken:
|
|
|
|
case Tolerant\TokenKind::EqualsToken:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-19 20:39:16 +00:00
|
|
|
|
2017-04-19 05:48:26 +00:00
|
|
|
/**
|
|
|
|
* Tries to get the parent property declaration given a Node
|
|
|
|
* @param Tolerant\Node $node
|
|
|
|
* @return Tolerant\Node\PropertyDeclaration | null $node
|
|
|
|
*/
|
|
|
|
public static function tryGetPropertyDeclaration(Tolerant\Node $node) {
|
|
|
|
if ($node instanceof Tolerant\Node\Expression\Variable &&
|
|
|
|
(($propertyDeclaration = $node->parent->parent) instanceof Tolerant\Node\PropertyDeclaration ||
|
|
|
|
($propertyDeclaration = $propertyDeclaration->parent) instanceof Tolerant\Node\PropertyDeclaration)
|
|
|
|
) {
|
|
|
|
return $propertyDeclaration;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tries to get the parent ConstDeclaration or ClassConstDeclaration given a Node
|
|
|
|
* @param Tolerant\Node $node
|
|
|
|
* @return Tolerant\Node\Statement\ConstDeclaration | Tolerant\Node\ClassConstDeclaration | null $node
|
|
|
|
*/
|
|
|
|
public static function tryGetConstOrClassConstDeclaration(Tolerant\Node $node) {
|
|
|
|
if (
|
|
|
|
$node instanceof Tolerant\Node\ConstElement && (
|
|
|
|
($constDeclaration = $node->parent->parent) instanceof Tolerant\Node\ClassConstDeclaration ||
|
|
|
|
$constDeclaration instanceof Tolerant\Node\Statement\ConstDeclaration )
|
|
|
|
) {
|
|
|
|
return $constDeclaration;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2017-05-22 22:31:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the node is a usage of `define`.
|
|
|
|
* e.g. define('TEST_DEFINE_CONSTANT', false);
|
|
|
|
* @param Tolerant\Node $node
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isConstDefineExpression(Tolerant\Node $node): bool {
|
|
|
|
return $node instanceof Tolerant\Node\Expression\CallExpression
|
|
|
|
&& $node->callableExpression instanceof Tolerant\Node\QualifiedName
|
|
|
|
&& strtolower($node->callableExpression->getText()) === 'define'
|
|
|
|
&& isset($node->argumentExpressionList->children[0])
|
|
|
|
&& $node->argumentExpressionList->children[0]->expression instanceof Tolerant\Node\StringLiteral
|
|
|
|
&& isset($node->argumentExpressionList->children[2]);
|
|
|
|
}
|
2017-04-19 05:48:26 +00:00
|
|
|
}
|