Skip to content

Commit

Permalink
Merge pull request #241 from palantirnet/feature/static-argument-rena…
Browse files Browse the repository at this point in the history
…me-rector

StaticArgumentRenameBase and DrupalServiceRenameBase are now covered with StaticArgumentRenameRector
  • Loading branch information
bbrala committed Aug 22, 2023
2 parents 37dd08f + 0ac14fa commit c8e5d87
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 281 deletions.
20 changes: 10 additions & 10 deletions config/drupal-8/drupal-8.8-deprecations.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

use DrupalRector\Rector\Deprecation\EntityTypeGetLowercaseLabelRector;
use DrupalRector\Rector\Deprecation\FileDefaultSchemeRector;
use DrupalRector\Rector\Deprecation\DrupalServiceRenameRector;
use DrupalRector\Rector\Deprecation\FunctionToServiceRector;
use DrupalRector\Rector\Deprecation\PathAliasManagerServiceNameRector;
use DrupalRector\Rector\Deprecation\PathAliasRepositoryRector;
use DrupalRector\Rector\Deprecation\PathAliasWhitelistServiceNameRector;
use DrupalRector\Rector\Deprecation\PathProcessorAliasServiceNameRector;
use DrupalRector\Rector\Deprecation\PathSubscriberServiceNameRector;
use DrupalRector\Rector\ValueObject\DrupalServiceRenameConfiguration;
use DrupalRector\Rector\ValueObject\FunctionToServiceConfiguration;

return static function (\Rector\Config\RectorConfig $rectorConfig): void {
$rectorConfig->rule(PathAliasManagerServiceNameRector::class);
$rectorConfig->rule(PathAliasWhitelistServiceNameRector::class);
$rectorConfig->rule(PathSubscriberServiceNameRector::class);
$rectorConfig->rule(PathProcessorAliasServiceNameRector::class);
$rectorConfig->rule(PathAliasRepositoryRector::class);
$rectorConfig->ruleWithConfiguration(DrupalServiceRenameRector::class, [
new DrupalServiceRenameConfiguration('path.alias_repository', 'path_alias.repository'),
new DrupalServiceRenameConfiguration('path.alias_whitelist', 'path_alias.whitelist'),
new DrupalServiceRenameConfiguration('path_processor_alias', 'path_alias.path_processor'),
new DrupalServiceRenameConfiguration('path_subscriber', 'path_alias.subscriber'),
new DrupalServiceRenameConfiguration('path.alias_manager', 'path_alias.manager'),
]);

$rectorConfig->rule(FileDefaultSchemeRector::class);

$rectorConfig->ruleWithConfiguration(FunctionToServiceRector::class,
Expand Down
47 changes: 0 additions & 47 deletions src/Rector/Deprecation/Base/DrupalServiceRenameBase.php

This file was deleted.

74 changes: 0 additions & 74 deletions src/Rector/Deprecation/Base/StaticArgumentRenameBase.php

This file was deleted.

80 changes: 80 additions & 0 deletions src/Rector/Deprecation/DrupalServiceRenameRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace DrupalRector\Rector\Deprecation;

use DrupalRector\Rector\ValueObject\DrupalServiceRenameConfiguration;
use PhpParser\Node;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

class DrupalServiceRenameRector extends AbstractRector implements ConfigurableRectorInterface {

/**
* @var DrupalServiceRenameConfiguration[] $staticArgumentRenameConfigs
*/
protected array $staticArgumentRenameConfigs = [];

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

$this->staticArgumentRenameConfigs = $configuration;
}

public function getNodeTypes(): array {
return [
Node\Expr\StaticCall::class,
];
}

public function refactor(Node $node) {
if ($node instanceof Node\Expr\StaticCall) {
foreach ($this->staticArgumentRenameConfigs as $configuration) {
if ($this->getName($node->name) === 'service' && (string) $node->class === 'Drupal') {
if (count($node->args) === 1) {
/* @var Node\Arg $argument */
$argument = $node->args[0];

if ($argument->value instanceof Node\Scalar\String_ && $argument->value->value === $configuration->getDeprecatedService()) {
$node->args[0] = new Node\Arg(new Node\Scalar\String_($configuration->getNewService()));

return $node;
}
}
}
}
}

return NULL;
}

public function getRuleDefinition(): RuleDefinition {
return new RuleDefinition('Renames the IDs in Drupal::service() calls', [
new ConfiguredCodeSample(
<<<'CODE_BEFORE'
\Drupal::service('old')->foo();
CODE_BEFORE
,
<<<'CODE_AFTER'
\Drupal::service('bar')->foo();
CODE_AFTER
,
[
new DrupalServiceRenameConfiguration(
'old',
'bar',
)
]
),
]);
}

}
30 changes: 0 additions & 30 deletions src/Rector/Deprecation/PathAliasManagerServiceNameRector.php

This file was deleted.

30 changes: 0 additions & 30 deletions src/Rector/Deprecation/PathAliasRepositoryRector.php

This file was deleted.

30 changes: 0 additions & 30 deletions src/Rector/Deprecation/PathAliasWhitelistServiceNameRector.php

This file was deleted.

30 changes: 0 additions & 30 deletions src/Rector/Deprecation/PathProcessorAliasServiceNameRector.php

This file was deleted.

30 changes: 0 additions & 30 deletions src/Rector/Deprecation/PathSubscriberServiceNameRector.php

This file was deleted.

Loading

0 comments on commit c8e5d87

Please sign in to comment.