Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New rector: Support for module_load_include deprecation (Drupal 9) #277

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/drupal-9/drupal-9.4-deprecations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
13 changes: 10 additions & 3 deletions docs/rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -715,15 +715,22 @@ Fixes deprecated `AssertLegacyTrait::getRawContent()` calls

<br>

### 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);
```

<br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* Replaces deprecated module_load_install call with ModuleHandler call.
*/
class ModuleLoadInstallRector extends AbstractRector
class ModuleLoadRector extends AbstractRector
{
/**
* {@inheritdoc}
Expand All @@ -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;
Expand All @@ -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
),
]);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* A simple example using the minimum number of arguments.
*/
function simple_example() {
module_load_install('example');

$module = 'simple';
module_load_install($module);

$type = 'install';
$module = 'example';
$name = 'name';
module_load_include($type, $module, $name);
module_load_include($type, $module);

module_load_include('install', 'example', 'name');
module_load_include('install', 'example');
}
?>
-----
<?php

/**
* A simple example using the minimum number of arguments.
*/
function simple_example() {
\Drupal::moduleHandler()->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');
}
?>