-
-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Traverser] Add ByRefReturnNodeVisitor (#3826)
Co-authored-by: Tomas Votruba <tomas.vot@gmail.com> Co-authored-by: GitHub Action <actions@github.com>
- Loading branch information
1 parent
a74c640
commit 2406dc8
Showing
7 changed files
with
84 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ByRefReturnNodeVisitor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\CallLike; | ||
use PhpParser\Node\FunctionLike; | ||
use PhpParser\Node\Stmt\Return_; | ||
use PhpParser\NodeTraverser; | ||
use PhpParser\NodeVisitorAbstract; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser; | ||
|
||
final class ByRefReturnNodeVisitor extends NodeVisitorAbstract | ||
{ | ||
public function __construct(private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser) | ||
{ | ||
} | ||
|
||
public function enterNode(Node $node): ?Node | ||
{ | ||
if (! $node instanceof FunctionLike) { | ||
return null; | ||
} | ||
|
||
if (! $node->returnsByRef()) { | ||
return null; | ||
} | ||
|
||
$stmts = $node->getStmts(); | ||
if ($stmts === null) { | ||
return null; | ||
} | ||
|
||
$this->simpleCallableNodeTraverser->traverseNodesWithCallable( | ||
$stmts, | ||
static function (Node $subNode) : int|null|Return_ { | ||
if ($subNode instanceof CallLike || $subNode instanceof FunctionLike) { | ||
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; | ||
} | ||
|
||
if (! $subNode instanceof Return_) { | ||
return null; | ||
} | ||
|
||
$subNode->setAttribute(AttributeKey::IS_BYREF_RETURN, true); | ||
return $subNode; | ||
} | ||
); | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters