From e356c847fa6d7b7830187fe957ec46fbabbe5b85 Mon Sep 17 00:00:00 2001 From: bjorn Date: Thu, 6 Jul 2023 20:54:49 +0200 Subject: [PATCH 1/9] Lets try --- fixtures/d8/rector_examples/user_password.php | 10 ++++ .../rector_examples_updated/user_password.php | 10 ++++ src/Contract/DrupalCoreRectorInterface.php | 12 +++++ src/Rector/AbstractDrupalCoreRector.php | 48 +++++++++++++++++++ .../Deprecation/Base/AbstractDrupalRector.php | 0 src/Rector/Deprecation/UserPasswordRector.php | 11 +++-- .../Drupal/Core/Utility/DeprecationHelper.php | 43 +++++++++++++++++ stubs/Drupal/Drupal.php | 9 ++++ .../AbstractDrupalCoreRectorTest.php | 11 +++++ .../UserPasswordRectorTest.php | 1 - .../fixture/user_password.php.inc | 21 ++++++-- 11 files changed, 169 insertions(+), 7 deletions(-) create mode 100644 fixtures/d8/rector_examples/user_password.php create mode 100644 fixtures/d8/rector_examples_updated/user_password.php create mode 100644 src/Contract/DrupalCoreRectorInterface.php create mode 100644 src/Rector/AbstractDrupalCoreRector.php create mode 100644 src/Rector/Deprecation/Base/AbstractDrupalRector.php create mode 100644 stubs/Drupal/Core/Utility/DeprecationHelper.php create mode 100644 stubs/Drupal/Drupal.php create mode 100644 tests/src/Rector/AbstractDrupalCoreRector/AbstractDrupalCoreRectorTest.php diff --git a/fixtures/d8/rector_examples/user_password.php b/fixtures/d8/rector_examples/user_password.php new file mode 100644 index 00000000..0e9fc8c9 --- /dev/null +++ b/fixtures/d8/rector_examples/user_password.php @@ -0,0 +1,10 @@ +getVersion(), '<')) { + return null; + } + $result = $this->doRefactor($node); + + if ($result === null) { + return $result; + } + $versionCompare = $this->nodeFactory->createFuncCall( + 'version_compare', + [ + $this->nodeFactory->createClassConstFetch(\Drupal::class, 'VERSION'), + $this->getVersion(), + '>=', + ] + ); + $if = new Node\Stmt\If_($versionCompare, [ + 'stmts' => [new Node\Stmt\Expression($result)], + 'else' => new Node\Stmt\Else_([ + new Node\Stmt\Expression($node) + ]), + ]); + $this->nodesToAddCollector->addNodeBeforeNode($if, $node); + return $result; + } + + /** + * Process Node of matched type + * @return Node|Node[]|null + */ + abstract protected function doRefactor(Node $node); + +} diff --git a/src/Rector/Deprecation/Base/AbstractDrupalRector.php b/src/Rector/Deprecation/Base/AbstractDrupalRector.php new file mode 100644 index 00000000..e69de29b diff --git a/src/Rector/Deprecation/UserPasswordRector.php b/src/Rector/Deprecation/UserPasswordRector.php index c698d882..e13763f9 100644 --- a/src/Rector/Deprecation/UserPasswordRector.php +++ b/src/Rector/Deprecation/UserPasswordRector.php @@ -2,12 +2,12 @@ namespace DrupalRector\Rector\Deprecation; +use DrupalRector\Rector\AbstractDrupalCoreRector; use PhpParser\Node; -use Rector\Core\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; -final class UserPasswordRector extends AbstractRector +final class UserPasswordRector extends AbstractDrupalCoreRector { /** * @inheritdoc @@ -39,10 +39,15 @@ public function getNodeTypes(): array ]; } + public function getVersion(): string + { + return '9.1.0'; + } + /** * @inheritdoc */ - public function refactor(Node $node): ?Node + public function doRefactor(Node $node): ?Node { assert($node instanceof Node\Expr\FuncCall); if ($this->getName($node->name) !== 'user_password') { diff --git a/stubs/Drupal/Core/Utility/DeprecationHelper.php b/stubs/Drupal/Core/Utility/DeprecationHelper.php new file mode 100644 index 00000000..f21e3a09 --- /dev/null +++ b/stubs/Drupal/Core/Utility/DeprecationHelper.php @@ -0,0 +1,43 @@ +=') ? $current() : $deprecated(); + } + +} diff --git a/stubs/Drupal/Drupal.php b/stubs/Drupal/Drupal.php new file mode 100644 index 00000000..4751c67f --- /dev/null +++ b/stubs/Drupal/Drupal.php @@ -0,0 +1,9 @@ +generate(); - $other_password = \Drupal::service('password_generator')->generate(8); + if (version_compare(\Drupal::VERSION, '9.1.0', '>=')) { + $password = \Drupal::service('password_generator')->generate(); + } + else { + $password = user_password(); + } + if (version_compare(\Drupal::VERSION, '9.1.0', '>=')) { + $other_password = \Drupal::service('password_generator')->generate(8); + } + else { + $other_password = user_password(8); + } $password_length = 12; - $last_password = \Drupal::service('password_generator')->generate($password_length); + if (version_compare(\Drupal::VERSION, '9.1.0', '>=')) { + $last_password = \Drupal::service('password_generator')->generate($password_length); + } + else { + $last_password = user_password($password_length); + } } ?> From dbc6dbb4711f4455c6b60c4d03a4887be8c3ee17 Mon Sep 17 00:00:00 2001 From: bjorn Date: Sun, 6 Aug 2023 21:50:40 +0200 Subject: [PATCH 2/9] Composer didnt like me? --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 06874e53..8a541b9f 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ "ast" ], "require": { - "rector/rector": "~0.17.0", + "rector/rector": "^0.17.0", "webflo/drupal-finder": "^1.2" }, "license": "MIT", @@ -72,7 +72,7 @@ "phpstan/phpstan": "^1.0", "phpstan/phpstan-deprecation-rules": "^1.0", "phpunit/phpunit": "^10.0", - "rector/rector-src": "dev-main", + "rector/rector-src": "^0.17.0", "symplify/vendor-patches": "^11.0", "symfony/yaml": "^5 || ^6" } From 505ad407aa79b5ae13ad3e6b7f292d88ca9f740b Mon Sep 17 00:00:00 2001 From: bjorn Date: Sun, 6 Aug 2023 21:51:28 +0200 Subject: [PATCH 3/9] Use DeprecationHelper to create a BC call on a CallLike --- src/Rector/AbstractDrupalCoreRector.php | 42 ++++++++++++------- .../fixture/user_password.php.inc | 33 +++++++-------- 2 files changed, 42 insertions(+), 33 deletions(-) diff --git a/src/Rector/AbstractDrupalCoreRector.php b/src/Rector/AbstractDrupalCoreRector.php index f95c050a..9c654d30 100644 --- a/src/Rector/AbstractDrupalCoreRector.php +++ b/src/Rector/AbstractDrupalCoreRector.php @@ -4,38 +4,39 @@ namespace DrupalRector\Rector; +use Drupal\Core\Utility\DeprecationHelper; use DrupalRector\Contract\DrupalCoreRectorInterface; use PhpParser\Node; +use PhpParser\NodeDumper; use Rector\Core\Rector\AbstractRector; +use Rector\NodeTypeResolver\Node\AttributeKey; +use Rector\Php72\NodeFactory\AnonymousFunctionFactory; abstract class AbstractDrupalCoreRector extends AbstractRector implements DrupalCoreRectorInterface { + public function __construct( + private readonly AnonymousFunctionFactory $anonymousFunctionFactory + ) { + } + public function refactor(Node $node) { if (version_compare(\Drupal::VERSION, $this->getVersion(), '<')) { return null; } + $result = $this->doRefactor($node); if ($result === null) { return $result; } - $versionCompare = $this->nodeFactory->createFuncCall( - 'version_compare', - [ - $this->nodeFactory->createClassConstFetch(\Drupal::class, 'VERSION'), - $this->getVersion(), - '>=', - ] - ); - $if = new Node\Stmt\If_($versionCompare, [ - 'stmts' => [new Node\Stmt\Expression($result)], - 'else' => new Node\Stmt\Else_([ - new Node\Stmt\Expression($node) - ]), - ]); - $this->nodesToAddCollector->addNodeBeforeNode($if, $node); + + if($node instanceof Node\Expr\CallLike && $result instanceof Node\Expr\CallLike) { + $bcCall = $this->addBackwardsCompatibleCall($node, $result); + return $bcCall; + } + return $result; } @@ -45,4 +46,15 @@ public function refactor(Node $node) */ abstract protected function doRefactor(Node $node); + private function addBackwardsCompatibleCall(Node\Expr\CallLike $node, Node\Expr\CallLike $result): Node\Expr\StaticCall + { + $clonedNode = clone $node; + return $this->nodeFactory->createStaticCall(DeprecationHelper::class, 'backwardsCompatibleCall', [ + $this->nodeFactory->createClassConstFetch(\Drupal::class, 'VERSION'), + $this->getVersion(), + $this->anonymousFunctionFactory->create([], [new Node\Stmt\Return_($clonedNode)], null), + $this->anonymousFunctionFactory->create([], [new Node\Stmt\Return_($result)], null), + ]); + } + } diff --git a/tests/src/Rector/Deprecation/UserPasswordRector/fixture/user_password.php.inc b/tests/src/Rector/Deprecation/UserPasswordRector/fixture/user_password.php.inc index c3d1d6fd..b4a664c0 100644 --- a/tests/src/Rector/Deprecation/UserPasswordRector/fixture/user_password.php.inc +++ b/tests/src/Rector/Deprecation/UserPasswordRector/fixture/user_password.php.inc @@ -11,24 +11,21 @@ function simple_example() { =')) { - $password = \Drupal::service('password_generator')->generate(); - } - else { - $password = user_password(); - } - if (version_compare(\Drupal::VERSION, '9.1.0', '>=')) { - $other_password = \Drupal::service('password_generator')->generate(8); - } - else { - $other_password = user_password(8); - } + $password = \Drupal\Core\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '9.1.0', function () { + return user_password(); + }, function () { + return \Drupal::service('password_generator')->generate(); + }); + $other_password = \Drupal\Core\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '9.1.0', function () { + return user_password(8); + }, function () { + return \Drupal::service('password_generator')->generate(8); + }); $password_length = 12; - if (version_compare(\Drupal::VERSION, '9.1.0', '>=')) { - $last_password = \Drupal::service('password_generator')->generate($password_length); - } - else { - $last_password = user_password($password_length); - } + $last_password = \Drupal\Core\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '9.1.0', function () use ($password_length) { + return user_password($password_length); + }, function () use ($password_length) { + return \Drupal::service('password_generator')->generate($password_length); + }); } ?> From 984ae9f23c378104fbe7ebb43d13ce67946a5122 Mon Sep 17 00:00:00 2001 From: bjorn Date: Sun, 6 Aug 2023 21:52:28 +0200 Subject: [PATCH 4/9] Cleanup --- src/Rector/AbstractDrupalCoreRector.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Rector/AbstractDrupalCoreRector.php b/src/Rector/AbstractDrupalCoreRector.php index 9c654d30..3c67b96a 100644 --- a/src/Rector/AbstractDrupalCoreRector.php +++ b/src/Rector/AbstractDrupalCoreRector.php @@ -7,14 +7,11 @@ use Drupal\Core\Utility\DeprecationHelper; use DrupalRector\Contract\DrupalCoreRectorInterface; use PhpParser\Node; -use PhpParser\NodeDumper; use Rector\Core\Rector\AbstractRector; -use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Php72\NodeFactory\AnonymousFunctionFactory; abstract class AbstractDrupalCoreRector extends AbstractRector implements DrupalCoreRectorInterface { - public function __construct( private readonly AnonymousFunctionFactory $anonymousFunctionFactory ) { @@ -33,8 +30,7 @@ public function refactor(Node $node) } if($node instanceof Node\Expr\CallLike && $result instanceof Node\Expr\CallLike) { - $bcCall = $this->addBackwardsCompatibleCall($node, $result); - return $bcCall; + return $this->createBcCallOnCallLike($node, $result); } return $result; @@ -46,7 +42,7 @@ public function refactor(Node $node) */ abstract protected function doRefactor(Node $node); - private function addBackwardsCompatibleCall(Node\Expr\CallLike $node, Node\Expr\CallLike $result): Node\Expr\StaticCall + private function createBcCallOnCallLike(Node\Expr\CallLike $node, Node\Expr\CallLike $result): Node\Expr\StaticCall { $clonedNode = clone $node; return $this->nodeFactory->createStaticCall(DeprecationHelper::class, 'backwardsCompatibleCall', [ From 853ca520d44bee527b46b21225c63d37f9213633 Mon Sep 17 00:00:00 2001 From: bjorn Date: Sun, 6 Aug 2023 21:56:04 +0200 Subject: [PATCH 5/9] Remove empty file --- .../AbstractDrupalCoreRectorTest.php | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 tests/src/Rector/AbstractDrupalCoreRector/AbstractDrupalCoreRectorTest.php diff --git a/tests/src/Rector/AbstractDrupalCoreRector/AbstractDrupalCoreRectorTest.php b/tests/src/Rector/AbstractDrupalCoreRector/AbstractDrupalCoreRectorTest.php deleted file mode 100644 index 58da1dca..00000000 --- a/tests/src/Rector/AbstractDrupalCoreRector/AbstractDrupalCoreRectorTest.php +++ /dev/null @@ -1,11 +0,0 @@ - Date: Sun, 6 Aug 2023 21:59:22 +0200 Subject: [PATCH 6/9] PHP 7.4 :/ --- src/Rector/AbstractDrupalCoreRector.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Rector/AbstractDrupalCoreRector.php b/src/Rector/AbstractDrupalCoreRector.php index 3c67b96a..e6e95f2e 100644 --- a/src/Rector/AbstractDrupalCoreRector.php +++ b/src/Rector/AbstractDrupalCoreRector.php @@ -12,9 +12,10 @@ abstract class AbstractDrupalCoreRector extends AbstractRector implements DrupalCoreRectorInterface { - public function __construct( - private readonly AnonymousFunctionFactory $anonymousFunctionFactory - ) { + private AnonymousFunctionFactory $anonymousFunctionFactory; + + public function __construct(AnonymousFunctionFactory $anonymousFunctionFactory) { + $this->anonymousFunctionFactory = $anonymousFunctionFactory; } public function refactor(Node $node) From 3ee5862bf3bfb9197fe554f9f2c71e69f5eb5738 Mon Sep 17 00:00:00 2001 From: bjorn Date: Mon, 7 Aug 2023 13:15:00 +0200 Subject: [PATCH 7/9] Move DeprecationHelper from Utility to Component namespace --- src/Rector/AbstractDrupalCoreRector.php | 2 +- .../Core/{Utility => Component}/DeprecationHelper.php | 4 ++-- .../UserPasswordRector/fixture/user_password.php.inc | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) rename stubs/Drupal/Core/{Utility => Component}/DeprecationHelper.php (93%) diff --git a/src/Rector/AbstractDrupalCoreRector.php b/src/Rector/AbstractDrupalCoreRector.php index e6e95f2e..2b841aad 100644 --- a/src/Rector/AbstractDrupalCoreRector.php +++ b/src/Rector/AbstractDrupalCoreRector.php @@ -4,7 +4,7 @@ namespace DrupalRector\Rector; -use Drupal\Core\Utility\DeprecationHelper; +use Drupal\Core\Component\DeprecationHelper; use DrupalRector\Contract\DrupalCoreRectorInterface; use PhpParser\Node; use Rector\Core\Rector\AbstractRector; diff --git a/stubs/Drupal/Core/Utility/DeprecationHelper.php b/stubs/Drupal/Core/Component/DeprecationHelper.php similarity index 93% rename from stubs/Drupal/Core/Utility/DeprecationHelper.php rename to stubs/Drupal/Core/Component/DeprecationHelper.php index f21e3a09..fe81241f 100644 --- a/stubs/Drupal/Core/Utility/DeprecationHelper.php +++ b/stubs/Drupal/Core/Component/DeprecationHelper.php @@ -2,9 +2,9 @@ declare(strict_types=1); -namespace Drupal\Core\Utility; +namespace Drupal\Component\Utility; -if (class_exists('Drupal\Core\Utility\DeprecationHelper')) { +if (class_exists('Drupal\Core\Component\DeprecationHelper')) { return; } diff --git a/tests/src/Rector/Deprecation/UserPasswordRector/fixture/user_password.php.inc b/tests/src/Rector/Deprecation/UserPasswordRector/fixture/user_password.php.inc index b4a664c0..d503e48a 100644 --- a/tests/src/Rector/Deprecation/UserPasswordRector/fixture/user_password.php.inc +++ b/tests/src/Rector/Deprecation/UserPasswordRector/fixture/user_password.php.inc @@ -11,18 +11,18 @@ function simple_example() { generate(); }); - $other_password = \Drupal\Core\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '9.1.0', function () { + $other_password = \Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '9.1.0', function () { return user_password(8); }, function () { return \Drupal::service('password_generator')->generate(8); }); $password_length = 12; - $last_password = \Drupal\Core\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '9.1.0', function () use ($password_length) { + $last_password = \Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '9.1.0', function () use ($password_length) { return user_password($password_length); }, function () use ($password_length) { return \Drupal::service('password_generator')->generate($password_length); From c39716340f441c0ab2da68a0f1bfd19ed87d9f99 Mon Sep 17 00:00:00 2001 From: bjorn Date: Mon, 7 Aug 2023 16:42:56 +0200 Subject: [PATCH 8/9] Refactor to ArrowFunction --- src/Rector/AbstractDrupalCoreRector.php | 14 +++------- .../{ => Utility}/DeprecationHelper.php | 0 .../fixture/user_password.php.inc | 26 +++++++------------ 3 files changed, 13 insertions(+), 27 deletions(-) rename stubs/Drupal/Core/Component/{ => Utility}/DeprecationHelper.php (100%) diff --git a/src/Rector/AbstractDrupalCoreRector.php b/src/Rector/AbstractDrupalCoreRector.php index 2b841aad..8689b83e 100644 --- a/src/Rector/AbstractDrupalCoreRector.php +++ b/src/Rector/AbstractDrupalCoreRector.php @@ -4,20 +4,14 @@ namespace DrupalRector\Rector; -use Drupal\Core\Component\DeprecationHelper; +use Drupal\Component\Utility\DeprecationHelper; use DrupalRector\Contract\DrupalCoreRectorInterface; use PhpParser\Node; +use PhpParser\Node\Expr\ArrowFunction; use Rector\Core\Rector\AbstractRector; -use Rector\Php72\NodeFactory\AnonymousFunctionFactory; abstract class AbstractDrupalCoreRector extends AbstractRector implements DrupalCoreRectorInterface { - private AnonymousFunctionFactory $anonymousFunctionFactory; - - public function __construct(AnonymousFunctionFactory $anonymousFunctionFactory) { - $this->anonymousFunctionFactory = $anonymousFunctionFactory; - } - public function refactor(Node $node) { if (version_compare(\Drupal::VERSION, $this->getVersion(), '<')) { @@ -49,8 +43,8 @@ private function createBcCallOnCallLike(Node\Expr\CallLike $node, Node\Expr\Call return $this->nodeFactory->createStaticCall(DeprecationHelper::class, 'backwardsCompatibleCall', [ $this->nodeFactory->createClassConstFetch(\Drupal::class, 'VERSION'), $this->getVersion(), - $this->anonymousFunctionFactory->create([], [new Node\Stmt\Return_($clonedNode)], null), - $this->anonymousFunctionFactory->create([], [new Node\Stmt\Return_($result)], null), + new ArrowFunction(['expr' => $clonedNode]), + new ArrowFunction(['expr' => $result]), ]); } diff --git a/stubs/Drupal/Core/Component/DeprecationHelper.php b/stubs/Drupal/Core/Component/Utility/DeprecationHelper.php similarity index 100% rename from stubs/Drupal/Core/Component/DeprecationHelper.php rename to stubs/Drupal/Core/Component/Utility/DeprecationHelper.php diff --git a/tests/src/Rector/Deprecation/UserPasswordRector/fixture/user_password.php.inc b/tests/src/Rector/Deprecation/UserPasswordRector/fixture/user_password.php.inc index d503e48a..9613afcb 100644 --- a/tests/src/Rector/Deprecation/UserPasswordRector/fixture/user_password.php.inc +++ b/tests/src/Rector/Deprecation/UserPasswordRector/fixture/user_password.php.inc @@ -1,31 +1,23 @@ ----- generate(); - }); - $other_password = \Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '9.1.0', function () { - return user_password(8); - }, function () { - return \Drupal::service('password_generator')->generate(8); - }); +function simple_example() +{ + $password = \Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '9.1.0', fn() => user_password(), fn() => \Drupal::service('password_generator')->generate()); + $other_password = \Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '9.1.0', fn() => user_password(8), fn() => \Drupal::service('password_generator')->generate(8)); $password_length = 12; - $last_password = \Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '9.1.0', function () use ($password_length) { - return user_password($password_length); - }, function () use ($password_length) { - return \Drupal::service('password_generator')->generate($password_length); - }); + $last_password = \Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '9.1.0', fn() => user_password($password_length), fn() => \Drupal::service('password_generator')->generate($password_length)); } + ?> From 3821e58fd7711e89a1dbd8085a4053f292138b49 Mon Sep 17 00:00:00 2001 From: bjorn Date: Tue, 8 Aug 2023 08:42:28 +0200 Subject: [PATCH 9/9] Revert "Composer didnt like me?" This reverts commit dbc6dbb4711f4455c6b60c4d03a4887be8c3ee17. --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 8a541b9f..06874e53 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ "ast" ], "require": { - "rector/rector": "^0.17.0", + "rector/rector": "~0.17.0", "webflo/drupal-finder": "^1.2" }, "license": "MIT", @@ -72,7 +72,7 @@ "phpstan/phpstan": "^1.0", "phpstan/phpstan-deprecation-rules": "^1.0", "phpunit/phpunit": "^10.0", - "rector/rector-src": "^0.17.0", + "rector/rector-src": "dev-main", "symplify/vendor-patches": "^11.0", "symfony/yaml": "^5 || ^6" }