Skip to content

Commit

Permalink
[TypeDeclaration][Php 8.1] Do not skip return on parent anonymous cla…
Browse files Browse the repository at this point in the history
…ss method (#1403)
  • Loading branch information
samsonasik committed Dec 6, 2021
1 parent 9ca7320 commit b6b0e64
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

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

final class DoNotSkipReturnParentAnonymousClassMethod
{
public function run()
{
new class {
public function run()
{
return;
}
};

exit();
}
}

?>
-----
<?php

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

final class DoNotSkipReturnParentAnonymousClassMethod
{
public function run(): never
{
new class {
public function run()
{
return;
}
};

exit();
}
}

?>
17 changes: 15 additions & 2 deletions rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ public function refactor(Node $node): ?Node

private function shouldSkip(ClassMethod | Function_ $node): bool
{
$return = $this->betterNodeFinder->findFirstInstanceOf($node, Return_::class);
if ($return instanceof Return_) {
if ($this->hasReturnNode($node)) {
return true;
}

Expand Down Expand Up @@ -129,6 +128,20 @@ private function shouldSkip(ClassMethod | Function_ $node): bool
return $this->isName($node->returnType, 'never');
}

private function hasReturnNode(ClassMethod | Function_ $functionLike): bool
{
return (bool) $this->betterNodeFinder->findFirst((array) $functionLike->stmts, function (Node $subNode) use (
$functionLike
): bool {
if (! $subNode instanceof Return_) {
return false;
}

$parentFunctionOrClassMethod = $this->betterNodeFinder->findParentByTypes($subNode, $this->getNodeTypes());
return $parentFunctionOrClassMethod === $functionLike;
});
}

/**
* @param class-string<Node>[] $yieldAndConditionalNodes
*/
Expand Down

0 comments on commit b6b0e64

Please sign in to comment.