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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ cd /path/to/upgrade

# To apply upgrade rules from 5.2 to 5.3
bin/cake upgrade rector --rules cakephp53 /path/to/your/app/src

# To apply upgrade rules from 5.3 to 5.4
bin/cake upgrade rector --rules cakephp54 /path/to/your/app/src
```

There are rules included for:
Expand All @@ -98,6 +101,7 @@ There are rules included for:
- cakephp51
- cakephp52
- cakephp53
- cakephp54

## Additional Rulesets

Expand Down
10 changes: 10 additions & 0 deletions config/rector/cakephp54.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

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

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

use Cake\Upgrade\Rector\Rector\MethodCall\DisableHydrationToUnhydratedFindRector;
use Rector\Config\RectorConfig;
use Rector\Renaming\Rector\Name\RenameClassRector;

# @see https://book.cakephp.org/5/en/appendices/5-4-migration-guide.html
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(RenameClassRector::class, [
'Cake\Command\Helper\BannerHelper' => 'Cake\Console\Helper\BannerHelper',
'Cake\Command\Helper\ProgressHelper' => 'Cake\Console\Helper\ProgressHelper',
'Cake\Command\Helper\TableHelper' => 'Cake\Console\Helper\TableHelper',
'Cake\Command\Helper\TreeHelper' => 'Cake\Console\Helper\TreeHelper',
]);
$rectorConfig->rule(DisableHydrationToUnhydratedFindRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
declare(strict_types=1);

namespace Cake\Upgrade\Rector\Rector\MethodCall;

use PhpParser\Node;
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;

/**
* Transforms Table::find()->disableHydration() to Table::unhydratedFind().
*
* @see https://book.cakephp.org/5/en/appendices/5-4-migration-guide.html
*/
final class DisableHydrationToUnhydratedFindRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change Table::find()->disableHydration() to Table::unhydratedFind()',
[
new CodeSample(
<<<'CODE_SAMPLE'
$articles->find('all')->disableHydration();
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$articles->unhydratedFind('all');
CODE_SAMPLE,
),
],
);
}

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

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

if (!$node->name instanceof Identifier || $node->name->toString() !== 'disableHydration') {
return null;
}

if (count($node->args) !== 0) {
return null;
}

$current = $node->var;
while ($current instanceof MethodCall) {
if ($current->name instanceof Identifier && $current->name->toString() === 'find') {
if (!(new ObjectType('Cake\ORM\Table'))->isSuperTypeOf($this->getType($current->var))->yes()) {
return null;
}

$current->name = new Identifier('unhydratedFind');

return $node->var;
}

$current = $current->var;
}

return null;
}
}
5 changes: 5 additions & 0 deletions src/Rector/Set/CakePHPSetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ final class CakePHPSetList
*/
public const CAKEPHP_53 = __DIR__ . '/../../../config/rector/sets/cakephp53.php';

/**
* @var string
*/
public const CAKEPHP_54 = __DIR__ . '/../../../config/rector/sets/cakephp54.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 @@ -123,6 +123,13 @@ public function testApply53()
$this->assertTestAppUpgraded();
}

public function testApply54()
{
$this->setupTestApp(__FUNCTION__);
$this->exec('upgrade rector --rules cakephp54 ' . 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,28 @@
<?php
declare(strict_types=1);

namespace Cake\Upgrade\Test\TestCase\Rector\MethodCall\DisableHydrationToUnhydratedFindRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class DisableHydrationToUnhydratedFindRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);

namespace Cake\Upgrade\Test\TestCase\Rector\MethodCall\DisableHydrationToUnhydratedFindRector\Fixture;

use Cake\ORM\Table;

class MyRepository
{
public function index(Table $articles)
{
$articles->find()->disableHydration();
$articles->find('all')->disableHydration();
$articles->find()->where(['id' => 1])->contain(['Users'])->disableHydration();
$articles->find()->disableHydration()->toArray();
}
}

?>
-----
<?php
declare(strict_types=1);

namespace Cake\Upgrade\Test\TestCase\Rector\MethodCall\DisableHydrationToUnhydratedFindRector\Fixture;

use Cake\ORM\Table;

class MyRepository
{
public function index(Table $articles)
{
$articles->unhydratedFind();
$articles->unhydratedFind('all');
$articles->unhydratedFind()->where(['id' => 1])->contain(['Users']);
$articles->unhydratedFind()->toArray();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);

namespace Cake\Upgrade\Test\TestCase\Rector\MethodCall\DisableHydrationToUnhydratedFindRector\Fixture;

use Cake\ORM\Query\SelectQuery;
use Cake\ORM\Table;

class DetachedQuery
{
public function index(Table $articles, SelectQuery $query)
{
// The find() call is not part of this chain, so there is nothing to rename.
$query->disableHydration();

// Associations do not have an unhydratedFind() counterpart.
$articles->getAssociation('Users')->find()->disableHydration();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);

namespace Cake\Upgrade\Test\TestCase\Rector\MethodCall\DisableHydrationToUnhydratedFindRector\Fixture;

class NotATable
{
public function find()
{
return $this;
}

public function disableHydration()
{
return $this;
}
}

class MyRepository
{
public function index(NotATable $repository)
{
$repository->find()->disableHydration();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

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

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(DisableHydrationToUnhydratedFindRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);

namespace MyPlugin;

use Cake\Command\Helper\TableHelper;
use Cake\ORM\Locator\LocatorAwareTrait;

class SomeTest
{
use LocatorAwareTrait;

private TableHelper $Table;

public function testRenames(): void
{
$table = $this->fetchTable('Articles');
$results = $table->find()->where(['published' => true])->disableHydration();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);

namespace MyPlugin;

use Cake\Command\Helper\TableHelper;
use Cake\ORM\Locator\LocatorAwareTrait;

class SomeTest
{
use LocatorAwareTrait;

private \Cake\Console\Helper\TableHelper $Table;

public function testRenames(): void
{
$table = $this->fetchTable('Articles');
$results = $table->unhydratedFind()->where(['published' => true]);
}
}