Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip non-final class method with no content in AddVoidReturnTypeWhereNoReturnRector #5386

Merged
merged 2 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 === [];
}
}