Skip to content

Commit

Permalink
Simplify detection of method inheritance (#677)
Browse files Browse the repository at this point in the history
  • Loading branch information
BackEndTea authored and maks-rafalko committed Mar 29, 2019
1 parent 6f93609 commit 1af42c6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 55 deletions.
20 changes: 4 additions & 16 deletions src/Mutator/FunctionSignature/ProtectedVisibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,10 @@ private function hasSameProtectedParentMethod(Node $node): bool
return true;
}

$parent = $reflection->getParentClass();

while ($parent) {
try {
$method = $parent->getMethod($node->name->name);

if ($method->isProtected()) {
return true;
}
} catch (\ReflectionException $e) {
return false;
} finally {
$parent = $parent->getParentClass();
}
try {
return $reflection->getMethod($node->name->name)->getPrototype()->isProtected();
} catch (\ReflectionException $e) {
return false;
}

return false;
}
}
43 changes: 4 additions & 39 deletions src/Mutator/FunctionSignature/PublicVisibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,45 +95,10 @@ private function hasSamePublicParentMethod(Node $node): bool
return true;
}

return $this->hasSamePublicMethodInInterface($node, $reflection) || $this->hasSamePublicMethodInParentClass($node, $reflection);
}

private function hasSamePublicMethodInInterface(Node $node, \ReflectionClass $reflection): bool
{
foreach ($reflection->getInterfaces() as $reflectionInterface) {
try {
$method = $reflectionInterface->getMethod($node->name->name);

if ($method->isPublic()) {
// we can't mutate because interface requires the same public visibility
return true;
}
} catch (\ReflectionException $e) {
continue;
}
}

return false;
}

private function hasSamePublicMethodInParentClass(Node $node, \ReflectionClass $reflection): bool
{
$parent = $reflection->getParentClass();

while ($parent) {
try {
$method = $parent->getMethod($node->name->name);

if ($method->isPublic()) {
return true;
}
} catch (\ReflectionException $e) {
return false;
} finally {
$parent = $parent->getParentClass();
}
try {
return $reflection->getMethod($node->name->name)->getPrototype()->isPublic();
} catch (\ReflectionException $e) {
return false;
}

return false;
}
}

0 comments on commit 1af42c6

Please sign in to comment.