Skip to content
Open
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
12 changes: 12 additions & 0 deletions config/drupal-10/drupal-10.3-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\VersionedClassConstantToClassConstantRector;
use DrupalRector\Drupal10\Rector\ValueObject\VersionedClassConstantToClassConstantConfiguration;
use DrupalRector\Rector\Deprecation\FunctionToStaticRector;
use DrupalRector\Rector\ValueObject\FunctionToStaticConfiguration;
use Rector\Config\RectorConfig;
Expand All @@ -12,4 +14,14 @@
new FunctionToStaticConfiguration('10.3.0', 'file_icon_class', 'Drupal\file\IconMimeTypes', 'getIconClass'),
new FunctionToStaticConfiguration('10.3.0', 'file_icon_map', 'Drupal\file\IconMimeTypes', 'getGenericMimeType'),
]);

$rectorConfig->ruleWithConfiguration(VersionedClassConstantToClassConstantRector::class, [
new VersionedClassConstantToClassConstantConfiguration(
'10.3.0',
'Drupal\Core\File\FileSystemInterface',
'EXISTS_REPLACE',
'Drupal\Core\File\FileExists',
'Replace',
),
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

declare(strict_types=1);

namespace DrupalRector\Drupal10\Rector\Deprecation;

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

/**
* Replaces deprecated constant with class constant.
*
* What is covered:
* - Replacement with a use statement.
*/
class VersionedClassConstantToClassConstantRector extends AbstractDrupalCoreRector implements ConfigurableRectorInterface
{
public function configure(array $configuration): void
{
foreach ($configuration as $value) {
if (!$value instanceof VersionedClassConstantToClassConstantConfiguration) {
throw new \InvalidArgumentException(sprintf('Each configuration item must be an instance of "%s"', VersionedClassConstantToClassConstantConfiguration::class));
}
}

parent::configure($configuration);
}

/**
* {@inheritdoc}
*/
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Fixes deprecated class contant use, used in Drupal 9.1 deprecations', [
new ConfiguredCodeSample(
<<<'CODE_BEFORE'
$value = Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_NAME;
$value2 = Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_OBJECT;
$value3 = Symfony\Cmf\Component\Routing\RouteObjectInterface::CONTROLLER_NAME;
CODE_BEFORE,
<<<'CODE_AFTER'
$value = \Drupal\Core\Routing\RouteObjectInterface::ROUTE_NAME;
$value2 = \Drupal\Core\Routing\RouteObjectInterface::ROUTE_OBJECT;
$value3 = \Drupal\Core\Routing\RouteObjectInterface::CONTROLLER_NAME;
CODE_AFTER,
[
new ClassConstantToClassConstantConfiguration(
'Symfony\Cmf\Component\Routing\RouteObjectInterface',
'ROUTE_NAME',
'Drupal\Core\Routing\RouteObjectInterface',
'ROUTE_NAME',
),
new ClassConstantToClassConstantConfiguration(
'Symfony\Cmf\Component\Routing\RouteObjectInterface',
'ROUTE_OBJECT',
'Drupal\Core\Routing\RouteObjectInterface',
'ROUTE_OBJECT',
),
new ClassConstantToClassConstantConfiguration(
'Symfony\Cmf\Component\Routing\RouteObjectInterface',
'CONTROLLER_NAME',
'Drupal\Core\Routing\RouteObjectInterface',
'CONTROLLER_NAME',
),
]
),
]);
}

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

/**
* @param Node\Expr\ClassConstFetch $node
* @param VersionedClassConstantToClassConstantConfiguration $configuration
*
* @return Node|null
*/
public function refactorWithConfiguration(Node $node, VersionedConfigurationInterface $configuration): ?Node
{
if ($this->getName($node->name) === $configuration->getDeprecated() && $this->getName($node->class) === $configuration->getDeprecatedClass()) {
// We add a fully qualified class name and the parameters in `rector.php` adds the use statement.
$fully_qualified_class = new Node\Name\FullyQualified($configuration->getClass());

$name = new Node\Identifier($configuration->getConstant());

return new Node\Expr\ClassConstFetch($fully_qualified_class, $name);
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace DrupalRector\Drupal10\Rector\ValueObject;

use DrupalRector\Contract\VersionedConfigurationInterface;

class VersionedClassConstantToClassConstantConfiguration implements VersionedConfigurationInterface
{
protected string $deprecated;
protected string $class;
protected string $constant;

protected string $deprecatedClass;

protected string $introducedVersion;

public function __construct(string $introducedVersion, string $deprecatedClass, string $deprecated, string $class, string $constant)
{
$this->introducedVersion = $introducedVersion;
$this->deprecatedClass = $deprecatedClass;
$this->deprecated = $deprecated;
$this->class = $class;
$this->constant = $constant;
}

public function getDeprecated(): string
{
return $this->deprecated;
}

public function getClass(): string
{
return $this->class;
}

public function getConstant(): string
{
return $this->constant;
}

public function getDeprecatedClass(): string
{
return $this->deprecatedClass;
}

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\FileSystemExistReplaceToFileExistsReplace;

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

class FileSystemExistReplaceToFileExistsReplaceTest 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,19 @@
<?php

declare(strict_types=1);

use DrupalRector\Drupal10\Rector\Deprecation\VersionedClassConstantToClassConstantRector;
use DrupalRector\Drupal10\Rector\ValueObject\VersionedClassConstantToClassConstantConfiguration;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(VersionedClassConstantToClassConstantRector::class, [
new VersionedClassConstantToClassConstantConfiguration(
'10.3.0',
'Drupal\Core\File\FileSystemInterface',
'EXISTS_REPLACE',
'Drupal\Core\File\FileExists',
'Replace',
),
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\File\FileExists;

function simple_example() {
$something = FileSystemInterface::EXISTS_REPLACE;
}

class AnotherExample {
public function test() {
$mode = FileSystemInterface::EXISTS_REPLACE;
}

public function foo() {
str_contains('mystring', FileSystemInterface::EXISTS_REPLACE);
}
}

?>
-----
<?php

use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\File\FileExists;

function simple_example() {
$something = \Drupal\Core\File\FileExists::Replace;
}

class AnotherExample {
public function test() {
$mode = \Drupal\Core\File\FileExists::Replace;
}

public function foo() {
str_contains('mystring', \Drupal\Core\File\FileExists::Replace);
}
}

?>
Loading