Skip to content

Commit

Permalink
DefinedVariableRule - defined variables in metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 19, 2020
1 parent 17bde93 commit 385819c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,23 @@ public function getVariableType(string $variableName): Type
return $this->variableTypes[$variableName]->getType();
}

/**
* @return array<int, string>
*/
public function getDefinedVariables(): array
{
$variables = [];
foreach ($this->variableTypes as $variableName => $holder) {
if (!$holder->getCertainty()->yes()) {
continue;
}

$variables[] = $variableName;
}

return $variables;
}

private function isGlobalVariable(string $variableName): bool
{
return in_array($variableName, [
Expand Down
5 changes: 5 additions & 0 deletions src/Analyser/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public function hasVariableType(string $variableName): TrinaryLogic;

public function getVariableType(string $variableName): Type;

/**
* @return array<int, string>
*/
public function getDefinedVariables(): array;

public function hasConstant(Name $name): bool;

public function isInAnonymousFunction(): bool;
Expand Down
20 changes: 20 additions & 0 deletions src/Rules/Variables/DefinedVariableRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public function processNode(Node $node, Scope $scope): array
'statementOrder' => $node->getAttribute('statementOrder'),
'depth' => $node->getAttribute('expressionDepth'),
'order' => $node->getAttribute('expressionOrder'),
'variables' => $scope->getDefinedVariables(),
'parentVariables' => $this->getParentVariables($scope),
])
->build(),
];
Expand All @@ -77,6 +79,8 @@ public function processNode(Node $node, Scope $scope): array
'statementOrder' => $node->getAttribute('statementOrder'),
'depth' => $node->getAttribute('expressionDepth'),
'order' => $node->getAttribute('expressionOrder'),
'variables' => $scope->getDefinedVariables(),
'parentVariables' => $this->getParentVariables($scope),
])
->build(),
];
Expand All @@ -85,4 +89,20 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

/**
* @param Scope $scope
* @return array<int, array<int, string>>
*/
private function getParentVariables(Scope $scope): array
{
$variables = [];
$parent = $scope->getParentScope();
while ($parent !== null) {
$variables[] = $parent->getDefinedVariables();
$parent = $parent->getParentScope();
}

return $variables;
}

}

0 comments on commit 385819c

Please sign in to comment.