diff --git a/config/sets/contao/contao-413.php b/config/sets/contao/contao-413.php index d786ce6..aff9703 100644 --- a/config/sets/contao/contao-413.php +++ b/config/sets/contao/contao-413.php @@ -7,9 +7,11 @@ use Contao\Controller; use Contao\CoreBundle\Security\ContaoCorePermissions; use Contao\Folder; +use Contao\Rector\Rector\ConfigOptionToParameterRector; use Contao\Rector\Rector\InsertTagsServiceRector; use Contao\Rector\Rector\LegacyFrameworkCallToServiceCallRector; use Contao\Rector\Rector\SystemLanguagesToServiceRector; +use Contao\Rector\ValueObject\ConfigOptionToParameter; use Contao\Rector\ValueObject\LegacyFrameworkCallToServiceCall; use Contao\StringUtil; use Patchwork\Utf8; @@ -68,6 +70,9 @@ // Contao 4.12 $rectorConfig->rule(SystemLanguagesToServiceRector::class); + $rectorConfig->ruleWithConfiguration(ConfigOptionToParameterRector::class, [ + new ConfigOptionToParameter('validImageTypes', '%contao.image.valid_extensions%'), + ]); // Contao 4.13 $rectorConfig->rule(InsertTagsServiceRector::class); diff --git a/src/Rector/ConfigOptionToParameterRector.php b/src/Rector/ConfigOptionToParameterRector.php new file mode 100644 index 0000000..5156229 --- /dev/null +++ b/src/Rector/ConfigOptionToParameterRector.php @@ -0,0 +1,72 @@ + + */ + private array $configuration; + + public function configure(array $configuration): void + { + Assert::allIsAOf($configuration, ConfigOptionToParameter::class); + $this->configuration = $configuration; + } + + public function getRuleDefinition(): RuleDefinition + { + return new RuleDefinition('Fixes deprecated config options to parameters', [ + new CodeSample( + <<<'CODE_BEFORE' +$extensions = \Contao\Config::get('validImageTypes'); +CODE_BEFORE + , + <<<'CODE_AFTER' +$extensions = '%contao.image.valid_extensions%'; +CODE_AFTER + ), + ]); + } + + public function getNodeTypes(): array + { + return [ + Node\Expr\StaticCall::class, + Node\Expr\MethodCall::class, + ]; + } + + public function refactor(Node $node): ?Node + { + foreach ($this->configuration as $config) { + if ($this->isStaticCall($node, Config::class, 'get', $config->getOption())) { + return new Node\Scalar\String_($config->getParameter()); + } + } + + return null; + } + + protected function isStaticCall(Node $node, string $className, string $methodName, string $option): bool + { + return $node instanceof Node\Expr\StaticCall + && $this->isName($node->name, $methodName) + && $this->isObjectType($node->class, new ObjectType($className)) + && $option + ; + } +} diff --git a/src/ValueObject/ConfigOptionToParameter.php b/src/ValueObject/ConfigOptionToParameter.php new file mode 100644 index 0000000..ed9ee57 --- /dev/null +++ b/src/ValueObject/ConfigOptionToParameter.php @@ -0,0 +1,24 @@ +option; + } + + public function getParameter(): string + { + return $this->parameter; + } +} diff --git a/tests/Rector/ConfigOptionToParameterRector/ConfigOptionToParameterRectorTest.php b/tests/Rector/ConfigOptionToParameterRector/ConfigOptionToParameterRectorTest.php new file mode 100644 index 0000000..b21cb8e --- /dev/null +++ b/tests/Rector/ConfigOptionToParameterRector/ConfigOptionToParameterRectorTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/config.php'; + } +} diff --git a/tests/Rector/ConfigOptionToParameterRector/config/config.php b/tests/Rector/ConfigOptionToParameterRector/config/config.php new file mode 100644 index 0000000..735f33c --- /dev/null +++ b/tests/Rector/ConfigOptionToParameterRector/config/config.php @@ -0,0 +1,13 @@ +ruleWithConfiguration(ConfigOptionToParameterRector::class, [ + new ConfigOptionToParameter('validImageTypes', '%contao.image.valid_extensions%'), + ]); +}; diff --git a/tests/Rector/ConfigOptionToParameterRector/fixture/constants.php.inc b/tests/Rector/ConfigOptionToParameterRector/fixture/constants.php.inc new file mode 100644 index 0000000..f6bc705 --- /dev/null +++ b/tests/Rector/ConfigOptionToParameterRector/fixture/constants.php.inc @@ -0,0 +1,21 @@ + +----- +