From b564ca479e7e735f750aaac4935af965572a7845 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sat, 28 Dec 2024 21:08:16 +0100 Subject: [PATCH] Handle SimpleXMLElement in VariablePropertyFetchRule --- .../VariablePropertyFetchRule.php | 15 ++++++++++++++- .../VariablePropertyFetchRuleTest.php | 6 +++++- tests/Rules/VariableVariables/data/bug243.php | 7 +++++++ 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 tests/Rules/VariableVariables/data/bug243.php diff --git a/src/Rules/VariableVariables/VariablePropertyFetchRule.php b/src/Rules/VariableVariables/VariablePropertyFetchRule.php index 73ff0888..61796b0b 100644 --- a/src/Rules/VariableVariables/VariablePropertyFetchRule.php +++ b/src/Rules/VariableVariables/VariablePropertyFetchRule.php @@ -10,6 +10,7 @@ use PHPStan\Rules\Rule; use PHPStan\Rules\RuleErrorBuilder; use PHPStan\Type\VerbosityLevel; +use SimpleXMLElement; use function sprintf; /** @@ -50,7 +51,11 @@ public function processNode(Node $node, Scope $scope): array continue; } - if ($this->isUniversalObjectCrate($this->reflectionProvider->getClass($referencedClass))) { + $classReflection = $this->reflectionProvider->getClass($referencedClass); + if ( + $this->isUniversalObjectCrate($classReflection) + || $this->isSimpleXMLElement($classReflection) + ) { return []; } } @@ -63,6 +68,14 @@ public function processNode(Node $node, Scope $scope): array ]; } + private function isSimpleXMLElement( + ClassReflection $classReflection + ): bool + { + return $classReflection->getName() === SimpleXMLElement::class + || $classReflection->isSubclassOf(SimpleXMLElement::class); + } + private function isUniversalObjectCrate( ClassReflection $classReflection ): bool diff --git a/tests/Rules/VariableVariables/VariablePropertyFetchRuleTest.php b/tests/Rules/VariableVariables/VariablePropertyFetchRuleTest.php index 9126d836..0f79cbfc 100644 --- a/tests/Rules/VariableVariables/VariablePropertyFetchRuleTest.php +++ b/tests/Rules/VariableVariables/VariablePropertyFetchRuleTest.php @@ -15,7 +15,6 @@ protected function getRule(): Rule { return new VariablePropertyFetchRule($this->createReflectionProvider(), [ 'stdClass', - 'SimpleXMLElement', ]); } @@ -29,4 +28,9 @@ public function testRule(): void ]); } + public function testBug243(): void + { + $this->analyse([__DIR__ . '/data/bug243.php'], []); + } + } diff --git a/tests/Rules/VariableVariables/data/bug243.php b/tests/Rules/VariableVariables/data/bug243.php new file mode 100644 index 00000000..a2db9fba --- /dev/null +++ b/tests/Rules/VariableVariables/data/bug243.php @@ -0,0 +1,7 @@ +{'foo-bar'}; +};