From 7859a17259a1145a7a4cb09832cb0f8f60f784f8 Mon Sep 17 00:00:00 2001 From: bjorn Date: Sun, 17 Dec 2023 21:32:27 +0100 Subject: [PATCH 1/2] Add support for module_load_include into existing module_load_install rector. --- config/drupal-9/drupal-9.4-deprecations.php | 2 +- ...InstallRector.php => ModuleLoadRector.php} | 20 ++++++++- .../fixture/basic.php.inc | 25 ----------- .../ModuleLoadRectorTest.php} | 4 +- .../config/configured_rule.php | 4 +- .../ModuleLoadRector/fixture/basic.php.inc | 43 +++++++++++++++++++ 6 files changed, 66 insertions(+), 32 deletions(-) rename src/Drupal9/Rector/Deprecation/{ModuleLoadInstallRector.php => ModuleLoadRector.php} (64%) delete mode 100644 tests/src/Drupal9/Rector/Deprecation/ModuleLoadInstallRector/fixture/basic.php.inc rename tests/src/Drupal9/Rector/Deprecation/{ModuleLoadInstallRector/ModuleLoadInstallRectorTest.php => ModuleLoadRector/ModuleLoadRectorTest.php} (82%) rename tests/src/Drupal9/Rector/Deprecation/{ModuleLoadInstallRector => ModuleLoadRector}/config/configured_rule.php (54%) create mode 100644 tests/src/Drupal9/Rector/Deprecation/ModuleLoadRector/fixture/basic.php.inc diff --git a/config/drupal-9/drupal-9.4-deprecations.php b/config/drupal-9/drupal-9.4-deprecations.php index 16d66f83..8c5ab1c2 100644 --- a/config/drupal-9/drupal-9.4-deprecations.php +++ b/config/drupal-9/drupal-9.4-deprecations.php @@ -4,5 +4,5 @@ return static function (Rector\Config\RectorConfig $rectorConfig): void { // Change record https://www.drupal.org/node/3220952 - $rectorConfig->rule(\DrupalRector\Drupal9\Rector\Deprecation\ModuleLoadInstallRector::class); + $rectorConfig->rule(\DrupalRector\Drupal9\Rector\Deprecation\ModuleLoadRector::class); }; diff --git a/src/Drupal9/Rector/Deprecation/ModuleLoadInstallRector.php b/src/Drupal9/Rector/Deprecation/ModuleLoadRector.php similarity index 64% rename from src/Drupal9/Rector/Deprecation/ModuleLoadInstallRector.php rename to src/Drupal9/Rector/Deprecation/ModuleLoadRector.php index bffa01d9..866623ae 100644 --- a/src/Drupal9/Rector/Deprecation/ModuleLoadInstallRector.php +++ b/src/Drupal9/Rector/Deprecation/ModuleLoadRector.php @@ -12,7 +12,7 @@ /** * Replaces deprecated module_load_install call with ModuleHandler call. */ -class ModuleLoadInstallRector extends AbstractRector +class ModuleLoadRector extends AbstractRector { /** * {@inheritdoc} @@ -35,6 +35,12 @@ public function refactor(Node $node): ?Node\Expr\CallLike $args[] = new Node\Arg(new Node\Scalar\String_('install')); return $this->nodeFactory->createMethodCall($this->nodeFactory->createStaticCall('Drupal', 'moduleHandler'), 'loadInclude', $args); + } elseif ($this->getName($node->name) === 'module_load_include') { + $newArgs = $args = $node->getArgs(); + $newArgs[0] = $args[1]; + $newArgs[1] = $args[0]; + + return $this->nodeFactory->createMethodCall($this->nodeFactory->createStaticCall('Drupal', 'moduleHandler'), 'loadInclude', $newArgs); } return null; @@ -46,10 +52,20 @@ public function getRuleDefinition(): RuleDefinition new CodeSample( <<<'CODE_BEFORE' module_load_install('example'); +$type = 'install'; +$module = 'example'; +$name = 'name'; +module_load_include($type, $module, $name); +module_load_include($type, $module); CODE_BEFORE , <<<'CODE_AFTER' -\Drupal\Core\Extension\ModuleHandler::loadInclude('example', 'install'); +\Drupal::moduleHandler()->loadInclude('example', 'install'); +$type = 'install'; +$module = 'example'; +$name = 'name'; +\Drupal::moduleHandler()->loadInclude($module, $type, $name); +\Drupal::moduleHandler()->loadInclude($module, $type); CODE_AFTER ), ]); diff --git a/tests/src/Drupal9/Rector/Deprecation/ModuleLoadInstallRector/fixture/basic.php.inc b/tests/src/Drupal9/Rector/Deprecation/ModuleLoadInstallRector/fixture/basic.php.inc deleted file mode 100644 index 7857c2fc..00000000 --- a/tests/src/Drupal9/Rector/Deprecation/ModuleLoadInstallRector/fixture/basic.php.inc +++ /dev/null @@ -1,25 +0,0 @@ - ------ -loadInclude('example', 'install'); - - $module = 'simple'; - \Drupal::moduleHandler()->loadInclude($module, 'install'); -} -?> diff --git a/tests/src/Drupal9/Rector/Deprecation/ModuleLoadInstallRector/ModuleLoadInstallRectorTest.php b/tests/src/Drupal9/Rector/Deprecation/ModuleLoadRector/ModuleLoadRectorTest.php similarity index 82% rename from tests/src/Drupal9/Rector/Deprecation/ModuleLoadInstallRector/ModuleLoadInstallRectorTest.php rename to tests/src/Drupal9/Rector/Deprecation/ModuleLoadRector/ModuleLoadRectorTest.php index 37e36032..cebd2d9c 100644 --- a/tests/src/Drupal9/Rector/Deprecation/ModuleLoadInstallRector/ModuleLoadInstallRectorTest.php +++ b/tests/src/Drupal9/Rector/Deprecation/ModuleLoadRector/ModuleLoadRectorTest.php @@ -2,12 +2,12 @@ declare(strict_types=1); -namespace Drupal9\Rector\Deprecation\ModuleLoadInstallRector; +namespace Drupal9\Rector\Deprecation\ModuleLoadRector; use Iterator; use Rector\Testing\PHPUnit\AbstractRectorTestCase; -class ModuleLoadInstallRectorTest extends AbstractRectorTestCase +class ModuleLoadRectorTest extends AbstractRectorTestCase { /** * @covers ::refactor diff --git a/tests/src/Drupal9/Rector/Deprecation/ModuleLoadInstallRector/config/configured_rule.php b/tests/src/Drupal9/Rector/Deprecation/ModuleLoadRector/config/configured_rule.php similarity index 54% rename from tests/src/Drupal9/Rector/Deprecation/ModuleLoadInstallRector/config/configured_rule.php rename to tests/src/Drupal9/Rector/Deprecation/ModuleLoadRector/config/configured_rule.php index eab586ad..f9b9c03a 100644 --- a/tests/src/Drupal9/Rector/Deprecation/ModuleLoadInstallRector/config/configured_rule.php +++ b/tests/src/Drupal9/Rector/Deprecation/ModuleLoadRector/config/configured_rule.php @@ -2,10 +2,10 @@ declare(strict_types=1); -use DrupalRector\Drupal9\Rector\Deprecation\ModuleLoadInstallRector; +use DrupalRector\Drupal9\Rector\Deprecation\ModuleLoadRector; use DrupalRector\Tests\Rector\Deprecation\DeprecationBase; use Rector\Config\RectorConfig; return static function (RectorConfig $rectorConfig): void { - DeprecationBase::addClass(ModuleLoadInstallRector::class, $rectorConfig, false); + DeprecationBase::addClass(ModuleLoadRector::class, $rectorConfig, false); }; diff --git a/tests/src/Drupal9/Rector/Deprecation/ModuleLoadRector/fixture/basic.php.inc b/tests/src/Drupal9/Rector/Deprecation/ModuleLoadRector/fixture/basic.php.inc new file mode 100644 index 00000000..11609627 --- /dev/null +++ b/tests/src/Drupal9/Rector/Deprecation/ModuleLoadRector/fixture/basic.php.inc @@ -0,0 +1,43 @@ + +----- +loadInclude('example', 'install'); + + $module = 'simple'; + \Drupal::moduleHandler()->loadInclude($module, 'install'); + + $type = 'install'; + $module = 'example'; + $name = 'name'; + \Drupal::moduleHandler()->loadInclude($module, $type, $name); + \Drupal::moduleHandler()->loadInclude($module, $type); + + \Drupal::moduleHandler()->loadInclude('example', 'install', 'name'); + \Drupal::moduleHandler()->loadInclude('example', 'install'); +} +?> From 61d867db29783ac5ed3b150309180e79168ed1f5 Mon Sep 17 00:00:00 2001 From: bjorn Date: Sun, 17 Dec 2023 21:37:10 +0100 Subject: [PATCH 2/2] update docs --- docs/rules_overview.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/docs/rules_overview.md b/docs/rules_overview.md index 251bb3b1..f6580895 100644 --- a/docs/rules_overview.md +++ b/docs/rules_overview.md @@ -715,15 +715,22 @@ Fixes deprecated `AssertLegacyTrait::getRawContent()` calls
-### ModuleLoadInstallRector +### ModuleLoadRector Fixes deprecated `module_load_install()` calls -- class: [`DrupalRector\Drupal9\Rector\Deprecation\ModuleLoadInstallRector`](../src/Drupal9/Rector/Deprecation/ModuleLoadInstallRector.php) +- class: [`DrupalRector\Drupal9\Rector\Deprecation\ModuleLoadRector`](../src/Drupal9/Rector/Deprecation/ModuleLoadRector.php) ```diff -module_load_install('example'); -+\Drupal\Core\Extension\ModuleHandler::loadInclude('example', 'install'); ++\Drupal::moduleHandler()->loadInclude('example', 'install'); + $type = 'install'; + $module = 'example'; + $name = 'name'; +-module_load_include($type, $module, $name); +-module_load_include($type, $module); ++\Drupal::moduleHandler()->loadInclude($module, $type, $name); ++\Drupal::moduleHandler()->loadInclude($module, $type); ```