diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 0d0e22839f..c76f26bc61 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -371,6 +371,23 @@ public function getVariableType(string $variableName): Type return $this->variableTypes[$variableName]->getType(); } + /** + * @return array + */ + 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, [ diff --git a/src/Analyser/Scope.php b/src/Analyser/Scope.php index fcda30d371..12c86e344c 100644 --- a/src/Analyser/Scope.php +++ b/src/Analyser/Scope.php @@ -39,6 +39,11 @@ public function hasVariableType(string $variableName): TrinaryLogic; public function getVariableType(string $variableName): Type; + /** + * @return array + */ + public function getDefinedVariables(): array; + public function hasConstant(Name $name): bool; public function isInAnonymousFunction(): bool; diff --git a/src/Rules/Variables/DefinedVariableRule.php b/src/Rules/Variables/DefinedVariableRule.php index 2fe965c4ba..f50735792d 100644 --- a/src/Rules/Variables/DefinedVariableRule.php +++ b/src/Rules/Variables/DefinedVariableRule.php @@ -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(), ]; @@ -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(), ]; @@ -85,4 +89,20 @@ public function processNode(Node $node, Scope $scope): array return []; } + /** + * @param Scope $scope + * @return array> + */ + private function getParentVariables(Scope $scope): array + { + $variables = []; + $parent = $scope->getParentScope(); + while ($parent !== null) { + $variables[] = $parent->getDefinedVariables(); + $parent = $parent->getParentScope(); + } + + return $variables; + } + }