diff --git a/src/contracts/Sets/ibexa-50.php b/src/contracts/Sets/ibexa-50.php index 5e1e015..fd2c335 100644 --- a/src/contracts/Sets/ibexa-50.php +++ b/src/contracts/Sets/ibexa-50.php @@ -8,6 +8,7 @@ namespace Ibexa\Contracts\Rector\Sets; +use Ibexa\Rector\Rule\PropertyToGetterRector; use Ibexa\Rector\Rule\RemoveArgumentFromMethodCallRector; use Rector\Config\RectorConfig; use Rector\Renaming\Rector\ClassConstFetch\RenameClassConstFetchRector; @@ -98,4 +99,19 @@ ), ] ); + + $rectorConfig->ruleWithConfiguration( + PropertyToGetterRector::class, + [ + 'Ibexa\Core\MVC\Symfony\Routing\SimplifiedRequest' => [ + 'scheme' => 'getScheme', + 'host' => 'getHost', + 'port' => 'getPort', + 'pathinfo' => 'getPathInfo', + 'queryParams' => 'getQueryParams', + 'languages' => 'getLanguages', + 'headers' => 'getHeaders', + ], + ] + ); }; diff --git a/src/lib/Rule/PropertyToGetterRector.php b/src/lib/Rule/PropertyToGetterRector.php new file mode 100644 index 0000000..f27bd0c --- /dev/null +++ b/src/lib/Rule/PropertyToGetterRector.php @@ -0,0 +1,92 @@ +> */ + private array $classPropertyToGetterMap = []; + + /** + * @throws \Exception + */ + public function getRuleDefinition(): RuleDefinition + { + return new RuleDefinition('Change direct property access to getter method for specified classes and properties', [new ConfiguredCodeSample( + <<<'CODE_SAMPLE' + class SomeClass + { + private $property; + public function getProperty() {} + } + + $instance = new SomeClass(); + $instance->property; + CODE_SAMPLE, + <<<'CODE_SAMPLE' + $instance = new SomeClass(); + $instance->getProperty(); + CODE_SAMPLE, + ['var_dump'] + )]); + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [PropertyFetch::class]; + } + + /** + * @param \PhpParser\Node\Expr\PropertyFetch $node + */ + public function refactor(Node $node): ?Node + { + $className = $this->resolveClassName($node); + if ($className === null || !isset($this->classPropertyToGetterMap[$className])) { + return null; + } + + $propertyName = $this->getName($node->name); + + if (!isset($this->classPropertyToGetterMap[$className][$propertyName])) { + return null; + } + + $getterMethodName = $this->classPropertyToGetterMap[$className][$propertyName]; + + return $this->nodeFactory->createMethodCall($node->var, $getterMethodName); + } + + private function resolveClassName(PropertyFetch $propertyFetch): ?string + { + $type = $this->nodeTypeResolver->getType($propertyFetch->var); + + if ($type instanceof ObjectType) { + return $type->getClassName(); + } + + return null; + } + + public function configure(array $configuration): void + { + $this->classPropertyToGetterMap = $configuration; + } +} diff --git a/tests/lib/Rule/PropertyToGetterRector/Fixture/some_class.php.inc b/tests/lib/Rule/PropertyToGetterRector/Fixture/some_class.php.inc new file mode 100644 index 0000000..d746136 --- /dev/null +++ b/tests/lib/Rule/PropertyToGetterRector/Fixture/some_class.php.inc @@ -0,0 +1,37 @@ +foo; + } +} + +$some = new SomeClass(); +$some->foo; + +?> +----- +foo; + } +} + +$some = new SomeClass(); +$some->getFoo(); + +?> diff --git a/tests/lib/Rule/PropertyToGetterRector/PropertyToGetterRectorTest.php b/tests/lib/Rule/PropertyToGetterRector/PropertyToGetterRectorTest.php new file mode 100644 index 0000000..23f3960 --- /dev/null +++ b/tests/lib/Rule/PropertyToGetterRector/PropertyToGetterRectorTest.php @@ -0,0 +1,34 @@ +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/PropertyToGetterRector/config/configured_rule.php b/tests/lib/Rule/PropertyToGetterRector/config/configured_rule.php new file mode 100644 index 0000000..61eb97e --- /dev/null +++ b/tests/lib/Rule/PropertyToGetterRector/config/configured_rule.php @@ -0,0 +1,21 @@ +ruleWithConfiguration( + PropertyToGetterRector::class, + [ + 'Ibexa\Rector\Tests\Rule\PropertyToGetterRector\Fixture\SomeClass' => [ + 'foo' => 'getFoo', + ], + ] + ); +}; diff --git a/tests/lib/Rule/RemoveArgumentFromMethodCallRector/RemoveArgumentFromMethodCallRectorTest.php b/tests/lib/Rule/RemoveArgumentFromMethodCallRector/RemoveArgumentFromMethodCallRectorTest.php index 62c7339..999d92b 100644 --- a/tests/lib/Rule/RemoveArgumentFromMethodCallRector/RemoveArgumentFromMethodCallRectorTest.php +++ b/tests/lib/Rule/RemoveArgumentFromMethodCallRector/RemoveArgumentFromMethodCallRectorTest.php @@ -11,9 +11,6 @@ use PHPUnit\Framework\Attributes\DataProvider; use Rector\Testing\PHPUnit\AbstractRectorTestCase; -/** - * @covers \Ibexa\Rector\Rule\Internal\RemoveLegacyClassAliasRector - */ final class RemoveArgumentFromMethodCallRectorTest extends AbstractRectorTestCase { /** diff --git a/tests/lib/Sets/Ibexa50/Fixture/simplified_request_properties.php.inc b/tests/lib/Sets/Ibexa50/Fixture/simplified_request_properties.php.inc new file mode 100644 index 0000000..28571c2 --- /dev/null +++ b/tests/lib/Sets/Ibexa50/Fixture/simplified_request_properties.php.inc @@ -0,0 +1,21 @@ +pathinfo; + +?> +----- +getPathInfo(); + +?>