Remove direct NodeTravser::* where possible, use node visitor attribute key instead#7697
Remove direct NodeTravser::* where possible, use node visitor attribute key instead#7697TomasVotruba merged 3 commits intomainfrom
Conversation
|
|
||
| # handle next | ||
| - | ||
| identifier: rector.noIntegerRefactorReturn |
There was a problem hiding this comment.
beware of use in other rector-* packages as well.
| return null; | ||
| } | ||
|
|
||
| if ($node->getAttribute(AttributeKey::IS_ASSIGN_OP_VAR)) { |
There was a problem hiding this comment.
There is ContextAnalyzer->isLeftPartOfAssign() service for this cases, see
rector-src/src/NodeNestingScope/ContextAnalyzer.php
Lines 49 to 60 in 82f5181
There was a problem hiding this comment.
There was a problem hiding this comment.
That's not necessary, as hiding simple attributes.
|
I came here from the rector-laravel PR, could y'all please elaborate on why this is being done? I'm just curious why the direct |
|
@calebdw Basically, we used it as a hack that was saying "don't use this rule further, if you found node X". It's like using a container to get a service to fetch a controller that has access to container to get service we want. Instead, Rector rules should be able to work standalone as it is and have all the metadata to decide in the |
|
Ah, thanks for the explanation---I'll just need to learn to think about things differently and learn the various attribute keys! |
|
@calebdw No worries, it's a mess :D That's why I'm reducing these constants to |
|
@calebdw This article explains the way to add custom attribute keys if needed: |
|
hi, i have this case and i want to stop traversing if it finds a parameter before this deprecation this was enough i tried the is there any other alternative to this ? or does it need a custom solution ? |
|
@Orest-Divintari you can create custom visitor for that, for example: <?php
declare(strict_types=1);
namespace My\Rector\Visitor;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Param;
use PhpParser\NodeVisitorAbstract;
use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface;
use Rector\PhpParser\NodeTraverser\SimpleNodeTraverser;
final class IsInParamDefaultVisitor extends NodeVisitorAbstract implements DecoratingNodeVisitorInterface
{
public const IS_IN_PARAM_DEFAULT = 'is_in_param_default;
public function enterNode(Node $node)
{
if (! $node instanceof Param) {
return null;
}
if (! $node->default instanceof Expr) {
return null;
}
SimpleNodeTraverser::decorateWithAttributeValue($param->default, self::IS_IN_PARAM_DEFAULT, true);
return null;
}
}Then, register to rector config: <?php
use Rector\Config\RectorConfig;
use My\Rector\Visitor\HelloVisitor;
return RectorConfig::configure()
// other existing config ...
->registerDecoratingNodeVisitor(\My\Rector\Visitor\IsInParamDefaultVisitor::class);Then, on your custom rector rule, check it: if ($node->getAttribute(\My\Rector\Visitor\IsInParamDefaultVisitor::IS_IN_PARAM_DEFAULT) === true) {
return null;
} |
Thank you ! |
No description provided.