Skip to content

Commit

Permalink
Merge pull request #199 from claudiu-cristea/user_password
Browse files Browse the repository at this point in the history
user_password() deprecation
  • Loading branch information
agentrickard authored May 24, 2022
2 parents 350315d + 10af8e5 commit 7ee195e
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/drupal-9/drupal-9.1-deprecations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -108,4 +109,5 @@
$services->set(ConstructFieldXpathRector::class);
$services->set(GetRawContentRector::class);
$services->set(GetAllOptionsRector::class);
$services->set(UserPasswordRector::class);
};
64 changes: 64 additions & 0 deletions src/Rector/Deprecation/UserPasswordRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace DrupalRector\Rector\Deprecation;

use DrupalRector\Rector\Deprecation\Base\FunctionToServiceBase;
use PhpParser\Node;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class UserPasswordRector extends AbstractRector
{
/**
* @inheritdoc
*/
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Fixes deprecated user_password() calls',[
new CodeSample(
<<<'CODE_BEFORE'
$pass = user_password();
$shorter_pass = user_password(8);
CODE_BEFORE
,
<<<'CODE_AFTER'
$pass = \Drupal::service('password_generator')->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());
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types=1);

namespace DrupalRector\Tests\Rector\Deprecation\UserPasswordRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

class UserPasswordRectorTest extends AbstractRectorTestCase {

/**
* @covers ::refactor
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/fixture');
}

public function provideConfigFilePath(): string
{
// must be implemented
return __DIR__ . '/config/configured_rule.php';
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

use DrupalRector\Rector\Deprecation\UserPasswordRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(UserPasswordRector::class);
$parameters = $containerConfigurator->parameters();
$parameters->set('drupal_rector_notices_as_comments', true);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

function simple_example() {
$password = user_password();
$other_password = user_password(8);
$password_length = 12;
$last_password = user_password($password_length);
}
?>
-----
<?php

function simple_example() {
$password = \Drupal::service('password_generator')->generate();
$other_password = \Drupal::service('password_generator')->generate(8);
$password_length = 12;
$last_password = \Drupal::service('password_generator')->generate($password_length);
}
?>

0 comments on commit 7ee195e

Please sign in to comment.