Skip to content

Commit f23c6b0

Browse files
committed
Add translation() => getOrCreateTranslation() rector rule for 6.x
In CakePHP 6.0, TranslateTrait::translation() becomes a pure getter that returns null if no translation exists. The old create-on-access behavior is now in getOrCreateTranslation(). This rector transforms $entity->translation('lang') calls to $entity->getOrCreateTranslation('lang') to preserve backward compatibility during the upgrade. See: cakephp/cakephp#19251
1 parent b53d7fc commit f23c6b0

7 files changed

Lines changed: 198 additions & 0 deletions

File tree

config/rector/sets/cakephp60.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\TranslationToGetOrCreateTranslationRector;
5+
use Rector\Config\RectorConfig;
6+
7+
# @see https://book.cakephp.org/6/en/appendices/6-0-migration-guide.html
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(TranslationToGetOrCreateTranslationRector::class);
10+
};
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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\MethodCall;
9+
use PhpParser\Node\Identifier;
10+
use PHPStan\Type\ObjectType;
11+
use Rector\Rector\AbstractRector;
12+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
13+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
14+
15+
/**
16+
* Renames translation() to getOrCreateTranslation() for entities using TranslateTrait.
17+
*
18+
* In CakePHP 6.0, translation() became a pure getter that returns null if no translation exists.
19+
* The old create-on-access behavior is now in getOrCreateTranslation().
20+
*
21+
* @see https://github.com/cakephp/cakephp/pull/19251
22+
*/
23+
final class TranslationToGetOrCreateTranslationRector extends AbstractRector
24+
{
25+
public function getRuleDefinition(): RuleDefinition
26+
{
27+
return new RuleDefinition(
28+
'Rename $entity->translation() to $entity->getOrCreateTranslation() to preserve create-on-access behavior',
29+
[
30+
new CodeSample(
31+
<<<'CODE_SAMPLE'
32+
$article->translation('fra');
33+
CODE_SAMPLE
34+
,
35+
<<<'CODE_SAMPLE'
36+
$article->getOrCreateTranslation('fra');
37+
CODE_SAMPLE,
38+
),
39+
],
40+
);
41+
}
42+
43+
public function getNodeTypes(): array
44+
{
45+
return [MethodCall::class];
46+
}
47+
48+
public function refactor(Node $node): ?Node
49+
{
50+
if (!$node instanceof MethodCall) {
51+
return null;
52+
}
53+
54+
if (!$this->isName($node->name, 'translation')) {
55+
return null;
56+
}
57+
58+
$objectType = $this->getType($node->var);
59+
if (!$objectType instanceof ObjectType) {
60+
return null;
61+
}
62+
63+
if (!$objectType->isInstanceOf(Entity::class)->yes()) {
64+
return null;
65+
}
66+
67+
$node->name = new Identifier('getOrCreateTranslation');
68+
69+
return $node;
70+
}
71+
}

src/Rector/Set/CakePHPSetList.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ final class CakePHPSetList
8585
*/
8686
public const CAKEPHP_53 = __DIR__ . '/../../../config/rector/sets/cakephp53.php';
8787

88+
/**
89+
* @var string
90+
*/
91+
public const CAKEPHP_60 = __DIR__ . '/../../../config/rector/sets/cakephp60.php';
92+
8893
/**
8994
* @var string
9095
*/
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Cake\Upgrade\Test\TestCase\Rector\MethodCall\TranslationToGetOrCreateTranslationRector\Fixture;
4+
5+
use Cake\ORM\Entity;
6+
7+
class Article extends Entity
8+
{
9+
}
10+
11+
class ArticlesController
12+
{
13+
public function edit()
14+
{
15+
$article = new Article();
16+
17+
// Should transform: Entity->translation()
18+
$translation = $article->translation('fra');
19+
20+
return $article;
21+
}
22+
}
23+
24+
?>
25+
-----
26+
<?php
27+
28+
namespace Cake\Upgrade\Test\TestCase\Rector\MethodCall\TranslationToGetOrCreateTranslationRector\Fixture;
29+
30+
use Cake\ORM\Entity;
31+
32+
class Article extends Entity
33+
{
34+
}
35+
36+
class ArticlesController
37+
{
38+
public function edit()
39+
{
40+
$article = new Article();
41+
42+
// Should transform: Entity->translation()
43+
$translation = $article->getOrCreateTranslation('fra');
44+
45+
return $article;
46+
}
47+
}
48+
49+
?>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Cake\Upgrade\Test\TestCase\Rector\MethodCall\TranslationToGetOrCreateTranslationRector\Fixture;
4+
5+
class SomeTranslationService
6+
{
7+
public function translation(string $lang): ?string
8+
{
9+
return null;
10+
}
11+
}
12+
13+
class SomeController
14+
{
15+
public function index()
16+
{
17+
$service = new SomeTranslationService();
18+
19+
// Should NOT transform: not an Entity subclass
20+
$result = $service->translation('fra');
21+
22+
return $result;
23+
}
24+
}
25+
26+
?>
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\TranslationToGetOrCreateTranslationRector;
5+
6+
use Iterator;
7+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
8+
9+
final class TranslationToGetOrCreateTranslationRectorTest 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: 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\Rector\MethodCall\TranslationToGetOrCreateTranslationRector;
5+
use Rector\Config\RectorConfig;
6+
7+
return static function (RectorConfig $rectorConfig): void {
8+
$rectorConfig->rule(TranslationToGetOrCreateTranslationRector::class);
9+
};

0 commit comments

Comments
 (0)