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
10 changes: 4 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: ['8.1']
php-version: ['8.2']
prefer-lowest: ['']
include:
- php-version: '8.1'
- php-version: '8.2'
prefer-lowest: 'prefer-lowest'

steps:
Expand Down Expand Up @@ -51,14 +51,12 @@ jobs:
run: |
if ${{ matrix.prefer-lowest == 'prefer-lowest' }}; then
make install-dev-lowest
elif ${{ matrix.php-version == '8.1' }}; then
make install-dev-ignore-reqs
else
make install-dev
fi

- name: Setup problem matchers for PHPUnit
if: matrix.php-version == '8.1'
if: matrix.php-version == '8.2'
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Run PHPUnit
Expand All @@ -74,7 +72,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
php-version: '8.2'
extensions: mbstring, intl
tools: cs2pr
coverage: none
Expand Down
2 changes: 2 additions & 0 deletions config/rector/sets/cakephp53.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Cake\Upgrade\Rector\Rector\MethodCall\EntityPatchRector;
use Cake\Upgrade\Rector\Rector\MethodCall\NewExprToFuncRector;
use Cake\Upgrade\Rector\Rector\MethodCall\QueryParamAccessRector;
use Cake\Upgrade\Rector\Rector\MethodCall\TypeFactoryGetMappedRector;
use Rector\Config\RectorConfig;
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Rector\Renaming\Rector\Name\RenameClassRector;
Expand All @@ -28,4 +29,5 @@
$rectorConfig->rule(EntityPatchRector::class);
$rectorConfig->rule(FormExecuteToProcessRector::class);
$rectorConfig->rule(QueryParamAccessRector::class);
$rectorConfig->rule(TypeFactoryGetMappedRector::class);
};
82 changes: 82 additions & 0 deletions src/Rector/Rector/MethodCall/TypeFactoryGetMappedRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);

namespace Cake\Upgrade\Rector\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* Transforms TypeFactory::getMap($type) to TypeFactory::getMapped($type)
*
* In CakePHP 5.3, calling getMap() with a type argument is deprecated.
* Use getMapped() instead for single-type lookups.
*
* @see https://book.cakephp.org/5/en/appendices/5-3-migration-guide.html
*/
final class TypeFactoryGetMappedRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change TypeFactory::getMap($type) to TypeFactory::getMapped($type)',
[
new CodeSample(
<<<'CODE_SAMPLE'
use Cake\Database\TypeFactory;

$class = TypeFactory::getMap('datetime');
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Cake\Database\TypeFactory;

$class = TypeFactory::getMapped('datetime');
CODE_SAMPLE,
),
],
);
}

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

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

// Must be getMap method
if (!$node->name instanceof Identifier || $node->name->toString() !== 'getMap') {
return null;
}

// Must have at least one argument (the type)
if (count($node->args) < 1) {
return null;
}

// Check if this is called on TypeFactory
if (!$node->class instanceof Name) {
return null;
}

$className = $node->class->toString();
if ($className !== 'Cake\Database\TypeFactory' && $className !== 'TypeFactory') {
return null;
}

// Rename to getMapped
$node->name = new Identifier('getMapped');

return $node;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

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

use Cake\Database\TypeFactory;

class SomeClass
{
public function someMethod()
{
// Should transform: getMap with type argument
$class = TypeFactory::getMap('datetime');

// Should also transform with variable
$type = 'string';
$class = TypeFactory::getMap($type);

return $class;
}
}

?>
-----
<?php

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

use Cake\Database\TypeFactory;

class SomeClass
{
public function someMethod()
{
// Should transform: getMap with type argument
$class = TypeFactory::getMapped('datetime');

// Should also transform with variable
$type = 'string';
$class = TypeFactory::getMapped($type);

return $class;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

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

use Cake\Database\TypeFactory;

class SomeClass
{
public function someMethod()
{
// Should NOT transform: getMap without argument (returns full map)
$map = TypeFactory::getMap();

return $map;
}
}

?>
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\TypeFactoryGetMappedRector;

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

final class TypeFactoryGetMappedRectorTest 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,9 @@
<?php
declare(strict_types=1);

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

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(TypeFactoryGetMappedRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace MyPlugin;

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

$table = $this->fetchTable('Articles');
$expr = $table->find()->newExpr();

// TypeFactory::getMap($type) should be changed to getMapped($type)
$class = TypeFactory::getMap('datetime');
$allTypes = TypeFactory::getMap(); // This should stay as is
}

public function findSomething(Query $query, array $options): Query {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace MyPlugin;

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

$table = $this->fetchTable('Articles');
$expr = $table->find()->expr();

// TypeFactory::getMap($type) should be changed to getMapped($type)
$class = TypeFactory::getMapped('datetime');
$allTypes = TypeFactory::getMap(); // This should stay as is
}

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