Skip to content

Commit c1e50cc

Browse files
committed
Fix code compatible
1 parent 9cc30cf commit c1e50cc

File tree

6 files changed

+92
-82
lines changed

6 files changed

+92
-82
lines changed

src/NodeAnalyzer/ExpectedClassMethodAnalyzer.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
final readonly class ExpectedClassMethodAnalyzer
1919
{
2020
public function __construct(
21-
private SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
2221
private NodeNameResolver $nodeNameResolver,
2322
private NodeTypeResolver $nodeTypeResolver,
2423
) {}
@@ -31,7 +30,7 @@ public function findExpectedJobCallsWithClassMethod(ClassMethod $classMethod): ?
3130
$notExpectedMethodCalls = [];
3231
$reasonsToNotContinue = false;
3332

34-
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($classMethod, function (Node $node) use (
33+
SimpleCallableNodeTraverser::traverseNodesWithCallable($classMethod, function (Node $node) use (
3534
&$expectedMethodCalls,
3635
&$notExpectedMethodCalls,
3736
&$reasonsToNotContinue,
@@ -87,7 +86,7 @@ public function findExpectedEventCallsWithClassMethod(ClassMethod $classMethod):
8786
$notExpectedMethodCalls = [];
8887
$reasonsToNotContinue = false;
8988

90-
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($classMethod, function (Node $node) use (
89+
SimpleCallableNodeTraverser::traverseNodesWithCallable($classMethod, function (Node $node) use (
9190
&$expectedMethodCalls,
9291
&$notExpectedMethodCalls,
9392
&$reasonsToNotContinue,

src/NodeFactory/ModelFactoryNodeFactory.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public function __construct(
3434
private NodeNameResolver $nodeNameResolver,
3535
private NodeFactory $nodeFactory,
3636
private ValueResolver $valueResolver,
37-
private SimpleCallableNodeTraverser $simpleCallableNodeTraverser
3837
) {}
3938

4039
public function createEmptyFactory(string $name, Expr $expr): Class_
@@ -116,7 +115,7 @@ public function createEmptyConfigure(): ClassMethod
116115

117116
public function appendConfigure(ClassMethod $classMethod, string $name, Closure|ArrowFunction $callable): void
118117
{
119-
$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
118+
SimpleCallableNodeTraverser::traverseNodesWithCallable(
120119
(array) $classMethod->stmts,
121120
function (Node $node) use ($callable, $name): ?Return_ {
122121
if (! $node instanceof Return_) {
@@ -145,7 +144,7 @@ function (Node $node) use ($callable, $name): ?Return_ {
145144
*/
146145
private function fakerVariableToPropertyFetch(array $stmts, Param $param): void
147146
{
148-
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($stmts, function (Node $node) use (
147+
SimpleCallableNodeTraverser::traverseNodesWithCallable($stmts, function (Node $node) use (
149148
$param
150149
): ?PropertyFetch {
151150
if (! $node instanceof Variable) {

src/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector.php

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
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;
1618
use RectorLaravel\ValueObject\ReplaceRequestKeyAndMethodValue;
1719
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -62,30 +64,30 @@ public function getNodeTypes(): array
6264
return [Node::class, ArrayDimFetch::class, Variable::class, Isset_::class];
6365
}
6466

65-
/**
66-
* @return StaticCall|NotIdentical|1|null
67-
*/
68-
public function refactor(Node $node): StaticCall|NotIdentical|int|null
67+
public function refactor(Node $node): StaticCall|NotIdentical|null
6968
{
7069
if (! $node instanceof ArrayDimFetch && ! $node instanceof Variable && ! $node instanceof Isset_) {
71-
$this->traverseNodesWithCallable($node, function (Node $subNode) {
72-
if ($subNode instanceof ArrayDimFetch) {
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) {
7374

74-
if ($subNode->dim instanceof Scalar) {
75-
return null;
76-
}
75+
if ($subNode->dim instanceof Scalar) {
76+
return null;
77+
}
7778

78-
$this->traverseNodesWithCallable($subNode, function (Node $subSubNode) {
79-
if ($subSubNode instanceof Variable) {
80-
$subSubNode->setAttribute(self::IS_INSIDE_ARRAY_DIM_FETCH_WITH_DIM_NOT_SCALAR, true);
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);
8182

82-
return $subSubNode;
83-
}
83+
return $subSubNode;
84+
}
8485

85-
return null;
86-
});
87-
}
88-
});
86+
return null;
87+
});
88+
}
89+
});
90+
}
8991

9092
return null;
9193
}
@@ -115,10 +117,7 @@ public function refactor(Node $node): StaticCall|NotIdentical|int|null
115117
return $replaceValue;
116118
}
117119

118-
/**
119-
* @return ReplaceRequestKeyAndMethodValue|1|null
120-
*/
121-
public function findAllKeys(ArrayDimFetch $arrayDimFetch): ReplaceRequestKeyAndMethodValue|int|null
120+
public function findAllKeys(ArrayDimFetch $arrayDimFetch): ?ReplaceRequestKeyAndMethodValue
122121
{
123122
if (! $arrayDimFetch->dim instanceof Scalar) {
124123
return null;
@@ -171,10 +170,7 @@ private function getGetterMethodName(Variable $variable): ?string
171170
};
172171
}
173172

174-
/**
175-
* @return StaticCall|NotIdentical|1|null
176-
*/
177-
private function processIsset(Isset_ $isset): StaticCall|NotIdentical|int|null
173+
private function processIsset(Isset_ $isset): StaticCall|NotIdentical|null
178174
{
179175
if (count($isset->vars) < 1) {
180176
return null;

src/Rector/ArrayDimFetch/ServerVariableToRequestFacadeRector.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use PhpParser\Node\Expr\StaticCall;
1212
use PhpParser\Node\Scalar\InterpolatedString;
1313
use PhpParser\Node\Stmt\Unset_;
14+
use PHPStan\Analyser\Scope;
15+
use Rector\NodeTypeResolver\Node\AttributeKey;
1416
use RectorLaravel\AbstractRector;
1517
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1618
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -45,24 +47,27 @@ public function getNodeTypes(): array
4547
public function refactor(Node $node): ?StaticCall
4648
{
4749
if (! $node instanceof ArrayDimFetch) {
48-
$this->traverseNodesWithCallable($node, function (Node $subNode) {
49-
if (in_array($subNode::class, [Assign::class, Isset_::class, Unset_::class, InterpolatedString::class], true)
50-
&& (! $subNode instanceof Assign || $subNode->var instanceof ArrayDimFetch && $this->isName($subNode->var->var, '_SERVER'))) {
51-
$this->traverseNodesWithCallable($subNode, function (Node $subSubNode) {
52-
if (! $subSubNode instanceof ArrayDimFetch) {
53-
return null;
54-
}
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+
}
5559

56-
$subSubNode->setAttribute(self::IS_IN_SERVER_VARIABLE, true);
60+
$subSubNode->setAttribute(self::IS_IN_SERVER_VARIABLE, true);
5761

58-
return $subSubNode;
59-
});
62+
return $subSubNode;
63+
});
6064

61-
return $subNode;
62-
}
65+
return $subNode;
66+
}
6367

64-
return null;
65-
});
68+
return null;
69+
});
70+
}
6671

6772
return null;
6873
}

src/Rector/ArrayDimFetch/SessionVariableToSessionFacadeRector.php

Lines changed: 22 additions & 17 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;
@@ -73,28 +75,31 @@ public function getNodeTypes(): array
7375
*/
7476
public function refactor(Node $node): StaticCall|Expression|null
7577
{
76-
$this->traverseNodesWithCallable($node, function (Node $subNode) {
77-
if (! $subNode instanceof ArrayDimFetch) {
78-
return null;
79-
}
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+
}
8084

81-
if (! $subNode->dim instanceof Expr) {
82-
$subNode->setAttribute(self::IS_INSIDE_ARRAY_DIM_FETCH_WITH_DIM_NOT_EXPR, true);
83-
$this->traverseNodesWithCallable($subNode, function (Node $subSubNode) {
84-
if (! $subSubNode instanceof Variable) {
85-
return null;
86-
}
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+
}
8791

88-
$subSubNode->setAttribute(self::IS_INSIDE_ARRAY_DIM_FETCH_WITH_DIM_NOT_EXPR, true);
92+
$subSubNode->setAttribute(self::IS_INSIDE_ARRAY_DIM_FETCH_WITH_DIM_NOT_EXPR, true);
8993

90-
return $subSubNode;
91-
});
94+
return $subSubNode;
95+
});
9296

93-
return $subNode;
94-
}
97+
return $subNode;
98+
}
9599

96-
return null;
97-
});
100+
return null;
101+
});
102+
}
98103

99104
if (! $node instanceof Isset_ && ! $node instanceof Unset_ && ! $node instanceof ArrayDimFetch && ! $node instanceof Assign && ! $node instanceof FuncCall && ! $node instanceof Variable) {
100105
return null;

src/Rector/PropertyFetch/ReplaceFakerInstanceWithHelperRector.php

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
use PhpParser\Node\Expr\MethodCall;
1212
use PhpParser\Node\Expr\PropertyFetch;
1313
use PhpParser\Node\Scalar\InterpolatedString;
14+
use PHPStan\Analyser\Scope;
1415
use PHPStan\Reflection\ClassReflection;
1516
use PHPStan\Reflection\ReflectionProvider;
17+
use Rector\NodeTypeResolver\Node\AttributeKey;
1618
use Rector\Reflection\ReflectionResolver;
1719
use RectorLaravel\AbstractRector;
1820
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -77,30 +79,34 @@ public function getNodeTypes(): array
7779
public function refactor(Node $node): ?Node
7880
{
7981
if (! $node instanceof PropertyFetch && ! $node instanceof MethodCall && ! $node instanceof InterpolatedString) {
80-
$this->traverseNodesWithCallable($node, function (Node $subNode) {
81-
if (! $subNode instanceof MethodCall) {
82-
return null;
83-
}
82+
$scope = $node->getAttribute(AttributeKey::SCOPE);
83+
if ($scope instanceof Scope && $scope->isInFirstLevelStatement()) {
84+
85+
$this->traverseNodesWithCallable($node, function (Node $subNode) {
86+
if (! $subNode instanceof MethodCall) {
87+
return null;
88+
}
8489

85-
// The randomEnum() method is a special case where the faker instance is used
86-
// see https://github.com/spatie/laravel-enum#faker-provider
87-
if ($this->isName($subNode->name, 'randomEnum')) {
88-
$subNode->setAttribute(self::IS_IN_RANDOM_ENUM, true);
89-
$this->traverseNodesWithCallable($subNode, function (Node $subSubNode) {
90-
if (! $subSubNode instanceof PropertyFetch && ! $subSubNode instanceof InterpolatedString) {
91-
return null;
92-
}
90+
// The randomEnum() method is a special case where the faker instance is used
91+
// see https://github.com/spatie/laravel-enum#faker-provider
92+
if ($this->isName($subNode->name, 'randomEnum')) {
93+
$subNode->setAttribute(self::IS_IN_RANDOM_ENUM, true);
94+
$this->traverseNodesWithCallable($subNode, function (Node $subSubNode) {
95+
if (! $subSubNode instanceof PropertyFetch && ! $subSubNode instanceof InterpolatedString) {
96+
return null;
97+
}
9398

94-
$subSubNode->setAttribute(self::IS_IN_RANDOM_ENUM, true);
99+
$subSubNode->setAttribute(self::IS_IN_RANDOM_ENUM, true);
95100

96-
return $subSubNode;
97-
});
101+
return $subSubNode;
102+
});
98103

99-
return $subNode;
100-
}
104+
return $subNode;
105+
}
101106

102-
return null;
103-
});
107+
return null;
108+
});
109+
}
104110

105111
return null;
106112
}

0 commit comments

Comments
 (0)