Skip to content

Commit

Permalink
Skip non-final class method with no content in AddVoidReturnTypeWhere…
Browse files Browse the repository at this point in the history
…NoReturnRector (#5386)

* Skip non-final class method with no content

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Dec 24, 2023
1 parent 00b442f commit 012e8bb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

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

class SkipNonFinalEmpty
{
public function getItem()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if ($node->returnType !== null) {
// already has return type → skip
if ($node->returnType instanceof Node) {
return null;
}

Expand Down Expand Up @@ -120,6 +121,11 @@ private function shouldSkipClassMethod(ClassMethod|Function_|Closure $functionLi
return true;
}

// possibly required by child implementation
if ($this->isNotFinalAndEmpty($functionLike)) {
return true;
}

if ($functionLike->isProtected()) {
return ! $this->classModifierChecker->isInsideFinalClass($functionLike);
}
Expand All @@ -140,4 +146,13 @@ private function isNotFinalAndHasExceptionOnly(ClassMethod $classMethod): bool
$onlyStmt = $classMethod->stmts[0] ?? null;
return $onlyStmt instanceof Throw_;
}

private function isNotFinalAndEmpty(ClassMethod $classMethod): bool
{
if ($this->classModifierChecker->isInsideFinalClass($classMethod)) {
return false;
}

return $classMethod->stmts === [];
}
}

0 comments on commit 012e8bb

Please sign in to comment.