Skip to content

Commit ba02a7c

Browse files
committed
Revert "[alternative] Use NodeVisitor for init attribute flag over global Node traverse (driftingly#444)"
This reverts commit a8408c7.
1 parent 2fdc0c4 commit ba02a7c

10 files changed

Lines changed: 144 additions & 180 deletions

.github/workflows/tests.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ jobs:
2525

2626
- uses: ramsey/composer-install@v3
2727

28-
- run: vendor/bin/phpunit
28+
- run: vendor/bin/phpunit tests

README.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,6 @@ use Rector\Config\RectorConfig;
105105
use RectorLaravel\Rector\FuncCall\RemoveDumpDataDeadCodeRector;
106106

107107
return RectorConfig::configure()
108-
// include default config
109-
->withSets([
110-
__DIR__ . '/vendor/driftingly/rector-laravel/config/config.php'
111-
])
112108
->withConfiguredRule(RemoveDumpDataDeadCodeRector::class, [
113109
'dd', 'dump', 'var_dump'
114110
]);
@@ -131,10 +127,6 @@ use Rector\Config\RectorConfig;
131127
use RectorLaravel\Rector\MethodCall\ResponseHelperCallToJsonResponseRector;
132128

133129
return RectorConfig::configure()
134-
// include default config
135-
->withSets([
136-
__DIR__ . '/vendor/driftingly/rector-laravel/config/config.php'
137-
])
138130
->withRules([
139131
ResponseHelperCallToJsonResponseRector::class,
140132
]);

config/config.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,5 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6-
use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface;
7-
use RectorLaravel\NodeVisitor\ArrayDimFetchContextNodeVisitor;
8-
use RectorLaravel\NodeVisitor\RandomEnumContextNodeVisitor;
96

10-
/**
11-
* to be imported, don't use RectorConfigBuilder for safe usage
12-
*/
13-
return static function (RectorConfig $rectorConfig): void {
14-
$rectorConfig->singleton(ArrayDimFetchContextNodeVisitor::class);
15-
$rectorConfig->tag(ArrayDimFetchContextNodeVisitor::class, DecoratingNodeVisitorInterface::class);
16-
17-
$rectorConfig->singleton(RandomEnumContextNodeVisitor::class);
18-
$rectorConfig->tag(RandomEnumContextNodeVisitor::class, DecoratingNodeVisitorInterface::class);
19-
};
7+
return static function (RectorConfig $rectorConfig): void {};

phpunit.xml

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,8 @@
99
displayDetailsOnTestsThatTriggerWarnings="true"
1010
>
1111
<testsuites>
12-
<testsuite name="commands">
13-
<directory>tests/Commands</directory>
14-
</testsuite>
15-
<testsuite name="sets">
16-
<directory>tests/Sets</directory>
17-
</testsuite>
18-
<testsuite name="rector">
19-
<directory>tests/Rector</directory>
20-
</testsuite>
21-
<!--
22-
This on the last as tests extends AbstractLazyTestCase
23-
that clear configuration
24-
-->
25-
<testsuite name="node-analizer">
26-
<directory>tests/NodeAnalyzer</directory>
12+
<testsuite name="main">
13+
<directory>tests</directory>
2714
</testsuite>
2815
</testsuites>
2916
</phpunit>

src/NodeVisitor/ArrayDimFetchContextNodeVisitor.php

Lines changed: 0 additions & 78 deletions
This file was deleted.

src/NodeVisitor/RandomEnumContextNodeVisitor.php

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
use PhpParser\Node\Expr\Variable;
1313
use PhpParser\Node\Scalar;
1414
use PhpParser\Node\Scalar\String_;
15+
use PHPStan\Analyser\Scope;
16+
use Rector\NodeTypeResolver\Node\AttributeKey;
1517
use RectorLaravel\AbstractRector;
16-
use RectorLaravel\NodeVisitor\ArrayDimFetchContextNodeVisitor;
1718
use RectorLaravel\ValueObject\ReplaceRequestKeyAndMethodValue;
1819
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1920
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -23,6 +24,8 @@
2324
*/
2425
class RequestVariablesToRequestFacadeRector extends AbstractRector
2526
{
27+
private const string IS_INSIDE_ARRAY_DIM_FETCH_WITH_DIM_NOT_SCALAR = 'is_inside_array_dim_fetch_with_dim_not_scalar';
28+
2629
public function getRuleDefinition(): RuleDefinition
2730
{
2831
return new RuleDefinition(
@@ -58,16 +61,39 @@ public function getRuleDefinition(): RuleDefinition
5861

5962
public function getNodeTypes(): array
6063
{
61-
return [ArrayDimFetch::class, Variable::class, Isset_::class];
64+
return [Node::class, ArrayDimFetch::class, Variable::class, Isset_::class];
6265
}
6366

64-
/**
65-
* @param ArrayDimFetch|Variable|Isset_ $node
66-
*/
6767
public function refactor(Node $node): StaticCall|NotIdentical|null
6868
{
69+
if (! $node instanceof ArrayDimFetch && ! $node instanceof Variable && ! $node instanceof Isset_) {
70+
$scope = $node->getAttribute(AttributeKey::SCOPE);
71+
if ($scope instanceof Scope && $scope->isInFirstLevelStatement()) {
72+
$this->traverseNodesWithCallable($node, function (Node $subNode) {
73+
if ($subNode instanceof ArrayDimFetch) {
74+
75+
if ($subNode->dim instanceof Scalar) {
76+
return null;
77+
}
78+
79+
$this->traverseNodesWithCallable($subNode, function (Node $subSubNode) {
80+
if ($subSubNode instanceof Variable) {
81+
$subSubNode->setAttribute(self::IS_INSIDE_ARRAY_DIM_FETCH_WITH_DIM_NOT_SCALAR, true);
82+
83+
return $subSubNode;
84+
}
85+
86+
return null;
87+
});
88+
}
89+
});
90+
}
91+
92+
return null;
93+
}
94+
6995
if ($node instanceof Variable) {
70-
if ($node->getAttribute(ArrayDimFetchContextNodeVisitor::IS_INSIDE_ARRAY_DIM_FETCH_WITH_DIM_NOT_SCALAR) === true) {
96+
if ($node->getAttribute(self::IS_INSIDE_ARRAY_DIM_FETCH_WITH_DIM_NOT_SCALAR) === true) {
7197
return null;
7298
}
7399

src/Rector/ArrayDimFetch/ServerVariableToRequestFacadeRector.php

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66
use PhpParser\Node\Arg;
77
use PhpParser\Node\Expr;
88
use PhpParser\Node\Expr\ArrayDimFetch;
9+
use PhpParser\Node\Expr\Assign;
10+
use PhpParser\Node\Expr\Isset_;
911
use PhpParser\Node\Expr\StaticCall;
12+
use PhpParser\Node\Scalar\InterpolatedString;
13+
use PhpParser\Node\Stmt\Unset_;
14+
use PHPStan\Analyser\Scope;
15+
use Rector\NodeTypeResolver\Node\AttributeKey;
1016
use RectorLaravel\AbstractRector;
11-
use RectorLaravel\NodeVisitor\ArrayDimFetchContextNodeVisitor;
1217
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1318
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1419

@@ -17,6 +22,8 @@
1722
*/
1823
class ServerVariableToRequestFacadeRector extends AbstractRector
1924
{
25+
private const string IS_IN_SERVER_VARIABLE = 'is_in_server_variable';
26+
2027
public function getRuleDefinition(): RuleDefinition
2128
{
2229
return new RuleDefinition(
@@ -34,14 +41,37 @@ public function getRuleDefinition(): RuleDefinition
3441

3542
public function getNodeTypes(): array
3643
{
37-
return [ArrayDimFetch::class];
44+
return [Node::class, ArrayDimFetch::class];
3845
}
3946

40-
/**
41-
* @param ArrayDimFetch $node
42-
*/
4347
public function refactor(Node $node): ?StaticCall
4448
{
49+
if (! $node instanceof ArrayDimFetch) {
50+
$scope = $node->getAttribute(AttributeKey::SCOPE);
51+
if ($scope instanceof Scope && $scope->isInFirstLevelStatement()) {
52+
$this->traverseNodesWithCallable($node, function (Node $subNode) {
53+
if (in_array($subNode::class, [Assign::class, Isset_::class, Unset_::class, InterpolatedString::class], true)
54+
&& (! $subNode instanceof Assign || $subNode->var instanceof ArrayDimFetch && $this->isName($subNode->var->var, '_SERVER'))) {
55+
$this->traverseNodesWithCallable($subNode, function (Node $subSubNode) {
56+
if (! $subSubNode instanceof ArrayDimFetch) {
57+
return null;
58+
}
59+
60+
$subSubNode->setAttribute(self::IS_IN_SERVER_VARIABLE, true);
61+
62+
return $subSubNode;
63+
});
64+
65+
return $subNode;
66+
}
67+
68+
return null;
69+
});
70+
}
71+
72+
return null;
73+
}
74+
4575
if (! $this->isName($node->var, '_SERVER')) {
4676
return null;
4777
}
@@ -50,7 +80,7 @@ public function refactor(Node $node): ?StaticCall
5080
return null;
5181
}
5282

53-
if ($node->getAttribute(ArrayDimFetchContextNodeVisitor::IS_IN_SERVER_VARIABLE) === true) {
83+
if ($node->getAttribute(self::IS_IN_SERVER_VARIABLE) === true) {
5484
return null;
5585
}
5686

src/Rector/ArrayDimFetch/SessionVariableToSessionFacadeRector.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use PhpParser\Node\Expr\Variable;
1414
use PhpParser\Node\Stmt\Expression;
1515
use PhpParser\Node\Stmt\Unset_;
16+
use PHPStan\Analyser\Scope;
17+
use Rector\NodeTypeResolver\Node\AttributeKey;
1618
use RectorLaravel\AbstractRector;
1719
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1820
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -58,6 +60,7 @@ public function getRuleDefinition(): RuleDefinition
5860
public function getNodeTypes(): array
5961
{
6062
return [
63+
Node::class,
6164
Isset_::class,
6265
Unset_::class,
6366
ArrayDimFetch::class,
@@ -72,6 +75,36 @@ public function getNodeTypes(): array
7275
*/
7376
public function refactor(Node $node): StaticCall|Expression|null
7477
{
78+
$scope = $node->getAttribute(AttributeKey::SCOPE);
79+
if ($scope instanceof Scope && $scope->isInFirstLevelStatement()) {
80+
$this->traverseNodesWithCallable($node, function (Node $subNode) {
81+
if (! $subNode instanceof ArrayDimFetch) {
82+
return null;
83+
}
84+
85+
if (! $subNode->dim instanceof Expr) {
86+
$subNode->setAttribute(self::IS_INSIDE_ARRAY_DIM_FETCH_WITH_DIM_NOT_EXPR, true);
87+
$this->traverseNodesWithCallable($subNode, function (Node $subSubNode) {
88+
if (! $subSubNode instanceof Variable) {
89+
return null;
90+
}
91+
92+
$subSubNode->setAttribute(self::IS_INSIDE_ARRAY_DIM_FETCH_WITH_DIM_NOT_EXPR, true);
93+
94+
return $subSubNode;
95+
});
96+
97+
return $subNode;
98+
}
99+
100+
return null;
101+
});
102+
}
103+
104+
if (! $node instanceof Isset_ && ! $node instanceof Unset_ && ! $node instanceof ArrayDimFetch && ! $node instanceof Assign && ! $node instanceof FuncCall && ! $node instanceof Variable) {
105+
return null;
106+
}
107+
75108
if ($node instanceof ArrayDimFetch) {
76109
return $this->processDimFetch($node);
77110
}

0 commit comments

Comments
 (0)