Skip to content

Commit 6fe997f

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

6 files changed

Lines changed: 112 additions & 0 deletions

File tree

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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
// Replace with !$entity->hasValue()
56+
return new BooleanNot(new MethodCall($node->var, new Identifier('hasValue')));
57+
}
58+
}

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
*/
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)