Skip to content

Commit

Permalink
Add ConfigOptionToParameterRector
Browse files Browse the repository at this point in the history
- Implements contao#3
  • Loading branch information
zoglo committed May 8, 2024
1 parent f50686e commit ffa5e5b
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config/sets/contao/contao-413.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
72 changes: 72 additions & 0 deletions src/Rector/ConfigOptionToParameterRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace Contao\Rector\Rector;

use Contao\Config;
use Contao\Rector\ValueObject\ConfigOptionToParameter;
use PhpParser\Node;
use PHPStan\Type\ObjectType;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

final class ConfigOptionToParameterRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var array<ConfigOptionToParameter>
*/
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
;
}
}
24 changes: 24 additions & 0 deletions src/ValueObject/ConfigOptionToParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Contao\Rector\ValueObject;

class ConfigOptionToParameter
{
public function __construct(
private readonly string $option,
private readonly string $parameter,
) {
}

public function getOption(): string
{
return $this->option;
}

public function getParameter(): string
{
return $this->parameter;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Contao\Rector\Tests\Rector\ConfigOptionToParameterRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ConfigOptionToParameterRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/config.php';
}
}
13 changes: 13 additions & 0 deletions tests/Rector/ConfigOptionToParameterRector/config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Contao\Rector\Rector\ConfigOptionToParameterRector;
use Contao\Rector\ValueObject\ConfigOptionToParameter;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(ConfigOptionToParameterRector::class, [
new ConfigOptionToParameter('validImageTypes', '%contao.image.valid_extensions%'),
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

class Foo
{
public function bar()
{
$extensions = \Contao\Config::get('validImageTypes');
}
}
?>
-----
<?php

class Foo
{
public function bar()
{
$extensions = '%contao.image.valid_extensions%';
}
}
?>

0 comments on commit ffa5e5b

Please sign in to comment.