-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added lots of rectors for deprecated methods
- Loading branch information
Showing
37 changed files
with
1,230 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
/vendor | ||
# Composer | ||
/vendor/ | ||
/composer.lock | ||
|
||
# PhpUnit | ||
/.phpunit.cache/ | ||
/.phpunit.result.cache |
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
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,14 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" bootstrap="vendor/autoload.php" executionOrder="depends,defects" beStrictAboutOutputDuringTests="true" colors="true" failOnRisky="true" failOnWarning="true" cacheDirectory=".phpunit.cache"> | ||
<testsuites> | ||
<testsuite name="default"> | ||
<directory suffix="Test.php">tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<coverage/> | ||
<source> | ||
<include> | ||
<directory suffix=".php">src</directory> | ||
</include> | ||
</source> | ||
</phpunit> |
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,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Contao\Rector\Rector; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Reflection\ClassReflection; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Core\Reflection\ReflectionResolver; | ||
|
||
abstract class AbstractLegacyFrameworkCallRector extends AbstractRector | ||
{ | ||
public function __construct(private readonly ReflectionResolver $reflectionResolver) | ||
{ | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [ | ||
Node\Expr\StaticCall::class, | ||
Node\Expr\MethodCall::class, | ||
]; | ||
} | ||
|
||
protected function isParentStaticOrMethodClassCall(Node $node, string $oldClassName, string $oldMethodName, string|null $newClassName = null, string|null $newMethodName = null): bool | ||
{ | ||
if ($this->isMethodCall($node, $oldClassName, $oldMethodName)) { | ||
return true; | ||
} | ||
|
||
if (!$node instanceof Node\Expr\StaticCall || !$this->isName($node->name, $oldMethodName)) { | ||
return false; | ||
} | ||
|
||
$classReflection = $this->reflectionResolver->resolveClassReflectionSourceObject($node); | ||
|
||
if (!$classReflection instanceof ClassReflection) { | ||
if (!$this->isNames($node->class, ['static', 'self'])) { | ||
if ($oldMethodName === $newMethodName && $this->getName($node->class) === $newClassName) { | ||
return false; | ||
} | ||
|
||
return is_a($this->getName($node->class), $oldClassName, true); | ||
} | ||
|
||
$classReflection = $this->reflectionResolver->resolveClassReflection($node); | ||
} | ||
|
||
if (!$classReflection instanceof ClassReflection) { | ||
return false; | ||
} | ||
|
||
if ($oldMethodName === $newMethodName && $classReflection->getName() === $newClassName) { | ||
return false; | ||
} | ||
|
||
return $classReflection->is($oldClassName); | ||
} | ||
|
||
protected function isMethodCall(Node $node, string $className, string $methodName): bool | ||
{ | ||
return $node instanceof Node\Expr\MethodCall | ||
&& $this->isName($node->name, $methodName) | ||
&& $this->isObjectType($node->var, new ObjectType($className)) | ||
; | ||
} | ||
} |
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,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Contao\Rector\Rector; | ||
|
||
use Contao\Rector\ValueObject\ConstantToServiceParameter; | ||
use PhpParser\Node; | ||
use Rector\Core\Contract\Rector\ConfigurableRectorInterface; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class ConstantToServiceParameterRector extends AbstractRector implements ConfigurableRectorInterface | ||
{ | ||
/** | ||
* @var array<ConstantToServiceParameter> | ||
*/ | ||
private array $configuration; | ||
|
||
public function configure(array $configuration): void | ||
{ | ||
Assert::allIsAOf($configuration, ConstantToServiceParameter::class); | ||
$this->configuration = $configuration; | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Fixes deprecated constants to service parameters', [ | ||
new CodeSample( | ||
<<<'CODE_BEFORE' | ||
$projectDir = TL_ROOT; | ||
CODE_BEFORE | ||
, | ||
<<<'CODE_AFTER' | ||
$projectDir = \Contao\System::getContainer()->getParameter('kernel.project_dir'); | ||
CODE_AFTER | ||
), | ||
]); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [ | ||
Node\Expr\ConstFetch::class, | ||
]; | ||
} | ||
|
||
public function refactor(Node $node): ?Node | ||
{ | ||
assert($node instanceof Node\Expr\ConstFetch); | ||
|
||
foreach ($this->configuration as $config) { | ||
if ($this->isName($node->name, $config->getConstant())) { | ||
$container = new Node\Expr\StaticCall(new Node\Name\FullyQualified('Contao\System'), 'getContainer'); | ||
$node = new Node\Expr\MethodCall($container, 'getParameter', [new Node\Arg(new Node\Scalar\String_($config->getParameter()))]); | ||
|
||
return $node; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
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,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Contao\Rector\Rector; | ||
|
||
use Contao\Controller; | ||
use PhpParser\Node; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
final class ControllerMethodToVersionsClassRector extends AbstractLegacyFrameworkCallRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Fixes deprecated Controller::createInitialVersion() and Controller::createNewVersion() to Versions class calls', [ | ||
new CodeSample( | ||
<<<'CODE_BEFORE' | ||
\Contao\Controller::createInitialVersion('tl_page', 17); | ||
\Contao\Controller::createNewVersion('tl_page', 17); | ||
CODE_BEFORE | ||
, | ||
<<<'CODE_AFTER' | ||
(new \Contao\Versions('tl_page', 17))->initialize(); | ||
(new \Contao\Versions('tl_page', 17))->create(); | ||
CODE_AFTER | ||
), | ||
]); | ||
} | ||
|
||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($this->isMethodCall($node, Controller::class, 'createInitialVersion')) { | ||
$versions = new Node\Expr\New_(new Node\Name\FullyQualified('Contao\Versions'), $node->args); | ||
$node = new Node\Expr\MethodCall($versions, 'initialize'); | ||
|
||
return $node; | ||
} | ||
|
||
if ($this->isMethodCall($node, Controller::class, 'createNewVersion')) { | ||
$versions = new Node\Expr\New_(new Node\Name\FullyQualified('Contao\Versions'), $node->args); | ||
$node = new Node\Expr\MethodCall($versions, 'create'); | ||
|
||
return $node; | ||
} | ||
|
||
return $node; | ||
} | ||
} |
Oops, something went wrong.