Skip to content

Commit

Permalink
Handle SimpleXMLElement in VariablePropertyFetchRule
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored and ondrejmirtes committed Jan 19, 2025
1 parent daeec74 commit b564ca4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/Rules/VariableVariables/VariablePropertyFetchRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\VerbosityLevel;
use SimpleXMLElement;
use function sprintf;

/**
Expand Down Expand Up @@ -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 [];
}
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ protected function getRule(): Rule
{
return new VariablePropertyFetchRule($this->createReflectionProvider(), [
'stdClass',
'SimpleXMLElement',
]);
}

Expand All @@ -29,4 +28,9 @@ public function testRule(): void
]);
}

public function testBug243(): void
{
$this->analyse([__DIR__ . '/data/bug243.php'], []);
}

}
7 changes: 7 additions & 0 deletions tests/Rules/VariableVariables/data/bug243.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Bug243;

function test(\SimpleXMLElement $xml) {
$xml->{'foo-bar'};
};

0 comments on commit b564ca4

Please sign in to comment.