-
-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DowngradePhp80] Add DowngradePhpTokenRector (#426)
Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
fb9ad83
commit 3b0e8b6
Showing
5 changed files
with
203 additions
and
4 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
33 changes: 33 additions & 0 deletions
33
.../DowngradePhp80/Rector/StaticCall/DowngradePhpTokenRector/DowngradePhpTokenRectorTest.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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DowngradePhp80\Rector\StaticCall\DowngradePhpTokenRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class DowngradePhpTokenRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @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 | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...tests/DowngradePhp80/Rector/StaticCall/DowngradePhpTokenRector/Fixture/some_class.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,27 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DowngradePhp80\Rector\StaticCall\DowngradePhpTokenRector\Fixture; | ||
|
||
$code = '<?php echo 1;'; | ||
$tokens = \PhpToken::tokenize($code); | ||
|
||
foreach ($tokens as $token) { | ||
$name = $token->getTokenName(); | ||
$text = $token->text; | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DowngradePhp80\Rector\StaticCall\DowngradePhpTokenRector\Fixture; | ||
|
||
$code = '<?php echo 1;'; | ||
$tokens = token_get_all($code); | ||
|
||
foreach ($tokens as $token) { | ||
$name = is_array($token) ? token_name($token[0]) : null; | ||
$text = is_array($token) ? $token[1] : $token; | ||
} | ||
|
||
?> |
12 changes: 12 additions & 0 deletions
12
...tests/DowngradePhp80/Rector/StaticCall/DowngradePhpTokenRector/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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\DowngradePhp80\Rector\StaticCall\DowngradePhpTokenRector; | ||
|
||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$services = $containerConfigurator->services(); | ||
$services->set(DowngradePhpTokenRector::class); | ||
}; |
127 changes: 127 additions & 0 deletions
127
rules/DowngradePhp80/Rector/StaticCall/DowngradePhpTokenRector.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,127 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DowngradePhp80\Rector\StaticCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Expr\ArrayDimFetch; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\PropertyFetch; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PhpParser\Node\Expr\Ternary; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Scalar\LNumber; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\DowngradePhp80\Rector\StaticCall\DowngradePhpTokenRector\DowngradePhpTokenRectorTest | ||
*/ | ||
final class DowngradePhpTokenRector extends AbstractRector | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private const PHP_TOKEN = 'PhpToken'; | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('"something()" will be renamed to "somethingElse()"', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
$tokens = \PhpToken::tokenize($code); | ||
foreach ($tokens as $phpToken) { | ||
$name = $phpToken->getTokenName(); | ||
$text = $phpToken->text; | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
$tokens = token_get_all($code); | ||
foreach ($tokens as $token) { | ||
$name = is_array($token) ? token_name($token[0]) : null; | ||
$text = is_array($token) ? $token[1] : $token; | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [StaticCall::class, MethodCall::class, PropertyFetch::class]; | ||
} | ||
|
||
/** | ||
* @param StaticCall|MethodCall|PropertyFetch $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($node instanceof StaticCall) { | ||
return $this->refactorStaticCall($node); | ||
} | ||
|
||
if ($node instanceof MethodCall) { | ||
return $this->refactorMethodCall($node); | ||
} | ||
|
||
return $this->refactorPropertyFetch($node); | ||
} | ||
|
||
private function refactorStaticCall(StaticCall $staticCall): ?FuncCall | ||
{ | ||
if (! $this->isObjectType($staticCall->class, new ObjectType(self::PHP_TOKEN))) { | ||
return null; | ||
} | ||
|
||
if (! $this->isName($staticCall->name, 'tokenize')) { | ||
return null; | ||
} | ||
|
||
return new FuncCall(new Name('token_get_all'), $staticCall->args); | ||
} | ||
|
||
private function refactorMethodCall(MethodCall $methodCall): ?Ternary | ||
{ | ||
if (! $this->isObjectType($methodCall->var, new ObjectType(self::PHP_TOKEN))) { | ||
return null; | ||
} | ||
|
||
if (! $this->isName($methodCall->name, 'getTokenName')) { | ||
return null; | ||
} | ||
|
||
$isArrayFuncCall = new FuncCall(new Name('is_array'), [new Arg($methodCall->var)]); | ||
$arrayDimFetch = new ArrayDimFetch($methodCall->var, new LNumber(0)); | ||
$tokenGetNameFuncCall = new FuncCall(new Name('token_name'), [new Arg($arrayDimFetch)]); | ||
|
||
return new Ternary($isArrayFuncCall, $tokenGetNameFuncCall, $this->nodeFactory->createNull()); | ||
} | ||
|
||
private function refactorPropertyFetch(PropertyFetch $propertyFetch): ?Ternary | ||
{ | ||
if (! $this->isObjectType($propertyFetch->var, new ObjectType(self::PHP_TOKEN))) { | ||
return null; | ||
} | ||
|
||
if (! $this->isName($propertyFetch->name, 'text')) { | ||
return null; | ||
} | ||
|
||
$isArrayFuncCall = new FuncCall(new Name('is_array'), [new Arg($propertyFetch->var)]); | ||
$arrayDimFetch = new ArrayDimFetch($propertyFetch->var, new LNumber(1)); | ||
|
||
return new Ternary($isArrayFuncCall, $arrayDimFetch, $propertyFetch->var); | ||
} | ||
} |