From b02832141d58a27362a97490b06eb9685ce9f364 Mon Sep 17 00:00:00 2001 From: Jasper Smet Date: Mon, 9 Nov 2026 20:25:33 +0100 Subject: [PATCH 1/2] Rename QueryExpression comparisons in CakePHP 6 upgrade rules --- README.md | 24 +++++++ config/rector/sets/cakephp60.php | 9 +++ .../Fixture/comparisons.php.inc | 66 +++++++++++++++++++ .../Fixture/skip_unrelated.php.inc | 28 ++++++++ .../Fixture/subclass.php.inc | 60 +++++++++++++++++ .../QueryExpressionComparisonNamesTest.php | 46 +++++++++++++ 6 files changed, 233 insertions(+) create mode 100644 tests/TestCase/Rector/MethodCall/QueryExpressionComparisonNames/Fixture/comparisons.php.inc create mode 100644 tests/TestCase/Rector/MethodCall/QueryExpressionComparisonNames/Fixture/skip_unrelated.php.inc create mode 100644 tests/TestCase/Rector/MethodCall/QueryExpressionComparisonNames/Fixture/subclass.php.inc create mode 100644 tests/TestCase/Rector/MethodCall/QueryExpressionComparisonNames/QueryExpressionComparisonNamesTest.php diff --git a/README.md b/README.md index 9f88fbd..7d2fc35 100644 --- a/README.md +++ b/README.md @@ -185,3 +185,27 @@ Review dynamically constructed keys, shared condition/configuration arrays, arrays assembled incrementally, and custom condition consumers manually. The rule leaves ambiguous arrays unchanged rather than changing unrelated application data. Run the tool before upgrading application dependencies to CakePHP 6. + +### CakePHP 6 expression comparison names + +The `cakephp60` ruleset also renames the fluent `QueryExpression` comparison +methods to match the predicate functions: + +| Before | After | +| --- | --- | +| `eq()` | `equals()` | +| `notEq()` | `notEquals()` | +| `gt()` | `greaterThan()` | +| `gte()` | `greaterThanOrEquals()` | +| `lt()` | `lessThan()` | +| `lte()` | `lessThanOrEquals()` | + +```bash +bin/cake upgrade rector --rules cakephp60 /path/to/your/app/src +bin/cake upgrade rector --rules cakephp60 /path/to/your/app/tests +``` + +Calls keep their arguments, binding types, and chaining. The rules apply to typed +`QueryExpression` receivers and subclasses, including overrides and parent calls. +Unrelated classes and receivers whose type cannot be determined are left unchanged. +Run the migration while the old CakePHP dependency is still installed. diff --git a/config/rector/sets/cakephp60.php b/config/rector/sets/cakephp60.php index a1717c5..c1e2feb 100644 --- a/config/rector/sets/cakephp60.php +++ b/config/rector/sets/cakephp60.php @@ -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); diff --git a/tests/TestCase/Rector/MethodCall/QueryExpressionComparisonNames/Fixture/comparisons.php.inc b/tests/TestCase/Rector/MethodCall/QueryExpressionComparisonNames/Fixture/comparisons.php.inc new file mode 100644 index 0000000..5b604f7 --- /dev/null +++ b/tests/TestCase/Rector/MethodCall/QueryExpressionComparisonNames/Fixture/comparisons.php.inc @@ -0,0 +1,66 @@ +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(...); +} + +----- +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(...); +} diff --git a/tests/TestCase/Rector/MethodCall/QueryExpressionComparisonNames/Fixture/skip_unrelated.php.inc b/tests/TestCase/Rector/MethodCall/QueryExpressionComparisonNames/Fixture/skip_unrelated.php.inc new file mode 100644 index 0000000..a2f6acc --- /dev/null +++ b/tests/TestCase/Rector/MethodCall/QueryExpressionComparisonNames/Fixture/skip_unrelated.php.inc @@ -0,0 +1,28 @@ +eq('value', 1); + $unknown->gt('value', 2); +} diff --git a/tests/TestCase/Rector/MethodCall/QueryExpressionComparisonNames/Fixture/subclass.php.inc b/tests/TestCase/Rector/MethodCall/QueryExpressionComparisonNames/Fixture/subclass.php.inc new file mode 100644 index 0000000..03135e0 --- /dev/null +++ b/tests/TestCase/Rector/MethodCall/QueryExpressionComparisonNames/Fixture/subclass.php.inc @@ -0,0 +1,60 @@ +eq('published', true)->gt('views', 1000); + } +} + +----- +equals('published', true)->greaterThan('views', 1000); + } +} diff --git a/tests/TestCase/Rector/MethodCall/QueryExpressionComparisonNames/QueryExpressionComparisonNamesTest.php b/tests/TestCase/Rector/MethodCall/QueryExpressionComparisonNames/QueryExpressionComparisonNamesTest.php new file mode 100644 index 0000000..7cab219 --- /dev/null +++ b/tests/TestCase/Rector/MethodCall/QueryExpressionComparisonNames/QueryExpressionComparisonNamesTest.php @@ -0,0 +1,46 @@ +doTestFile($filePath); + } + + /** + * Supply supported comparison calls and unrelated-class fixtures. + * + * @return \Iterator + */ + 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'; + } +} From 18dc937a9b71c54b255fa0e3ec2f90044582f245 Mon Sep 17 00:00:00 2001 From: Jasper Smet Date: Mon, 9 Nov 2026 20:32:08 +0100 Subject: [PATCH 2/2] Fix property rule tests and remove comparison README entry --- README.md | 24 ------------------- .../AddPropertyTypeDeclarationRector.php | 17 ++++++++++++- .../AddPropertyTypeDeclarationRectorTest.php | 15 +++++++++++- .../config/configured_rule.php | 2 +- 4 files changed, 31 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 7d2fc35..9f88fbd 100644 --- a/README.md +++ b/README.md @@ -185,27 +185,3 @@ Review dynamically constructed keys, shared condition/configuration arrays, arrays assembled incrementally, and custom condition consumers manually. The rule leaves ambiguous arrays unchanged rather than changing unrelated application data. Run the tool before upgrading application dependencies to CakePHP 6. - -### CakePHP 6 expression comparison names - -The `cakephp60` ruleset also renames the fluent `QueryExpression` comparison -methods to match the predicate functions: - -| Before | After | -| --- | --- | -| `eq()` | `equals()` | -| `notEq()` | `notEquals()` | -| `gt()` | `greaterThan()` | -| `gte()` | `greaterThanOrEquals()` | -| `lt()` | `lessThan()` | -| `lte()` | `lessThanOrEquals()` | - -```bash -bin/cake upgrade rector --rules cakephp60 /path/to/your/app/src -bin/cake upgrade rector --rules cakephp60 /path/to/your/app/tests -``` - -Calls keep their arguments, binding types, and chaining. The rules apply to typed -`QueryExpression` receivers and subclasses, including overrides and parent calls. -Unrelated classes and receivers whose type cannot be determined are left unchanged. -Run the migration while the old CakePHP dependency is still installed. diff --git a/src/Rector/Cake5/AddPropertyTypeDeclarationRector.php b/src/Rector/Cake5/AddPropertyTypeDeclarationRector.php index 9d236ca..3d0feed 100644 --- a/src/Rector/Cake5/AddPropertyTypeDeclarationRector.php +++ b/src/Rector/Cake5/AddPropertyTypeDeclarationRector.php @@ -16,6 +16,9 @@ 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 { /** @@ -23,10 +26,16 @@ final class AddPropertyTypeDeclarationRector extends AbstractRector implements C */ 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', [ @@ -39,6 +48,8 @@ public function getRuleDefinition(): RuleDefinition } /** + * Returns the property nodes handled by this rule. + * * @return array> */ public function getNodeTypes(): array @@ -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; } @@ -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 diff --git a/tests/TestCase/Rector/Property/AddPropertyTypeDeclarationRector/AddPropertyTypeDeclarationRectorTest.php b/tests/TestCase/Rector/Property/AddPropertyTypeDeclarationRector/AddPropertyTypeDeclarationRectorTest.php index 342bb10..cabf397 100644 --- a/tests/TestCase/Rector/Property/AddPropertyTypeDeclarationRector/AddPropertyTypeDeclarationRectorTest.php +++ b/tests/TestCase/Rector/Property/AddPropertyTypeDeclarationRector/AddPropertyTypeDeclarationRectorTest.php @@ -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 + */ 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'; diff --git a/tests/TestCase/Rector/Property/AddPropertyTypeDeclarationRector/config/configured_rule.php b/tests/TestCase/Rector/Property/AddPropertyTypeDeclarationRector/config/configured_rule.php index 45a1e5b..fcab7ab 100644 --- a/tests/TestCase/Rector/Property/AddPropertyTypeDeclarationRector/config/configured_rule.php +++ b/tests/TestCase/Rector/Property/AddPropertyTypeDeclarationRector/config/configured_rule.php @@ -1,7 +1,7 @@