Skip to content

Commit

Permalink
Update ProtectedVisibility/PublicVisibility to guard against missing …
Browse files Browse the repository at this point in the history
…reflection

It seems that PHP-Parser can't provide reflection for anonymous classes yet.

Fixes infection#501 for time being until nikic/PHP-Parser#543 gets fixed.
  • Loading branch information
sanmai committed Oct 12, 2018
1 parent c4ebe3e commit 0ec4864
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Mutator/FunctionSignature/ProtectedVisibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ private function hasSameProtectedParentMethod(Node $node): bool
/** @var \ReflectionClass $reflection */
$reflection = $node->getAttribute(ReflectionVisitor::REFLECTION_CLASS_KEY);

if (!$reflection instanceof \ReflectionClass) {
// assuming the worst where interface has the same method
return true;
}

$parent = $reflection->getParentClass();

while ($parent) {
Expand Down
10 changes: 10 additions & 0 deletions src/Mutator/FunctionSignature/PublicVisibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ private function hasSamePublicMethodInInterface(Node $node): bool
/** @var \ReflectionClass $reflection */
$reflection = $node->getAttribute(ReflectionVisitor::REFLECTION_CLASS_KEY);

if (!$reflection instanceof \ReflectionClass) {
// assuming the worst where interface has the same method
return true;
}

foreach ($reflection->getInterfaces() as $reflectionInterface) {
try {
$method = $reflectionInterface->getMethod($node->name->name);
Expand All @@ -121,6 +126,11 @@ private function hasSamePublicMethodInParentClass(Node $node): bool
/** @var \ReflectionClass $reflection */
$reflection = $node->getAttribute(ReflectionVisitor::REFLECTION_CLASS_KEY);

if (!$reflection instanceof \ReflectionClass) {
// assuming the worst where interface has the same method
return true;
}

$parent = $reflection->getParentClass();

while ($parent) {
Expand Down
16 changes: 16 additions & 0 deletions tests/Mutator/FunctionSignature/ProtectedVisibilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,22 @@ private function bar()
{
}
}
PHP
];

yield 'it does not mutate an anonymous class because reflection is not avalable' => [
<<<'PHP'
<?php
function something()
{
return new class() {
protected function anything()
{
return null;
}
};
}
PHP
];
}
Expand Down
16 changes: 16 additions & 0 deletions tests/Mutator/FunctionSignature/PublicVisibilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,22 @@ protected function bar()
{
}
}
PHP
];

yield 'it does not mutate an anonymous class because reflection is not avalable' => [
<<<'PHP'
<?php
function something()
{
return new class() {
public function anything()
{
return null;
}
};
}
PHP
];
}
Expand Down

0 comments on commit 0ec4864

Please sign in to comment.