Skip to content

Commit 3ec89de

Browse files
authored
Bump to Rector 1.2.5 and fix compatible of isObjectType() usage to use ClassLike instead of ClassMethod (#247)
* Bump to Rector 1.2.5 and fix compatible of isObjectType() usage on ClassLike * cs fix
1 parent ca45e6d commit 3ec89de

File tree

3 files changed

+62
-48
lines changed

3 files changed

+62
-48
lines changed

Diff for: composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"description": "Rector upgrades rules for Laravel Framework",
66
"require": {
77
"php": ">=8.2",
8-
"rector/rector": "^1.2"
8+
"rector/rector": "^1.2.5"
99
},
1010
"require-dev": {
1111
"nikic/php-parser": "^4.18",

Diff for: src/Rector/ClassMethod/AddArgumentDefaultValueRector.php

+27-18
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use PhpParser\Node;
99
use PhpParser\Node\Expr;
1010
use PhpParser\Node\Param;
11-
use PhpParser\Node\Stmt\ClassMethod;
11+
use PhpParser\Node\Stmt\ClassLike;
1212
use Rector\Contract\Rector\ConfigurableRectorInterface;
1313
use Rector\Rector\AbstractRector;
1414
use RectorLaravel\ValueObject\AddArgumentDefaultValue;
@@ -70,40 +70,49 @@ public function someMethod($value = false)
7070
*/
7171
public function getNodeTypes(): array
7272
{
73-
return [ClassMethod::class];
73+
return [ClassLike::class];
7474
}
7575

7676
/**
77-
* @param ClassMethod $node
77+
* @param ClassLike $node
7878
*/
79-
public function refactor(Node $node): ClassMethod
79+
public function refactor(Node $node): ?ClassLike
8080
{
81+
$hasChanged = false;
8182
foreach ($this->addedArguments as $addedArgument) {
8283
if (! $this->nodeTypeResolver->isObjectType($node, $addedArgument->getObjectType())) {
8384
continue;
8485
}
8586

86-
if (! $this->isName($node->name, $addedArgument->getMethod())) {
87-
continue;
88-
}
87+
foreach ($node->getMethods() as $classMethod) {
88+
if (! $this->isName($classMethod->name, $addedArgument->getMethod())) {
89+
continue;
90+
}
8991

90-
if (! isset($node->params[$addedArgument->getPosition()])) {
91-
continue;
92-
}
92+
if (! isset($classMethod->params[$addedArgument->getPosition()])) {
93+
continue;
94+
}
9395

94-
$position = $addedArgument->getPosition();
95-
$param = $node->params[$position];
96+
$position = $addedArgument->getPosition();
97+
$param = $classMethod->params[$position];
9698

97-
if ($param->default instanceof Expr) {
98-
continue;
99+
if ($param->default instanceof Expr) {
100+
continue;
101+
}
102+
103+
$classMethod->params[$position] = new Param($param->var, BuilderHelpers::normalizeValue(
104+
$addedArgument->getDefaultValue()
105+
));
106+
107+
$hasChanged = true;
99108
}
109+
}
100110

101-
$node->params[$position] = new Param($param->var, BuilderHelpers::normalizeValue(
102-
$addedArgument->getDefaultValue()
103-
));
111+
if ($hasChanged) {
112+
return $node;
104113
}
105114

106-
return $node;
115+
return null;
107116
}
108117

109118
/**

Diff for: src/Rector/MethodCall/ValidationRuleArrayStringValueToArrayRector.php

+34-29
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use PhpParser\Node\Expr\MethodCall;
1010
use PhpParser\Node\Expr\StaticCall;
1111
use PhpParser\Node\Scalar\String_;
12-
use PhpParser\Node\Stmt\ClassMethod;
12+
use PhpParser\Node\Stmt\ClassLike;
1313
use PhpParser\Node\Stmt\Return_;
1414
use PhpParser\NodeTraverser;
1515
use PHPStan\Type\ObjectType;
@@ -48,15 +48,15 @@ public function getRuleDefinition(): RuleDefinition
4848

4949
public function getNodeTypes(): array
5050
{
51-
return [MethodCall::class, StaticCall::class, ClassMethod::class];
51+
return [MethodCall::class, StaticCall::class, ClassLike::class];
5252
}
5353

5454
/**
55-
* @param MethodCall|StaticCall|ClassMethod $node
55+
* @param MethodCall|StaticCall|ClassLike $node
5656
*/
57-
public function refactor(Node $node): MethodCall|StaticCall|ClassMethod|null
57+
public function refactor(Node $node): MethodCall|StaticCall|ClassLike|null
5858
{
59-
if ($node instanceof ClassMethod) {
59+
if ($node instanceof ClassLike) {
6060
return $this->refactorClassMethod($node);
6161
}
6262

@@ -122,42 +122,47 @@ private function refactorCall(StaticCall|MethodCall $node): StaticCall|MethodCal
122122
return $this->processValidationRules($rulesArgument) ? $node : null;
123123
}
124124

125-
private function refactorClassMethod(ClassMethod $classMethod): ?ClassMethod
125+
private function refactorClassMethod(ClassLike $classLike): ?ClassLike
126126
{
127-
if (! $this->isObjectType($classMethod, new ObjectType('Illuminate\Foundation\Http\FormRequest'))) {
127+
if (! $this->isObjectType($classLike, new ObjectType('Illuminate\Foundation\Http\FormRequest'))) {
128128
return null;
129129
}
130130

131-
if (! $this->isName($classMethod, 'rules')) {
132-
return null;
133-
}
131+
$hasChanged = false;
132+
foreach ($classLike->getMethods() as $classMethod) {
133+
if (! $this->isName($classMethod, 'rules')) {
134+
continue;
135+
}
134136

135-
$changed = false;
137+
$changed = false;
138+
$this->traverseNodesWithCallable($classMethod, function (Node $node) use (&$changed, &$hasChanged): Return_|int|null {
139+
if ($changed) {
140+
$hasChanged = true;
136141

137-
$this->traverseNodesWithCallable($classMethod, function (Node $node) use (&$changed): Return_|int|null {
138-
if ($changed) {
139-
return NodeTraverser::STOP_TRAVERSAL;
140-
}
142+
return NodeTraverser::STOP_TRAVERSAL;
143+
}
141144

142-
if (! $node instanceof Return_) {
143-
return null;
144-
}
145+
if (! $node instanceof Return_) {
146+
return null;
147+
}
145148

146-
if (! $node->expr instanceof Array_) {
147-
return null;
148-
}
149+
if (! $node->expr instanceof Array_) {
150+
return null;
151+
}
149152

150-
if ($this->processValidationRules($node->expr)) {
151-
$changed = true;
153+
if ($this->processValidationRules($node->expr)) {
154+
$hasChanged = true;
155+
$changed = true;
152156

153-
return $node;
154-
}
157+
return $node;
158+
}
155159

156-
return null;
157-
});
160+
return null;
161+
});
162+
}
158163

159-
if ($changed) {
160-
return $classMethod;
164+
if ($hasChanged) {
165+
return $classLike;
161166
}
162167

163168
return null;

0 commit comments

Comments
 (0)