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/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/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'; + } +} 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 @@