diff --git a/config/drupal-9/drupal-9.1-deprecations.php b/config/drupal-9/drupal-9.1-deprecations.php index 622d9e32..1777e87e 100644 --- a/config/drupal-9/drupal-9.1-deprecations.php +++ b/config/drupal-9/drupal-9.1-deprecations.php @@ -48,6 +48,7 @@ use DrupalRector\Rector\Deprecation\GetRawContentRector; use DrupalRector\Rector\Deprecation\PassRector; use DrupalRector\Rector\Deprecation\UiHelperTraitDrupalPostFormRector; +use DrupalRector\Rector\Deprecation\UserPasswordRector; use Rector\PHPUnit\Set\PHPUnitSetList; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; @@ -108,4 +109,5 @@ $services->set(ConstructFieldXpathRector::class); $services->set(GetRawContentRector::class); $services->set(GetAllOptionsRector::class); + $services->set(UserPasswordRector::class); }; diff --git a/src/Rector/Deprecation/UserPasswordRector.php b/src/Rector/Deprecation/UserPasswordRector.php new file mode 100644 index 00000000..7be443e1 --- /dev/null +++ b/src/Rector/Deprecation/UserPasswordRector.php @@ -0,0 +1,64 @@ +generate(); +$shorter_pass = \Drupal::service('password_generator')->generate(8); +CODE_AFTER + ) + ]); + } + + /** + * @inheritdoc + */ + public function getNodeTypes(): array + { + return [ + Node\Expr\FuncCall::class, + ]; + } + + /** + * @inheritdoc + */ + public function refactor(Node $node): ?Node + { + assert($node instanceof Node\Expr\FuncCall); + if ($this->getName($node->name) !== 'user_password') { + return null; + } + + $service = new Node\Expr\StaticCall( + new Node\Name\FullyQualified('Drupal'), + 'service', + [new Node\Arg(new Node\Scalar\String_('password_generator'))] + ); + $methodName = new Node\Identifier('generate'); + return new Node\Expr\MethodCall($service, $methodName, $node->getArgs()); + } + + +} diff --git a/tests/src/Rector/Deprecation/UserPasswordRector/UserPasswordRectorTest.php b/tests/src/Rector/Deprecation/UserPasswordRector/UserPasswordRectorTest.php new file mode 100644 index 00000000..700ca735 --- /dev/null +++ b/tests/src/Rector/Deprecation/UserPasswordRector/UserPasswordRectorTest.php @@ -0,0 +1,34 @@ +doTestFileInfo($fileInfo); + } + + /** + * @return Iterator + */ + public function provideData(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/fixture'); + } + + public function provideConfigFilePath(): string + { + // must be implemented + return __DIR__ . '/config/configured_rule.php'; + } + +} diff --git a/tests/src/Rector/Deprecation/UserPasswordRector/config/configured_rule.php b/tests/src/Rector/Deprecation/UserPasswordRector/config/configured_rule.php new file mode 100644 index 00000000..160d8cd2 --- /dev/null +++ b/tests/src/Rector/Deprecation/UserPasswordRector/config/configured_rule.php @@ -0,0 +1,11 @@ +services(); + $services->set(UserPasswordRector::class); + $parameters = $containerConfigurator->parameters(); + $parameters->set('drupal_rector_notices_as_comments', true); +}; diff --git a/tests/src/Rector/Deprecation/UserPasswordRector/fixture/user_password.php.inc b/tests/src/Rector/Deprecation/UserPasswordRector/fixture/user_password.php.inc new file mode 100644 index 00000000..7d816d4f --- /dev/null +++ b/tests/src/Rector/Deprecation/UserPasswordRector/fixture/user_password.php.inc @@ -0,0 +1,19 @@ + +----- +generate(); + $other_password = \Drupal::service('password_generator')->generate(8); + $password_length = 12; + $last_password = \Drupal::service('password_generator')->generate($password_length); +} +?>