-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #199 from claudiu-cristea/user_password
user_password() deprecation
- Loading branch information
Showing
5 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
|
||
} |
34 changes: 34 additions & 0 deletions
34
tests/src/Rector/Deprecation/UserPasswordRector/UserPasswordRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
tests/src/Rector/Deprecation/UserPasswordRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
19 changes: 19 additions & 0 deletions
19
tests/src/Rector/Deprecation/UserPasswordRector/fixture/user_password.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
?> |