Skip to content

Commit 8d0bc89

Browse files
committed
Fix rector rule to handle static method calls and update test expectations
1 parent 67f660f commit 8d0bc89

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

composer.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,12 @@
3838
},
3939
"support": {
4040
"source": "https://github.com/cakephp/upgrade"
41+
},
42+
"require-dev": {
43+
"cakephp/cakephp": "5.x-dev",
44+
"cakephp/cakephp-codesniffer": "^5.0",
45+
"cakephp/migrations": "^4.5.0",
46+
"mikey179/vfsstream": "^1.6.8",
47+
"phpunit/phpunit": "^10.5.38"
4148
}
4249
}

src/Rector/Rector/MethodCall/RemoveAssignmentFromVoidMethodRector.php

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Expr\Assign;
99
use PhpParser\Node\Expr\MethodCall;
10+
use PhpParser\Node\Expr\StaticCall;
1011
use PhpParser\Node\Stmt\Expression;
1112
use Rector\Contract\Rector\ConfigurableRectorInterface;
1213
use Rector\Rector\AbstractRector;
@@ -63,23 +64,41 @@ public function refactor(Node $node): ?Node
6364
}
6465

6566
$assign = $node->expr;
66-
if (!$assign->expr instanceof MethodCall) {
67-
return null;
68-
}
6967

70-
$methodCall = $assign->expr;
68+
// Handle instance method calls
69+
if ($assign->expr instanceof MethodCall) {
70+
$methodCall = $assign->expr;
7171

72-
foreach ($this->voidMethods as $voidMethod) {
73-
if (!$this->isObjectType($methodCall->var, $voidMethod->getObjectType())) {
74-
continue;
75-
}
72+
foreach ($this->voidMethods as $voidMethod) {
73+
if (!$this->isObjectType($methodCall->var, $voidMethod->getObjectType())) {
74+
continue;
75+
}
7676

77-
if (!$this->isName($methodCall->name, $voidMethod->getMethod())) {
78-
continue;
77+
if (!$this->isName($methodCall->name, $voidMethod->getMethod())) {
78+
continue;
79+
}
80+
81+
// Replace the assignment with just the method call
82+
return new Expression($methodCall);
7983
}
84+
}
8085

81-
// Replace the assignment with just the method call
82-
return new Expression($methodCall);
86+
// Handle static method calls
87+
if ($assign->expr instanceof StaticCall) {
88+
$staticCall = $assign->expr;
89+
90+
foreach ($this->voidMethods as $voidMethod) {
91+
if (!$this->isName($staticCall->class, $voidMethod->getClass())) {
92+
continue;
93+
}
94+
95+
if (!$this->isName($staticCall->name, $voidMethod->getMethod())) {
96+
continue;
97+
}
98+
99+
// Replace the assignment with just the static call
100+
return new Expression($staticCall);
101+
}
83102
}
84103

85104
return null;

tests/test_apps/upgraded/RectorCommand-testApply60/src/VoidMethods.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class VoidMethods
2121

2222
public function testAllowMethod(): void
2323
{
24-
// Assignment should be removed
2524
$this->request->allowMethod('post');
2625

2726
// Method call without assignment should stay the same
@@ -30,32 +29,27 @@ public function testAllowMethod(): void
3029

3130
public function testConfigureLoad(): void
3231
{
33-
// Assignment should be removed
3432
Configure::load('app', 'default');
3533
}
3634

3735
public function testResponseEmitter(): void
3836
{
3937
$emitter = new ResponseEmitter();
40-
// Assignment should be removed
4138
$emitter->emit($response);
4239
}
4340

4441
public function testSessionClose(): void
4542
{
46-
// Assignment should be removed
4743
$this->session->close();
4844
}
4945

5046
public function testTableDeleteOrFail(): void
5147
{
52-
// Assignment should be removed
5348
$this->table->deleteOrFail($entity);
5449
}
5550

5651
public function testFixtureInsertTruncate(): void
5752
{
58-
// Assignments should be removed
5953
$this->fixture->insert($connection);
6054
$this->fixture->truncate($connection);
6155
}

0 commit comments

Comments
 (0)