From 4e3b8096767133fd354b91ac24795d696c936922 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Parafi=C5=84ski?= Date: Wed, 4 Sep 2024 15:49:22 +0200 Subject: [PATCH] IBX-8535: Added REST related rectors for 5.0 update (#9) --- src/contracts/Sets/ibexa-50.php | 29 +++++ .../RemoveArgumentFromMethodCallRector.php | 108 ++++++++++++++++++ .../Fixture/some_class.php.inc | 61 ++++++++++ ...RemoveArgumentFromMethodCallRectorTest.php | 37 ++++++ .../config/configured_rule.php | 22 ++++ .../Sets/Ibexa50/Fixture/rest_rename.php.inc | 55 +++++++++ 6 files changed, 312 insertions(+) create mode 100644 src/lib/Rule/RemoveArgumentFromMethodCallRector.php create mode 100644 tests/lib/Rule/RemoveArgumentFromMethodCallRector/Fixture/some_class.php.inc create mode 100644 tests/lib/Rule/RemoveArgumentFromMethodCallRector/RemoveArgumentFromMethodCallRectorTest.php create mode 100644 tests/lib/Rule/RemoveArgumentFromMethodCallRector/config/configured_rule.php create mode 100644 tests/lib/Sets/Ibexa50/Fixture/rest_rename.php.inc diff --git a/src/contracts/Sets/ibexa-50.php b/src/contracts/Sets/ibexa-50.php index 77ebeb0..ceccb47 100644 --- a/src/contracts/Sets/ibexa-50.php +++ b/src/contracts/Sets/ibexa-50.php @@ -8,10 +8,13 @@ namespace Ibexa\Contracts\Rector\Sets; +use Ibexa\Rector\Rule\RemoveArgumentFromMethodCallRector; use Rector\Config\RectorConfig; use Rector\Renaming\Rector\ClassConstFetch\RenameClassConstFetchRector; +use Rector\Renaming\Rector\MethodCall\RenameMethodRector; use Rector\Renaming\Rector\Name\RenameClassRector; use Rector\Renaming\Rector\PropertyFetch\RenamePropertyRector; +use Rector\Renaming\ValueObject\MethodCallRename; use Rector\Renaming\ValueObject\RenameClassConstFetch; use Rector\Renaming\ValueObject\RenameProperty; @@ -50,4 +53,30 @@ ), ] ); + + $rectorConfig->ruleWithConfiguration( + RenameMethodRector::class, + [ + new MethodCallRename( + 'Ibexa\Contracts\Rest\Output\Generator', + 'generateMediaType', + 'generateMediaTypeWithVendor' + ), + new MethodCallRename( + 'Ibexa\Rest\Output\FieldTypeSerializer', + 'serializeFieldValue', + 'serializeContentFieldValue' + ), + ] + ); + + $rectorConfig->ruleWithConfiguration( + RemoveArgumentFromMethodCallRector::class, + [ + 'class_name' => 'Ibexa\Rest\Output\FieldTypeSerializer', + 'method_name' => 'serializeContentFieldValue', + 'argument_index_to_remove' => 1, + 'more_than' => 2, + ] + ); }; diff --git a/src/lib/Rule/RemoveArgumentFromMethodCallRector.php b/src/lib/Rule/RemoveArgumentFromMethodCallRector.php new file mode 100644 index 0000000..d5eeb3c --- /dev/null +++ b/src/lib/Rule/RemoveArgumentFromMethodCallRector.php @@ -0,0 +1,108 @@ +someMethod('arg1', 'arg2', 'arg3'); +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +class SomeClass +{ + public function someMethod($a, $b, $c) {} +} + +$instance = new SomeClass(); +$instance->someMethod('arg1', 'arg2'); +CODE_SAMPLE + , + ['var_dump'] + )]); + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [MethodCall::class]; + } + + /** + * @param \PhpParser\Node\Expr\MethodCall $node + */ + public function refactor(Node $node): ?Node + { + $className = $this->resolveClassName($node); + if ($className !== $this->className) { + return null; + } + if ($this->nodeNameResolver->isName($node->name, $this->methodName) + && count($node->args) > $this->moreThan + ) { + if (isset($node->args[$this->argumentIndex])) { + unset($node->args[$this->argumentIndex]); + $node->args = array_values($node->args); + } + } + + return $node; + } + + private function resolveClassName(MethodCall $methodCall): ?string + { + $type = $this->nodeTypeResolver->getType($methodCall->var); + + if ($type instanceof ObjectType) { + return $type->getClassName(); + } + + return null; + } + + public function configure(array $configuration): void + { + $this->argumentIndex = (int)$configuration['argument_index_to_remove']; + $this->className = $configuration['class_name']; + $this->methodName = $configuration['method_name']; + + //Used to protect already renamed methods. + $this->moreThan = (int)$configuration['more_than']; + } +} diff --git a/tests/lib/Rule/RemoveArgumentFromMethodCallRector/Fixture/some_class.php.inc b/tests/lib/Rule/RemoveArgumentFromMethodCallRector/Fixture/some_class.php.inc new file mode 100644 index 0000000..e7e93a4 --- /dev/null +++ b/tests/lib/Rule/RemoveArgumentFromMethodCallRector/Fixture/some_class.php.inc @@ -0,0 +1,61 @@ +foo($a, $b, $c); + +$some->foo($a, $b); + +$someOther = new SomeOtherClass(); +$someOther->foo($a, $b, $c); +?> +----- +foo($a, $c); + +$some->foo($a, $b); + +$someOther = new SomeOtherClass(); +$someOther->foo($a, $b, $c); +?> diff --git a/tests/lib/Rule/RemoveArgumentFromMethodCallRector/RemoveArgumentFromMethodCallRectorTest.php b/tests/lib/Rule/RemoveArgumentFromMethodCallRector/RemoveArgumentFromMethodCallRectorTest.php new file mode 100644 index 0000000..62c7339 --- /dev/null +++ b/tests/lib/Rule/RemoveArgumentFromMethodCallRector/RemoveArgumentFromMethodCallRectorTest.php @@ -0,0 +1,37 @@ +doTestFile($filePath); + } + + public static function provideData(): \Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/lib/Rule/RemoveArgumentFromMethodCallRector/config/configured_rule.php b/tests/lib/Rule/RemoveArgumentFromMethodCallRector/config/configured_rule.php new file mode 100644 index 0000000..d2ffe93 --- /dev/null +++ b/tests/lib/Rule/RemoveArgumentFromMethodCallRector/config/configured_rule.php @@ -0,0 +1,22 @@ +ruleWithConfiguration( + RemoveArgumentFromMethodCallRector::class, + [ + 'class_name' => 'Ibexa\Rector\Tests\Rule\RemoveArgumentFromMethodCallRector\Fixture\SomeClass', + 'method_name' => 'foo', + 'argument_index_to_remove' => 1, + 'more_than' => 2, + ] + ); +}; diff --git a/tests/lib/Sets/Ibexa50/Fixture/rest_rename.php.inc b/tests/lib/Sets/Ibexa50/Fixture/rest_rename.php.inc new file mode 100644 index 0000000..14235ad --- /dev/null +++ b/tests/lib/Sets/Ibexa50/Fixture/rest_rename.php.inc @@ -0,0 +1,55 @@ +generateMediaType('name', 'type'); + } + + public function fieldTypeSerializer(): void + { + $serializer = new \Ibexa\Rest\Output\FieldTypeSerializer(); + + $generator = new \Ibexa\Contracts\Rest\Output\Generator(); + $contentType = new \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType(); + $field = new \Ibexa\Contracts\Core\Repository\Values\Content\Field(); + + return $serializer->serializeFieldValue($generator, $contentType, $field); + } +} + +?> +----- +generateMediaTypeWithVendor('name', 'type'); + } + + public function fieldTypeSerializer(): void + { + $serializer = new \Ibexa\Rest\Output\FieldTypeSerializer(); + + $generator = new \Ibexa\Contracts\Rest\Output\Generator(); + $contentType = new \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType(); + $field = new \Ibexa\Contracts\Core\Repository\Values\Content\Field(); + + return $serializer->serializeContentFieldValue($generator, $field); + } +} + +?>