forked from contao/contao-rector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Implements contao#3
- Loading branch information
Showing
6 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
tests/Rector/ConfigOptionToParameterRector/ConfigOptionToParameterRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
tests/Rector/ConfigOptionToParameterRector/config/config.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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%'), | ||
]); | ||
}; |
21 changes: 21 additions & 0 deletions
21
tests/Rector/ConfigOptionToParameterRector/fixture/constants.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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%'; | ||
} | ||
} | ||
?> |