-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #302 from palantirnet/feature/_drupal_flush_css_js
New rector: Rector for _drupal_flush_css_js through new VersionedFunctionToServiceRector
- Loading branch information
Showing
6 changed files
with
208 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
84 changes: 84 additions & 0 deletions
84
src/Drupal10/Rector/Deprecation/VersionedFunctionToServiceRector.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,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'), | ||
] | ||
), | ||
]); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/Drupal10/Rector/ValueObject/VersionedFunctionToServiceConfiguration.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,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; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...tor/Deprecation/VersionedFunctionToServiceRector/VersionedFunctionToServiceRectorTest.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,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DrupalRector\Tests\Rector\Deprecation\VersionedFunctionToServiceRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
class VersionedFunctionToServiceRectorTest 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'; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...c/Drupal10/Rector/Deprecation/VersionedFunctionToServiceRector/config/configured_rule.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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use DrupalRector\Drupal10\Rector\Deprecation\VersionedFunctionToServiceRector; | ||
use DrupalRector\Drupal10\Rector\ValueObject\VersionedFunctionToServiceConfiguration; | ||
use DrupalRector\Tests\Rector\Deprecation\DeprecationBase; | ||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
DeprecationBase::addClass(VersionedFunctionToServiceRector::class, $rectorConfig, false, [ | ||
new VersionedFunctionToServiceConfiguration('10.2.0', '_drupal_flush_css_js', 'asset.query_string', 'reset'), | ||
]); | ||
}; |
13 changes: 13 additions & 0 deletions
13
.../src/Drupal10/Rector/Deprecation/VersionedFunctionToServiceRector/fixture/fixture.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,13 @@ | ||
<?php | ||
|
||
function simple_example() { | ||
_drupal_flush_css_js(); | ||
} | ||
?> | ||
----- | ||
<?php | ||
|
||
function simple_example() { | ||
\Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.2.0', fn() => \Drupal::service('asset.query_string')->reset(), fn() => _drupal_flush_css_js()); | ||
} | ||
?> |