Skip to content
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
1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -392,5 +392,4 @@ parameters:
-
identifier: rector.noIntegerRefactorReturn
paths:
- rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php
- tests/Issues/InfiniteLoop/Rector/MethodCall/InfinityLoopRector.php
15 changes: 7 additions & 8 deletions rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
use PhpParser\Node\Expr\Cast\Int_;
use PhpParser\Node\Expr\Cast\String_;
use PhpParser\Node\Expr\Match_;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\NodeVisitor;
use PHPStan\Type\ObjectType;
use Rector\NodeAnalyzer\ExprAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php80\NodeAnalyzer\MatchSwitchAnalyzer;
use Rector\Php80\NodeFactory\MatchFactory;
use Rector\Php80\NodeResolver\SwitchExprsResolver;
Expand Down Expand Up @@ -83,25 +82,25 @@ public function getNodeTypes(): array

/**
* @param StmtsAware $node
* @return null|Node|NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN
*/
public function refactor(Node $node): null|Node|int
public function refactor(Node $node): null|Node
{
if (! is_array($node->stmts)) {
return null;
}

if ($node instanceof FunctionLike && $node->returnsByRef()) {
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}

$hasChanged = false;

foreach ($node->stmts as $key => $stmt) {
if (! $stmt instanceof Switch_) {
continue;
}

// possible reference override, where match
if ($stmt->getAttribute(AttributeKey::IS_INSIDE_BYREF_FUNCTION_LIKE)) {
continue;
}

$nextStmt = $node->stmts[$key + 1] ?? null;

$condAndExprs = $this->switchExprsResolver->resolve($stmt);
Expand Down
2 changes: 2 additions & 0 deletions src/NodeTypeResolver/Node/AttributeKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,6 @@ final class AttributeKey
public const IS_CLASS_CONST_VALUE = 'is_default_class_const_value';

public const IS_INSIDE_SYMFONY_PHP_CLOSURE = 'is_inside_symfony_php_closure';

public const IS_INSIDE_BYREF_FUNCTION_LIKE = 'is_inside_byref_function_like';
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PhpParser\NodeTraverser\SimpleNodeTraverser;

final class ByRefReturnNodeVisitor extends NodeVisitorAbstract implements DecoratingNodeVisitorInterface
{
Expand All @@ -36,9 +37,12 @@ public function enterNode(Node $node): ?Node
return null;
}

SimpleNodeTraverser::decorateWithAttributeValue($stmts, AttributeKey::IS_INSIDE_BYREF_FUNCTION_LIKE, true);

$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
$stmts,
static function (Node $node): int|null|Node {
// avoid nested functions or classes
if ($node instanceof Class_ || $node instanceof FunctionLike) {
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use PhpParser\NodeVisitorAbstract;
use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\NodeTraverser\SimpleTraverser;
use Rector\PhpParser\NodeTraverser\SimpleNodeTraverser;

final class PropertyOrClassConstDefaultNodeVisitor extends NodeVisitorAbstract implements DecoratingNodeVisitorInterface
{
Expand All @@ -24,13 +24,21 @@ public function enterNode(Node $node): ?Node
continue;
}

SimpleTraverser::decorateWithTrueAttribute($default, AttributeKey::IS_DEFAULT_PROPERTY_VALUE);
SimpleNodeTraverser::decorateWithAttributeValue(
$default,
AttributeKey::IS_DEFAULT_PROPERTY_VALUE,
true
);
}
}

if ($node instanceof ClassConst) {
foreach ($node->consts as $const) {
SimpleTraverser::decorateWithTrueAttribute($const->value, AttributeKey::IS_CLASS_CONST_VALUE);
SimpleNodeTraverser::decorateWithAttributeValue(
$const->value,
AttributeKey::IS_CLASS_CONST_VALUE,
true
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use PhpParser\NodeVisitorAbstract;
use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\NodeTraverser\SimpleTraverser;
use Rector\PhpParser\NodeTraverser\SimpleNodeTraverser;
use Rector\Symfony\NodeAnalyzer\SymfonyPhpClosureDetector;

final class SymfonyClosureNodeVisitor extends NodeVisitorAbstract implements DecoratingNodeVisitorInterface
Expand All @@ -29,7 +29,11 @@ public function enterNode(Node $node): ?Node
return null;
}

SimpleTraverser::decorateWithTrueAttribute($node, AttributeKey::IS_INSIDE_SYMFONY_PHP_CLOSURE);
SimpleNodeTraverser::decorateWithAttributeValue(
(array) $node->stmts,
AttributeKey::IS_INSIDE_SYMFONY_PHP_CLOSURE,
true
);

return null;
}
Expand Down
47 changes: 47 additions & 0 deletions src/PhpParser/NodeTraverser/SimpleNodeTraverser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Rector\PhpParser\NodeTraverser;

use PhpParser\Node;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt\Class_;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor;
use PhpParser\NodeVisitorAbstract;
use Rector\NodeTypeResolver\Node\AttributeKey;

final class SimpleNodeTraverser
{
/**
* @param Node[]|Node $nodesOrNode
* @param AttributeKey::* $attributeKey
*/
public static function decorateWithAttributeValue(array|Node $nodesOrNode, string $attributeKey, mixed $value): void
{
$callableNodeVisitor = new class($attributeKey, $value) extends NodeVisitorAbstract {
public function __construct(
private readonly string $attributeKey,
private readonly mixed $value
) {
}

public function enterNode(Node $node): ?int
{
// avoid nested functions or classes
if ($node instanceof Class_ || $node instanceof FunctionLike) {
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}

$node->setAttribute($this->attributeKey, $this->value);
return null;
}
};

$nodeTraverser = new NodeTraverser($callableNodeVisitor);

$nodes = $nodesOrNode instanceof Node ? [$nodesOrNode] : $nodesOrNode;
$nodeTraverser->traverse($nodes);
}
}
38 changes: 0 additions & 38 deletions src/PhpParser/NodeTraverser/SimpleTraverser.php

This file was deleted.