Skip to content

Commit

Permalink
refactor: use FunctionLike Interface (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
jens1o authored and felixfbecker committed Oct 30, 2017
1 parent 744062c commit 1edbe35
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/CompletionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ private function suggestVariablesAtNode(Node $node, string $namePrefix = ''): ar

// Walk the AST upwards until a scope boundary is met
$level = $node;
while ($level && !ParserHelpers\isFunctionLike($level)) {
while ($level && !($level instanceof PhpParser\FunctionLike)) {
// Walk siblings before the node
$sibling = $level;
while ($sibling = $sibling->getPreviousSibling()) {
Expand All @@ -429,7 +429,7 @@ private function suggestVariablesAtNode(Node $node, string $namePrefix = ''): ar

// If the traversal ended because a function was met,
// also add its parameters and closure uses to the result list
if ($level && ParserHelpers\isFunctionLike($level) && $level->parameters !== null) {
if ($level && $level instanceof PhpParser\FunctionLike && $level->parameters !== null) {
foreach ($level->parameters->getValues() as $param) {
$paramName = $param->getName();
if (empty($namePrefix) || strpos($paramName, $namePrefix) !== false) {
Expand Down
4 changes: 2 additions & 2 deletions src/DefinitionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ public function resolveVariableToNode($var)
// Traverse the AST up
do {
// If a function is met, check the parameters and use statements
if (ParserHelpers\isFunctionLike($n)) {
if ($n instanceof PhpParser\FunctionLike) {
if ($n->parameters !== null) {
foreach ($n->parameters->getElements() as $param) {
if ($param->getName() === $name) {
Expand Down Expand Up @@ -1018,7 +1018,7 @@ public function getTypeFromNode($node)
// 1. doc block
// 2. return type hint
// 3. TODO: infer from return statements
if (ParserHelpers\isFunctionLike($node)) {
if ($node instanceof PhpParser\FunctionLike) {
// Functions/methods
$docBlock = $this->getDocBlock($node);
if (
Expand Down
10 changes: 1 addition & 9 deletions src/ParserHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function isConstantFetch(Node $node) : bool
$parent instanceof Node\Expression\CallExpression ||
$parent instanceof Node\Expression\ObjectCreationExpression ||
$parent instanceof Node\Expression\ScopedPropertyAccessExpression ||
isFunctionLike($parent) ||
$parent instanceof PhpParser\FunctionLike ||
(
$parent instanceof Node\Expression\BinaryExpression &&
$parent->operator->kind === PhpParser\TokenKind::InstanceOfKeyword
Expand All @@ -38,14 +38,6 @@ function getFunctionLikeDeclarationFromParameter(Node\Parameter $node)
return $node->parent->parent;
}

function isFunctionLike(Node $node)
{
return
$node instanceof Node\Statement\FunctionDeclaration ||
$node instanceof Node\MethodDeclaration ||
$node instanceof Node\Expression\AnonymousFunctionCreationExpression;
}

function isBooleanExpression($expression) : bool
{
if (!($expression instanceof Node\Expression\BinaryExpression)) {
Expand Down
4 changes: 2 additions & 2 deletions src/TreeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ private function collectDiagnostics($node)
$range = PhpParser\PositionUtilities::getRangeFromPosition($error->start, $error->length, $this->sourceFileNode->fileContents);

switch ($error->kind) {
case \Microsoft\PhpParser\DiagnosticKind::Error:
case PhpParser\DiagnosticKind::Error:
$severity = DiagnosticSeverity::ERROR;
break;
case \Microsoft\PhpParser\DiagnosticKind::Warning:
case PhpParser\DiagnosticKind::Warning:
default:
$severity = DiagnosticSeverity::WARNING;
break;
Expand Down

0 comments on commit 1edbe35

Please sign in to comment.