Skip to content

Commit a5e93c3

Browse files
committed
#316 add custom rector to change ->isEmpty() to statusentity->hasValue()
#316 add custom rector to change $entity->isEmpty() to !$entity->hasValue()
1 parent 448f3da commit a5e93c3

File tree

7 files changed

+125
-0
lines changed

7 files changed

+125
-0
lines changed

config/rector/cakephp53.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Cake\Upgrade\Rector\Set\CakePHPSetList;
5+
use Rector\Config\RectorConfig;
6+
7+
return static function (RectorConfig $rectorConfig): void {
8+
$rectorConfig->sets([CakePHPSetList::CAKEPHP_53]);
9+
};

config/rector/sets/cakephp53.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Cake\Upgrade\Rector\Rector\MethodCall\EntityIsEmptyRector;
5+
use Rector\Config\RectorConfig;
6+
7+
# @see https://book.cakephp.org/5/en/appendices/5-3-migration-guide.html
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(EntityIsEmptyRector::class);
10+
};
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Cake\Upgrade\Rector\Rector\MethodCall;
5+
6+
use Cake\ORM\Entity;
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\BooleanNot;
9+
use PhpParser\Node\Expr\MethodCall;
10+
use PhpParser\Node\Identifier;
11+
use PHPStan\Type\ObjectType;
12+
use Rector\Rector\AbstractRector;
13+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
14+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
15+
16+
final class EntityIsEmptyRector extends AbstractRector
17+
{
18+
public function getRuleDefinition(): RuleDefinition
19+
{
20+
return new RuleDefinition(
21+
'Replace $entity->isEmpty() with !$entity->hasValue() for \Cake\ORM\Entity descendants',
22+
[
23+
new CodeSample(
24+
'$entity->isEmpty();',
25+
'!$entity->hasValue();',
26+
),
27+
],
28+
);
29+
}
30+
31+
public function getNodeTypes(): array
32+
{
33+
return [MethodCall::class];
34+
}
35+
36+
public function refactor(Node $node): ?Node
37+
{
38+
if (!$node instanceof MethodCall) {
39+
return null;
40+
}
41+
42+
if (!$this->isName($node->name, 'isEmpty')) {
43+
return null;
44+
}
45+
46+
$objectType = $this->getType($node->var);
47+
if (!$objectType instanceof ObjectType) {
48+
return null;
49+
}
50+
51+
if (!$objectType->isInstanceOf(Entity::class)->yes()) {
52+
return null;
53+
}
54+
55+
$newMethodCall = new MethodCall(
56+
$node->var,
57+
new Identifier('hasValue'),
58+
$node->args,
59+
);
60+
61+
// Replace with !$entity->hasValue($args)
62+
return new BooleanNot($newMethodCall);
63+
}
64+
}

src/Rector/Set/CakePHPSetList.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ final class CakePHPSetList
8080
*/
8181
public const CAKEPHP_52 = __DIR__ . '/../../../config/rector/sets/cakephp52.php';
8282

83+
/**
84+
* @var string
85+
*/
86+
public const CAKEPHP_53 = __DIR__ . '/../../../config/rector/sets/cakephp53.php';
87+
8388
/**
8489
* @var string
8590
*/

tests/TestCase/Command/RectorCommandTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ public function testApply52()
106106
$this->assertTestAppUpgraded();
107107
}
108108

109+
public function testApply53()
110+
{
111+
$this->setupTestApp(__FUNCTION__);
112+
$this->exec('upgrade rector --rules cakephp53 ' . TEST_APP);
113+
$this->assertTestAppUpgraded();
114+
}
115+
109116
public function testApplyMigrations45()
110117
{
111118
$this->setupTestApp(__FUNCTION__);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MyPlugin;
5+
6+
use Cake\ORM\Entity;
7+
8+
class SomeTest
9+
{
10+
public function testRenames(): void
11+
{
12+
$entity = new Entity();
13+
$result = $entity->isEmpty('test');
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MyPlugin;
5+
6+
use Cake\ORM\Entity;
7+
8+
class SomeTest
9+
{
10+
public function testRenames(): void
11+
{
12+
$entity = new Entity();
13+
$result = !$entity->hasValue('test');
14+
}
15+
}

0 commit comments

Comments
 (0)