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
9 changes: 9 additions & 0 deletions config/rector/sets/cakephp60.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ConditionOperatorsToPredicatesRector::class);

$rectorConfig->ruleWithConfiguration(RenameMethodRector::class, [
new MethodCallRename('Cake\Database\Expression\QueryExpression', 'eq', 'equals'),
new MethodCallRename('Cake\Database\Expression\QueryExpression', 'notEq', 'notEquals'),
new MethodCallRename('Cake\Database\Expression\QueryExpression', 'gt', 'greaterThan'),
new MethodCallRename('Cake\Database\Expression\QueryExpression', 'gte', 'greaterThanOrEquals'),
new MethodCallRename('Cake\Database\Expression\QueryExpression', 'lt', 'lessThan'),
new MethodCallRename('Cake\Database\Expression\QueryExpression', 'lte', 'lessThanOrEquals'),
]);

// EventManager::on() signature change
$rectorConfig->rule(EventManagerOnRector::class);

Expand Down
17 changes: 16 additions & 1 deletion src/Rector/Cake5/AddPropertyTypeDeclarationRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,26 @@
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\TypeDeclaration\ValueObject\AddPropertyTypeDeclaration;

/**
* Adds property types required by CakePHP to matching declarations.
*/
final class AddPropertyTypeDeclarationRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var array<\Rector\TypeDeclaration\ValueObject\AddPropertyTypeDeclaration>
*/
private array $declarations = [];

/**
* Creates the rule with its property type mapper.
*/
public function __construct(private StaticTypeMapper $staticTypeMapper)
{
}

/**
* Describes the configured property type conversion.
*/
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Add property types required by CakePHP 5', [
Expand All @@ -39,6 +48,8 @@ public function getRuleDefinition(): RuleDefinition
}

/**
* Returns the property nodes handled by this rule.
*
* @return array<class-string<\PhpParser\Node>>
*/
public function getNodeTypes(): array
Expand All @@ -47,11 +58,13 @@ public function getNodeTypes(): array
}

/**
* Adds a configured type to an untyped, individual property.
*
* @param \PhpParser\Node\Stmt\Property $node
*/
public function refactor(Node $node): ?Node
{
if ($node->type !== null) {
if ($node->type !== null || count($node->props) !== 1) {
return null;
}

Expand Down Expand Up @@ -80,6 +93,8 @@ public function refactor(Node $node): ?Node
}

/**
* Sets the property types to add for matching classes.
*
* @param array<\Rector\TypeDeclaration\ValueObject\AddPropertyTypeDeclaration> $configuration
*/
public function configure(array $configuration): void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

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

use Cake\Database\Expression\QueryExpression;
use Cake\Database\Query\SelectQuery;

/**
* Build chained comparisons with positional and named arguments.
*/
function comparisons(QueryExpression $expression, SelectQuery $query): void
{
$expression->eq('id', 1, 'integer')
->notEq('status', 'deleted')
->gt('views', 1000)
->gte('rating', 4)
->lt('price', 50)
->lte('stock', 10);
$expression->eq(value: 2, field: 'id', type: 'integer');
$query->where(fn (QueryExpression $exp) => $exp->gt('views', 1000));
}

/**
* Migrate a nullable expression and a first-class method callable.
*/
function nullableComparison(?QueryExpression $expression): void
{
$expression?->gte('views', 1000);
$compare = $expression === null ? null : $expression->lt(...);
}

-----
<?php

declare(strict_types=1);

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

use Cake\Database\Expression\QueryExpression;
use Cake\Database\Query\SelectQuery;

/**
* Build chained comparisons with positional and named arguments.
*/
function comparisons(QueryExpression $expression, SelectQuery $query): void
{
$expression->equals('id', 1, 'integer')
->notEquals('status', 'deleted')
->greaterThan('views', 1000)
->greaterThanOrEquals('rating', 4)
->lessThan('price', 50)
->lessThanOrEquals('stock', 10);
$expression->equals(value: 2, field: 'id', type: 'integer');
$query->where(fn (QueryExpression $exp) => $exp->greaterThan('views', 1000));
}

/**
* Migrate a nullable expression and a first-class method callable.
*/
function nullableComparison(?QueryExpression $expression): void
{
$expression?->greaterThanOrEquals('views', 1000);
$compare = $expression === null ? null : $expression->lessThan(...);
}
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\QueryExpressionComparisonNames\Fixture;

/**
* Unrelated APIs keep their own short method names.
*/
class OtherExpression
{
/**
* Compare an unrelated value.
*/
public function eq(string $field, mixed $value): self
{
return $this;
}
}

/**
* Leave unrelated and untyped receivers unchanged.
*/
function unrelated(OtherExpression $expression, $unknown): void
{
$expression->eq('value', 1);
$unknown->gt('value', 2);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

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

use Cake\Database\Expression\QueryExpression;

/**
* Custom expressions retain their overrides and parent calls after migration.
*/
class CustomExpression extends QueryExpression
{
/**
* Delegate equality to the expression builder.
*/
public function eq($field, $value, ?string $type = null): static
{
return parent::eq($field, $value, $type);
}

/**
* Use inherited comparisons on the custom expression.
*/
public function published(): static
{
return $this->eq('published', true)->gt('views', 1000);
}
}

-----
<?php

declare(strict_types=1);

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

use Cake\Database\Expression\QueryExpression;

/**
* Custom expressions retain their overrides and parent calls after migration.
*/
class CustomExpression extends QueryExpression
{
/**
* Delegate equality to the expression builder.
*/
public function equals($field, $value, ?string $type = null): static
{
return parent::equals($field, $value, $type);
}

/**
* Use inherited comparisons on the custom expression.
*/
public function published(): static
{
return $this->equals('published', true)->greaterThan('views', 1000);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);

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

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

/**
* Verify the CakePHP 6 ruleset renames expression comparisons without changing operands.
*/
final class QueryExpressionComparisonNamesTest extends AbstractRectorTestCase
{
/**
* Upgrade each expression fixture through the complete CakePHP 6 ruleset.
*
* @param string $filePath Input and expected output fixture.
* @return void
*/
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

/**
* Supply supported comparison calls and unrelated-class fixtures.
*
* @return \Iterator<array{string}>
*/
public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

/**
* Use the shipped configuration so these rules are exercised as users run them.
*
* @return string
*/
public function provideConfigFilePath(): string
{
return dirname(__DIR__, 5) . '/config/rector/cakephp60.php';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,36 @@
namespace Cake\Upgrade\Test\TestCase\Rector\Property\AddPropertyTypeDeclarationRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

/**
* Tests property type declarations required by CakePHP.
*/
final class AddPropertyTypeDeclarationRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
* Tests property type conversion against a fixture.
*/
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

/**
* Provides property declaration fixtures.
*
* @return \Iterator<string[]>
*/
public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

/**
* Returns the property type rule configuration.
*/
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);

use Cake\Upgrade\Rector\Rector\Property\AddPropertyTypeDeclarationRector;
use Cake\Upgrade\Rector\Cake5\AddPropertyTypeDeclarationRector;
use PHPStan\Type\ArrayType;
use PHPStan\Type\MixedType;
use Rector\Config\RectorConfig;
Expand Down
Loading