-
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.
Add rector for system_time_zones (#271)
* Add rector for system_time_zones * Fix styl * Remove extra comment
- Loading branch information
Showing
6 changed files
with
208 additions
and
2 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
104 changes: 104 additions & 0 deletions
104
src/Drupal10/Rector/Deprecation/SystemTimeZonesRector.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,104 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DrupalRector\Drupal10\Rector\Deprecation; | ||
|
||
use DrupalRector\Contract\VersionedConfigurationInterface; | ||
use DrupalRector\Rector\AbstractDrupalCoreRector; | ||
use DrupalRector\Rector\ValueObject\DrupalIntroducedVersionConfiguration; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ConstFetch; | ||
use Rector\Core\PhpParser\Node\Value\ValueResolver; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
class SystemTimeZonesRector extends AbstractDrupalCoreRector | ||
{ | ||
/** | ||
* @var array|DrupalIntroducedVersionConfiguration[] | ||
*/ | ||
protected array $configuration; | ||
|
||
/** | ||
* @var \Rector\Core\PhpParser\Node\Value\ValueResolver | ||
*/ | ||
private ValueResolver $valueResolver; | ||
|
||
public function __construct(ValueResolver $valueResolver) | ||
{ | ||
$this->valueResolver = $valueResolver; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [ | ||
Node\Expr\FuncCall::class, | ||
]; | ||
} | ||
|
||
public function configure(array $configuration): void | ||
{ | ||
foreach ($configuration as $value) { | ||
if (!($value instanceof DrupalIntroducedVersionConfiguration)) { | ||
throw new \InvalidArgumentException(sprintf('Each configuration item must be an instance of "%s"', DrupalIntroducedVersionConfiguration::class)); | ||
} | ||
} | ||
|
||
parent::configure($configuration); | ||
} | ||
|
||
public function refactorWithConfiguration(Node $node, VersionedConfigurationInterface $configuration) | ||
{ | ||
if (!$node instanceof Node\Expr\FuncCall || $this->getName($node) !== 'system_time_zones') { | ||
return null; | ||
} | ||
|
||
$args = $node->getArgs(); | ||
|
||
if (count($args) == 2 && $args[1]->value instanceof ConstFetch && $this->valueResolver->isTrue($args[1]->value)) { | ||
return $this->nodeFactory->createStaticCall('Drupal\Core\Datetime\TimeZoneFormHelper', 'getOptionsListByRegion'); | ||
} | ||
|
||
if (count($args) == 0) { | ||
return $this->nodeFactory->createStaticCall('Drupal\Core\Datetime\TimeZoneFormHelper', 'getOptionsList'); | ||
} | ||
|
||
if (count($args) <= 2) { | ||
return $this->nodeFactory->createStaticCall('Drupal\Core\Datetime\TimeZoneFormHelper', 'getOptionsList', [$args[0]]); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Fixes deprecated system_time_zones() calls', [ | ||
new ConfiguredCodeSample( | ||
<<<'CODE_BEFORE' | ||
system_time_zones(); | ||
system_time_zones(FALSE, TRUE); | ||
system_time_zones(NULL, FALSE); | ||
system_time_zones(TRUE, FALSE); | ||
CODE_BEFORE | ||
, | ||
<<<'CODE_AFTER' | ||
\Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsList(); | ||
\Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsListByRegion(); | ||
\Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsList(NULL); | ||
\Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsList(TRUE); | ||
CODE_AFTER | ||
, | ||
[ | ||
new DrupalIntroducedVersionConfiguration('10.1.0'), | ||
] | ||
), | ||
]); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
tests/src/Drupal10/Rector/Deprecation/SystemTimeZonesRector/SystemTimeZonesRectorTest.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\Drupal10\Rector\Deprecation\SystemTimeZonesRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
class SystemTimeZonesRectorTest 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
tests/src/Drupal10/Rector/Deprecation/SystemTimeZonesRector/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\SystemTimeZonesRector; | ||
use DrupalRector\Rector\ValueObject\DrupalIntroducedVersionConfiguration; | ||
use DrupalRector\Tests\Rector\Deprecation\DeprecationBase; | ||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
DeprecationBase::addClass(SystemTimeZonesRector::class, $rectorConfig, false, [ | ||
new DrupalIntroducedVersionConfiguration('10.1.0'), | ||
]); | ||
}; |
26 changes: 26 additions & 0 deletions
26
...s/src/Drupal10/Rector/Deprecation/SystemTimeZonesRector/fixture/system_time_zones.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,26 @@ | ||
<?php | ||
|
||
function simple_example() { | ||
system_time_zones(); | ||
|
||
system_time_zones(FALSE, TRUE); | ||
|
||
system_time_zones(NULL, FALSE); | ||
|
||
system_time_zones(TRUE, FALSE); | ||
} | ||
?> | ||
|
||
----- | ||
<?php | ||
|
||
function simple_example() { | ||
\Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => system_time_zones(), fn() => \Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsList()); | ||
|
||
\Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => system_time_zones(FALSE, TRUE), fn() => \Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsListByRegion()); | ||
|
||
\Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => system_time_zones(NULL, FALSE), fn() => \Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsList(NULL)); | ||
|
||
\Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => system_time_zones(TRUE, FALSE), fn() => \Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsList(TRUE)); | ||
} | ||
?> |