diff --git a/config/drupal-8/drupal-8.8-deprecations.php b/config/drupal-8/drupal-8.8-deprecations.php index 307b0559..34f259c6 100644 --- a/config/drupal-8/drupal-8.8-deprecations.php +++ b/config/drupal-8/drupal-8.8-deprecations.php @@ -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, diff --git a/src/Rector/Deprecation/Base/DrupalServiceRenameBase.php b/src/Rector/Deprecation/Base/DrupalServiceRenameBase.php deleted file mode 100644 index 08eb6030..00000000 --- a/src/Rector/Deprecation/Base/DrupalServiceRenameBase.php +++ /dev/null @@ -1,47 +0,0 @@ -foo(); -CODE_BEFORE - , - <<<'CODE_AFTER' -\Drupal::service('bar')->foo(); -CODE_AFTER - ) - ]); - } - -} diff --git a/src/Rector/Deprecation/Base/StaticArgumentRenameBase.php b/src/Rector/Deprecation/Base/StaticArgumentRenameBase.php deleted file mode 100644 index 4f19ba40..00000000 --- a/src/Rector/Deprecation/Base/StaticArgumentRenameBase.php +++ /dev/null @@ -1,74 +0,0 @@ -getName($node->name) === $this->methodName && (string) $node->class === $this->fullyQualifiedClassName) { - - if (count($node->args) === 1) { - /* @var Node\Arg $argument */ - $argument = $node->args[0]; - - if ($argument->value instanceof Node\Scalar\String_ && $argument->value->value === $this->deprecatedArgument) { - $node->args[0] = new Node\Arg(new Node\Scalar\String_($this->argument)); - - return $node; - } - } - } - } - - return null; - } -} diff --git a/src/Rector/Deprecation/DrupalServiceRenameRector.php b/src/Rector/Deprecation/DrupalServiceRenameRector.php new file mode 100644 index 00000000..333f0c9e --- /dev/null +++ b/src/Rector/Deprecation/DrupalServiceRenameRector.php @@ -0,0 +1,80 @@ +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', + ) + ] + ), + ]); + } + +} diff --git a/src/Rector/Deprecation/PathAliasManagerServiceNameRector.php b/src/Rector/Deprecation/PathAliasManagerServiceNameRector.php deleted file mode 100644 index 0bd49fa9..00000000 --- a/src/Rector/Deprecation/PathAliasManagerServiceNameRector.php +++ /dev/null @@ -1,30 +0,0 @@ -deprecatedService = $deprecatedService; + $this->newService = $newService; + } + public function getNewService(): string { + return $this->newService; + } + + public function getDeprecatedService(): string { + return $this->deprecatedService; + } + +}