Skip to content

Commit

Permalink
[TypeDeclaration] Skip return void in abstract empty class in AddVoid…
Browse files Browse the repository at this point in the history
…ReturnTypeWhereNoReturnRector (#5311)
  • Loading branch information
TomasVotruba committed Dec 2, 2023
1 parent cd9ea2c commit 3b84030
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector\Fixture;

abstract class SkipAbstractEmptyClass
{
/**
* @return string[]
*/
public function getValues()
{
}
}
Expand Up @@ -81,7 +81,11 @@ public function refactor(Node $node): ?Node
$hasChanged = false;
$isFinal = $class->isFinal();

$this->traverseNodesWithCallable($node, function (Node $node) use ($class, &$hasChanged, $isFinal): ?PropertyFetch {
$this->traverseNodesWithCallable($node, function (Node $node) use (
$class,
&$hasChanged,
$isFinal
): ?PropertyFetch {
if (! $node instanceof MethodCall) {
return null;
}
Expand Down
Expand Up @@ -117,7 +117,7 @@ private function shouldSkipClassMethod(ClassMethod|Function_|Closure $functionLi
return ! $this->isInsideFinalClass($functionLike);
}

return false;
return $this->isInsideAbstractClass($functionLike) && $functionLike->getStmts() === [];
}

private function isInsideFinalClass(ClassMethod $classMethod): bool
Expand All @@ -129,4 +129,14 @@ private function isInsideFinalClass(ClassMethod $classMethod): bool

return $classReflection->isFinalByKeyword();
}

private function isInsideAbstractClass(ClassMethod $classMethod): bool
{
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
if (! $classReflection instanceof ClassReflection) {
return false;
}

return $classReflection->isAbstract();
}
}

0 comments on commit 3b84030

Please sign in to comment.