Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions config/rector/cakephp53.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

use Cake\Upgrade\Rector\Set\CakePHPSetList;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([CakePHPSetList::CAKEPHP_53]);
};
10 changes: 10 additions & 0 deletions config/rector/sets/cakephp53.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

use Cake\Upgrade\Rector\Rector\MethodCall\EntityIsEmptyRector;
use Rector\Config\RectorConfig;

# @see https://book.cakephp.org/5/en/appendices/5-3-migration-guide.html
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(EntityIsEmptyRector::class);
};
64 changes: 64 additions & 0 deletions src/Rector/Rector/MethodCall/EntityIsEmptyRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
declare(strict_types=1);

namespace Cake\Upgrade\Rector\Rector\MethodCall;

use Cake\ORM\Entity;
use PhpParser\Node;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PHPStan\Type\ObjectType;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class EntityIsEmptyRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replace $entity->isEmpty() with !$entity->hasValue() for \Cake\ORM\Entity descendants',
[
new CodeSample(
'$entity->isEmpty();',
'!$entity->hasValue();',
),
],
);
}

public function getNodeTypes(): array
{
return [MethodCall::class];
}

public function refactor(Node $node): ?Node
{
if (!$node instanceof MethodCall) {
return null;
}

if (!$this->isName($node->name, 'isEmpty')) {
return null;
}

$objectType = $this->getType($node->var);
if (!$objectType instanceof ObjectType) {
return null;
}

if (!$objectType->isInstanceOf(Entity::class)->yes()) {
return null;
}

$newMethodCall = new MethodCall(
$node->var,
new Identifier('hasValue'),
$node->args,
);

// Replace with !$entity->hasValue($args)
return new BooleanNot($newMethodCall);
}
}
5 changes: 5 additions & 0 deletions src/Rector/Set/CakePHPSetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ final class CakePHPSetList
*/
public const CAKEPHP_52 = __DIR__ . '/../../../config/rector/sets/cakephp52.php';

/**
* @var string
*/
public const CAKEPHP_53 = __DIR__ . '/../../../config/rector/sets/cakephp53.php';

/**
* @var string
*/
Expand Down
7 changes: 7 additions & 0 deletions tests/TestCase/Command/RectorCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ public function testApply52()
$this->assertTestAppUpgraded();
}

public function testApply53()
{
$this->setupTestApp(__FUNCTION__);
$this->exec('upgrade rector --rules cakephp53 ' . TEST_APP);
$this->assertTestAppUpgraded();
}

public function testApplyMigrations45()
{
$this->setupTestApp(__FUNCTION__);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);

namespace MyPlugin;

use Cake\ORM\Entity;

class SomeTest
{
public function testRenames(): void
{
$entity = new Entity();
$result = $entity->isEmpty('test');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);

namespace MyPlugin;

use Cake\ORM\Entity;

class SomeTest
{
public function testRenames(): void
{
$entity = new Entity();
$result = !$entity->hasValue('test');
}
}
Loading