Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Versioned RenameClassRector #303

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions config/drupal-10/drupal-10.2-deprecations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

declare(strict_types=1);

use DrupalRector\Drupal10\Rector\Deprecation\VersionedFunctionToServiceRector;
use DrupalRector\Drupal10\Rector\ValueObject\VersionedFunctionToServiceConfiguration;
use DrupalRector\Rector\Deprecation\FunctionToStaticRector;
use DrupalRector\Rector\Deprecation\MethodToMethodWithCheckRector;
use DrupalRector\Rector\ValueObject\FunctionToStaticConfiguration;
Expand All @@ -24,4 +26,9 @@
new MethodToMethodWithCheckConfiguration('Drupal\system\Plugin\ImageToolkit\GDToolkit', 'getResource', 'getImage'),
new MethodToMethodWithCheckConfiguration('Drupal\system\Plugin\ImageToolkit\GDToolkit', 'setResource', 'setImage'),
]);

// https://www.drupal.org/node/3358337
$rectorConfig->ruleWithConfiguration(VersionedFunctionToServiceRector::class, [
new VersionedFunctionToServiceConfiguration('10.2.0', '_drupal_flush_css_js', 'asset.query_string', 'reset'),
]);
};
109 changes: 109 additions & 0 deletions src/Drupal10/Rector/Deprecation/RenameClassRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

declare(strict_types=1);

namespace DrupalRector\Drupal10\Rector\Deprecation;

use DrupalRector\Contract\VersionedConfigurationInterface;
use DrupalRector\Drupal10\Rector\ValueObject\RenameClassRectorConfiguration;
use DrupalRector\Rector\AbstractDrupalCoreRector;
use DrupalRector\Rector\ValueObject\DrupalIntroducedVersionConfiguration;
use PhpParser\Node;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Property;
use Rector\Configuration\RenamedClassesDataCollector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Renaming\NodeManipulator\ClassRenamer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

class RenameClassRector extends AbstractDrupalCoreRector
{
/**
* @var array|DrupalIntroducedVersionConfiguration[]
*/
protected array $configuration;

/**
* @readonly
*
* @var RenamedClassesDataCollector
*/
private $renamedClassesDataCollector;
/**
* @readonly
*
* @var ClassRenamer
*/
private $classRenamer;

public function __construct(RenamedClassesDataCollector $renamedClassesDataCollector, ClassRenamer $classRenamer)
{
$this->renamedClassesDataCollector = $renamedClassesDataCollector;
$this->classRenamer = $classRenamer;
}

/**
* {@inheritdoc}
*/
public function getNodeTypes(): array
{
return [FullyQualified::class, Property::class, FunctionLike::class, Expression::class, ClassLike::class, If_::class];
}

public function configure(array $configuration): void
{
foreach ($configuration as $value) {
if (!($value instanceof RenameClassRectorConfiguration)) {
throw new \InvalidArgumentException(sprintf('Each configuration item must be an instance of "%s"', RenameClassRectorConfiguration::class));
}
}

parent::configure($configuration);
}

public function refactorWithConfiguration(Node $node, VersionedConfigurationInterface $configuration)
{
$oldToNewClasses = $this->renamedClassesDataCollector->getOldToNewClasses();
if ($oldToNewClasses !== []) {
/** @var \PHPStan\Analyser\Scope $scope */
$scope = $node->getAttribute(AttributeKey::SCOPE);
$return = $this->classRenamer->renameNode($node, $oldToNewClasses, $scope);
if (!is_null($return)) {
$scope->getFile();

return $return;
}
}

return null;
}

/**
* {@inheritdoc}
*/
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Fixes deprecated watchdog_exception(\'update\', $exception) calls', [
new ConfiguredCodeSample(
<<<'CODE_BEFORE'
watchdog_exception('update', $exception);
CODE_BEFORE
,
<<<'CODE_AFTER'
use \Drupal\Core\Utility\Error;
$logger = \Drupal::logger('update');
Error::logException($logger, $exception);
CODE_AFTER
,
[
new DrupalIntroducedVersionConfiguration('10.1.0'),
]
),
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace DrupalRector\Drupal10\Rector\Deprecation;

use DrupalRector\Contract\VersionedConfigurationInterface;
use DrupalRector\Drupal10\Rector\ValueObject\VersionedFunctionToServiceConfiguration;
use DrupalRector\Rector\AbstractDrupalCoreRector;
use PhpParser\Node;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* Replaces deprecated function call with service method call with backwards compatibility.
*/
class VersionedFunctionToServiceRector extends AbstractDrupalCoreRector
{
/**
* @var array|VersionedFunctionToServiceConfiguration[]
*/
protected array $configurations = [];

public function configure(array $configuration): void
{
foreach ($configuration as $value) {
if (!($value instanceof VersionedFunctionToServiceConfiguration)) {
throw new \InvalidArgumentException(sprintf('Each configuration item must be an instance of "%s"', VersionedFunctionToServiceConfiguration::class));
}
}

parent::configure($configuration);
}

/**
* {@inheritdoc}
*/
public function getNodeTypes(): array
{
return [
Node\Expr\FuncCall::class,
];
}

/**
* @param Node\Expr\FuncCall $node
* @param VersionedFunctionToServiceConfiguration $configuration
*
* @return Node|null
*/
public function refactorWithConfiguration(Node $node, VersionedConfigurationInterface $configuration): ?Node
{
/** @var Node\Expr\FuncCall $node */
if ($this->getName($node->name) === $configuration->getDeprecatedFunctionName()) {
// This creates a service call like `\Drupal::service('file_system').
$service = new Node\Expr\StaticCall(new Node\Name\FullyQualified('Drupal'), 'service', [new Node\Arg(new Node\Scalar\String_($configuration->getServiceName()))]);

$method_name = new Node\Identifier($configuration->getServiceMethodName());

return new Node\Expr\MethodCall($service, $method_name, $node->args);
}

return null;
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Fixes deprecated function to service calls, used in Drupal 8 and 9 deprecations', [
new ConfiguredCodeSample(
<<<'CODE_BEFORE'
_drupal_flush_css_js();
CODE_BEFORE
,
<<<'CODE_AFTER'
\Drupal::service('asset.query_string')->reset();
CODE_AFTER
,
[
new VersionedFunctionToServiceConfiguration('10.2.0', '_drupal_flush_css_js', 'asset.query_string', 'reset'),
]
),
]);
}
}
38 changes: 38 additions & 0 deletions src/Drupal10/Rector/ValueObject/RenameClassRectorConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace DrupalRector\Drupal10\Rector\ValueObject;

use DrupalRector\Contract\VersionedConfigurationInterface;

class RenameClassRectorConfiguration implements VersionedConfigurationInterface
{
private string $introducedVersion;

private string $oldClass;

private string $newClass;

public function __construct(string $introducedVersion, string $oldClass, string $newClass)
{
$this->introducedVersion = $introducedVersion;
$this->oldClass = $oldClass;
$this->newClass = $newClass;
}

public function getIntroducedVersion(): string
{
return $this->introducedVersion;
}

public function getOldClass(): string
{
return $this->oldClass;
}

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

declare(strict_types=1);

namespace DrupalRector\Drupal10\Rector\ValueObject;

use DrupalRector\Contract\VersionedConfigurationInterface;

class VersionedFunctionToServiceConfiguration implements VersionedConfigurationInterface
{
/**
* The deprecated function name.
*/
protected string $deprecatedFunctionName;

/**
* The replacement service name.
*/
protected string $serviceName;

/**
* The replacement service method.
*/
protected string $serviceMethodName;

protected string $introducedVersion;

public function __construct(string $introducedVersion, string $deprecatedFunctionName, string $serviceName, string $serviceMethodName)
{
$this->deprecatedFunctionName = $deprecatedFunctionName;
$this->serviceName = $serviceName;
$this->serviceMethodName = $serviceMethodName;
$this->introducedVersion = $introducedVersion;
}

public function getDeprecatedFunctionName(): string
{
return $this->deprecatedFunctionName;
}

public function getServiceName(): string
{
return $this->serviceName;
}

public function getServiceMethodName(): string
{
return $this->serviceMethodName;
}

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

declare(strict_types=1);

namespace DrupalRector\Tests\Drupal10\Rector\Deprecation\WatchdogExceptionRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

class RenameClassRectorTest extends AbstractRectorTestCase
{
/**
* @covers ::refactor
*
* @dataProvider provideData
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

/**
* @return Iterator<<string>>
*/
public static function provideData(): \Iterator
{
return self::yieldFilesFromDirectory(__DIR__.'/fixture');
}

public function provideConfigFilePath(): string
{
// must be implemented
return __DIR__.'/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

use DrupalRector\Drupal10\Rector\ValueObject\RenameClassRectorConfiguration;
use DrupalRector\Tests\Rector\Deprecation\DeprecationBase;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
DeprecationBase::addClass(DrupalRector\Drupal10\Rector\Deprecation\RenameClassRector::class, $rectorConfig, false, [
new RenameClassRectorConfiguration('10.1.0', 'Drupal\\Tests\\field\\Traits\\EntityReferenceTestTrait', 'Drupal\\Tests\\field\\Traits\\EntityReferenceFieldCreationTrait'),
new RenameClassRectorConfiguration('10.1.0', 'Drupal\\OldClass', 'Drupal\\NewClass'),
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Drupal\Tests\field\Traits\EntityReferenceTestTrait;

class EntityReferenceItemTest extends FieldKernelTestBase {

use EntityReferenceTestTrait;

}

?>
-----
<?php

use Drupal\Tests\field\Traits\EntityReferenceFieldCreationTrait;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;

class EntityReferenceItemTest extends FieldKernelTestBase {

use EntityReferenceFieldCreationTrait;

}
Loading
Loading