Skip to content

Commit ea8b464

Browse files
authored
Add TypeFactory::getMap($type) => getMapped($type) rector rule (#382)
* Add TypeFactory::getMap($type) => getMapped($type) rector rule In CakePHP 5.3, calling TypeFactory::getMap() with a type argument is deprecated. Use getMapped() instead for single-type lookups. Fixes #354 * Update CI to use PHP 8.2 (required by CakePHP 5.x)
1 parent 5fbb739 commit ea8b464

File tree

9 files changed

+198
-6
lines changed

9 files changed

+198
-6
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ jobs:
1717
strategy:
1818
fail-fast: false
1919
matrix:
20-
php-version: ['8.1']
20+
php-version: ['8.2']
2121
prefer-lowest: ['']
2222
include:
23-
- php-version: '8.1'
23+
- php-version: '8.2'
2424
prefer-lowest: 'prefer-lowest'
2525

2626
steps:
@@ -51,14 +51,12 @@ jobs:
5151
run: |
5252
if ${{ matrix.prefer-lowest == 'prefer-lowest' }}; then
5353
make install-dev-lowest
54-
elif ${{ matrix.php-version == '8.1' }}; then
55-
make install-dev-ignore-reqs
5654
else
5755
make install-dev
5856
fi
5957
6058
- name: Setup problem matchers for PHPUnit
61-
if: matrix.php-version == '8.1'
59+
if: matrix.php-version == '8.2'
6260
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
6361

6462
- name: Run PHPUnit
@@ -74,7 +72,7 @@ jobs:
7472
- name: Setup PHP
7573
uses: shivammathur/setup-php@v2
7674
with:
77-
php-version: '8.1'
75+
php-version: '8.2'
7876
extensions: mbstring, intl
7977
tools: cs2pr
8078
coverage: none

config/rector/sets/cakephp53.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Cake\Upgrade\Rector\Rector\MethodCall\EntityPatchRector;
77
use Cake\Upgrade\Rector\Rector\MethodCall\NewExprToFuncRector;
88
use Cake\Upgrade\Rector\Rector\MethodCall\QueryParamAccessRector;
9+
use Cake\Upgrade\Rector\Rector\MethodCall\TypeFactoryGetMappedRector;
910
use Rector\Config\RectorConfig;
1011
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
1112
use Rector\Renaming\Rector\Name\RenameClassRector;
@@ -28,4 +29,5 @@
2829
$rectorConfig->rule(EntityPatchRector::class);
2930
$rectorConfig->rule(FormExecuteToProcessRector::class);
3031
$rectorConfig->rule(QueryParamAccessRector::class);
32+
$rectorConfig->rule(TypeFactoryGetMappedRector::class);
3133
};
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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\StaticCall;
8+
use PhpParser\Node\Identifier;
9+
use PhpParser\Node\Name;
10+
use Rector\Rector\AbstractRector;
11+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
12+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
13+
14+
/**
15+
* Transforms TypeFactory::getMap($type) to TypeFactory::getMapped($type)
16+
*
17+
* In CakePHP 5.3, calling getMap() with a type argument is deprecated.
18+
* Use getMapped() instead for single-type lookups.
19+
*
20+
* @see https://book.cakephp.org/5/en/appendices/5-3-migration-guide.html
21+
*/
22+
final class TypeFactoryGetMappedRector extends AbstractRector
23+
{
24+
public function getRuleDefinition(): RuleDefinition
25+
{
26+
return new RuleDefinition(
27+
'Change TypeFactory::getMap($type) to TypeFactory::getMapped($type)',
28+
[
29+
new CodeSample(
30+
<<<'CODE_SAMPLE'
31+
use Cake\Database\TypeFactory;
32+
33+
$class = TypeFactory::getMap('datetime');
34+
CODE_SAMPLE
35+
,
36+
<<<'CODE_SAMPLE'
37+
use Cake\Database\TypeFactory;
38+
39+
$class = TypeFactory::getMapped('datetime');
40+
CODE_SAMPLE,
41+
),
42+
],
43+
);
44+
}
45+
46+
public function getNodeTypes(): array
47+
{
48+
return [StaticCall::class];
49+
}
50+
51+
public function refactor(Node $node): ?Node
52+
{
53+
if (!$node instanceof StaticCall) {
54+
return null;
55+
}
56+
57+
// Must be getMap method
58+
if (!$node->name instanceof Identifier || $node->name->toString() !== 'getMap') {
59+
return null;
60+
}
61+
62+
// Must have at least one argument (the type)
63+
if (count($node->args) < 1) {
64+
return null;
65+
}
66+
67+
// Check if this is called on TypeFactory
68+
if (!$node->class instanceof Name) {
69+
return null;
70+
}
71+
72+
$className = $node->class->toString();
73+
if ($className !== 'Cake\Database\TypeFactory' && $className !== 'TypeFactory') {
74+
return null;
75+
}
76+
77+
// Rename to getMapped
78+
$node->name = new Identifier('getMapped');
79+
80+
return $node;
81+
}
82+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Cake\Upgrade\Test\TestCase\Rector\MethodCall\TypeFactoryGetMappedRector\Fixture;
4+
5+
use Cake\Database\TypeFactory;
6+
7+
class SomeClass
8+
{
9+
public function someMethod()
10+
{
11+
// Should transform: getMap with type argument
12+
$class = TypeFactory::getMap('datetime');
13+
14+
// Should also transform with variable
15+
$type = 'string';
16+
$class = TypeFactory::getMap($type);
17+
18+
return $class;
19+
}
20+
}
21+
22+
?>
23+
-----
24+
<?php
25+
26+
namespace Cake\Upgrade\Test\TestCase\Rector\MethodCall\TypeFactoryGetMappedRector\Fixture;
27+
28+
use Cake\Database\TypeFactory;
29+
30+
class SomeClass
31+
{
32+
public function someMethod()
33+
{
34+
// Should transform: getMap with type argument
35+
$class = TypeFactory::getMapped('datetime');
36+
37+
// Should also transform with variable
38+
$type = 'string';
39+
$class = TypeFactory::getMapped($type);
40+
41+
return $class;
42+
}
43+
}
44+
45+
?>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Cake\Upgrade\Test\TestCase\Rector\MethodCall\TypeFactoryGetMappedRector\Fixture;
4+
5+
use Cake\Database\TypeFactory;
6+
7+
class SomeClass
8+
{
9+
public function someMethod()
10+
{
11+
// Should NOT transform: getMap without argument (returns full map)
12+
$map = TypeFactory::getMap();
13+
14+
return $map;
15+
}
16+
}
17+
18+
?>
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\TypeFactoryGetMappedRector;
5+
6+
use Iterator;
7+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
8+
9+
final class TypeFactoryGetMappedRectorTest 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\TypeFactoryGetMappedRector;
5+
use Rector\Config\RectorConfig;
6+
7+
return static function (RectorConfig $rectorConfig): void {
8+
$rectorConfig->rule(TypeFactoryGetMappedRector::class);
9+
};

tests/test_apps/original/RectorCommand-testApply53/src/SomeTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace MyPlugin;
55

6+
use Cake\Database\TypeFactory;
67
use Cake\ORM\Entity;
78
use Cake\ORM\Locator\LocatorAwareTrait;
89
use Cake\ORM\Query;
@@ -22,6 +23,10 @@ public function testRenames(): void
2223

2324
$table = $this->fetchTable('Articles');
2425
$expr = $table->find()->newExpr();
26+
27+
// TypeFactory::getMap($type) should be changed to getMapped($type)
28+
$class = TypeFactory::getMap('datetime');
29+
$allTypes = TypeFactory::getMap(); // This should stay as is
2530
}
2631

2732
public function findSomething(Query $query, array $options): Query {

tests/test_apps/upgraded/RectorCommand-testApply53/src/SomeTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace MyPlugin;
55

6+
use Cake\Database\TypeFactory;
67
use Cake\ORM\Entity;
78
use Cake\ORM\Locator\LocatorAwareTrait;
89
use Cake\ORM\Query;
@@ -22,6 +23,10 @@ public function testRenames(): void
2223

2324
$table = $this->fetchTable('Articles');
2425
$expr = $table->find()->expr();
26+
27+
// TypeFactory::getMap($type) should be changed to getMapped($type)
28+
$class = TypeFactory::getMapped('datetime');
29+
$allTypes = TypeFactory::getMap(); // This should stay as is
2530
}
2631

2732
public function findSomething(\Cake\ORM\Query\SelectQuery $query, array $options): \Cake\ORM\Query\SelectQuery {

0 commit comments

Comments
 (0)