Skip to content

Commit c0cd293

Browse files
committed
Merge branch '5.x' into 6.x
# Conflicts: # composer.json # src/Rector/Cake5/DisableHydrationToUnhydratedFindRector.php # src/Rector/Set/CakePHPSetList.php # tests/TestCase/Command/RectorCommandTest.php
2 parents d78d4bb + 36d4768 commit c0cd293

13 files changed

Lines changed: 278 additions & 1 deletion

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ cd /path/to/upgrade
9090

9191
# To apply upgrade rules from 5.2 to 5.3
9292
bin/cake upgrade rector --rules cakephp53 /path/to/your/app/src
93+
94+
# To apply upgrade rules from 5.3 to 5.4
95+
bin/cake upgrade rector --rules cakephp54 /path/to/your/app/src
9396
```
9497

9598
There are rules included for:
@@ -98,6 +101,7 @@ There are rules included for:
98101
- cakephp51
99102
- cakephp52
100103
- cakephp53
104+
- cakephp54
101105

102106
## Additional Rulesets
103107

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"php": "^8.2",
99
"cakephp/console": "^5.1.5",
1010
"nette/utils": "^4.0",
11-
"rector/rector": "~2.5.2",
11+
"rector/rector": "~2.6.1",
1212
"symfony/process": "^6.0 || ^7.0",
1313
"symfony/string": "^6.0 || ^7.0"
1414
},

config/rector/cakephp54.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\Set\CakePHPSetList;
5+
use Rector\Config\RectorConfig;
6+
7+
return static function (RectorConfig $rectorConfig): void {
8+
$rectorConfig->import(__DIR__ . '/defaults.php');
9+
$rectorConfig->sets([CakePHPSetList::CAKEPHP_54]);
10+
};

config/rector/sets/cakephp54.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Cake\Upgrade\Rector\Rector\MethodCall\DisableHydrationToUnhydratedFindRector;
5+
use Rector\Config\RectorConfig;
6+
use Rector\Renaming\Rector\Name\RenameClassRector;
7+
8+
# @see https://book.cakephp.org/5/en/appendices/5-4-migration-guide.html
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->ruleWithConfiguration(RenameClassRector::class, [
11+
'Cake\Command\Helper\BannerHelper' => 'Cake\Console\Helper\BannerHelper',
12+
'Cake\Command\Helper\ProgressHelper' => 'Cake\Console\Helper\ProgressHelper',
13+
'Cake\Command\Helper\TableHelper' => 'Cake\Console\Helper\TableHelper',
14+
'Cake\Command\Helper\TreeHelper' => 'Cake\Console\Helper\TreeHelper',
15+
]);
16+
$rectorConfig->rule(DisableHydrationToUnhydratedFindRector::class);
17+
};
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Cake\Upgrade\Rector\Rector\MethodCall;
5+
6+
use PhpParser\Node;
7+
use PhpParser\Node\Expr\MethodCall;
8+
use PhpParser\Node\Identifier;
9+
use PHPStan\Type\ObjectType;
10+
use Rector\Rector\AbstractRector;
11+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
12+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
13+
14+
/**
15+
* Transforms Table::find()->disableHydration() to Table::unhydratedFind().
16+
*
17+
* @see https://book.cakephp.org/5/en/appendices/5-4-migration-guide.html
18+
*/
19+
final class DisableHydrationToUnhydratedFindRector extends AbstractRector
20+
{
21+
public function getRuleDefinition(): RuleDefinition
22+
{
23+
return new RuleDefinition(
24+
'Change Table::find()->disableHydration() to Table::unhydratedFind()',
25+
[
26+
new CodeSample(
27+
<<<'CODE_SAMPLE'
28+
$articles->find('all')->disableHydration();
29+
CODE_SAMPLE
30+
,
31+
<<<'CODE_SAMPLE'
32+
$articles->unhydratedFind('all');
33+
CODE_SAMPLE,
34+
),
35+
],
36+
);
37+
}
38+
39+
public function getNodeTypes(): array
40+
{
41+
return [MethodCall::class];
42+
}
43+
44+
public function refactor(Node $node): ?Node
45+
{
46+
if (!$node instanceof MethodCall) {
47+
return null;
48+
}
49+
50+
if (!$node->name instanceof Identifier || $node->name->toString() !== 'disableHydration') {
51+
return null;
52+
}
53+
54+
if (count($node->args) !== 0) {
55+
return null;
56+
}
57+
58+
$current = $node->var;
59+
while ($current instanceof MethodCall) {
60+
if ($current->name instanceof Identifier && $current->name->toString() === 'find') {
61+
if (!(new ObjectType('Cake\ORM\Table'))->isSuperTypeOf($this->getType($current->var))->yes()) {
62+
return null;
63+
}
64+
65+
$current->name = new Identifier('unhydratedFind');
66+
67+
return $node->var;
68+
}
69+
70+
$current = $current->var;
71+
}
72+
73+
return null;
74+
}
75+
}

tests/TestCase/Command/RectorCommandTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ public function testApply60()
129129
$this->assertTestAppUpgraded();
130130
}
131131

132+
public function testApply54()
133+
{
134+
$this->setupTestApp(__FUNCTION__);
135+
$this->exec('upgrade rector --rules cakephp54 ' . TEST_APP);
136+
$this->assertTestAppUpgraded();
137+
}
138+
132139
public function testApplyMigrations45()
133140
{
134141
$this->setupTestApp(__FUNCTION__);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Cake\Upgrade\Test\TestCase\Rector\MethodCall\DisableHydrationToUnhydratedFindRector;
5+
6+
use Iterator;
7+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
8+
9+
final class DisableHydrationToUnhydratedFindRectorTest extends AbstractRectorTestCase
10+
{
11+
/**
12+
* @dataProvider provideData()
13+
*/
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Cake\Upgrade\Test\TestCase\Rector\MethodCall\DisableHydrationToUnhydratedFindRector\Fixture;
5+
6+
use Cake\ORM\Table;
7+
8+
class MyRepository
9+
{
10+
public function index(Table $articles)
11+
{
12+
$articles->find()->disableHydration();
13+
$articles->find('all')->disableHydration();
14+
$articles->find()->where(['id' => 1])->contain(['Users'])->disableHydration();
15+
$articles->find()->disableHydration()->toArray();
16+
}
17+
}
18+
19+
?>
20+
-----
21+
<?php
22+
declare(strict_types=1);
23+
24+
namespace Cake\Upgrade\Test\TestCase\Rector\MethodCall\DisableHydrationToUnhydratedFindRector\Fixture;
25+
26+
use Cake\ORM\Table;
27+
28+
class MyRepository
29+
{
30+
public function index(Table $articles)
31+
{
32+
$articles->unhydratedFind();
33+
$articles->unhydratedFind('all');
34+
$articles->unhydratedFind()->where(['id' => 1])->contain(['Users']);
35+
$articles->unhydratedFind()->toArray();
36+
}
37+
}
38+
39+
?>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Cake\Upgrade\Test\TestCase\Rector\MethodCall\DisableHydrationToUnhydratedFindRector\Fixture;
5+
6+
use Cake\ORM\Query\SelectQuery;
7+
use Cake\ORM\Table;
8+
9+
class DetachedQuery
10+
{
11+
public function index(Table $articles, SelectQuery $query)
12+
{
13+
// The find() call is not part of this chain, so there is nothing to rename.
14+
$query->disableHydration();
15+
16+
// Associations do not have an unhydratedFind() counterpart.
17+
$articles->getAssociation('Users')->find()->disableHydration();
18+
}
19+
}
20+
21+
?>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Cake\Upgrade\Test\TestCase\Rector\MethodCall\DisableHydrationToUnhydratedFindRector\Fixture;
5+
6+
class NotATable
7+
{
8+
public function find()
9+
{
10+
return $this;
11+
}
12+
13+
public function disableHydration()
14+
{
15+
return $this;
16+
}
17+
}
18+
19+
class MyRepository
20+
{
21+
public function index(NotATable $repository)
22+
{
23+
$repository->find()->disableHydration();
24+
}
25+
}
26+
27+
?>

0 commit comments

Comments
 (0)