Skip to content

Commit

Permalink
[DowngradePhp80] Add DowngradePhpTokenRector (#426)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Jul 11, 2021
1 parent fb9ad83 commit 3b0e8b6
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Rector\Tests\CodeQuality\Rector\Array_\CallableThisArrayToAnonymousFunctionRector\Fixture;

final class StaticCall
final class SomeStaticCall
{
public function run(array $values)
{
usort($values, [StaticCall::class, 'compareSize']);
usort($values, [SomeStaticCall::class, 'compareSize']);

return $values;
}
Expand All @@ -23,12 +23,12 @@ final class StaticCall

namespace Rector\Tests\CodeQuality\Rector\Array_\CallableThisArrayToAnonymousFunctionRector\Fixture;

final class StaticCall
final class SomeStaticCall
{
public function run(array $values)
{
usort($values, function (int $first, $second) : bool {
return StaticCall::compareSize($first, $second);
return SomeStaticCall::compareSize($first, $second);
});

return $values;
Expand Down
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';
}
}
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;
}

?>
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 rules/DowngradePhp80/Rector/StaticCall/DowngradePhpTokenRector.php
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);
}
}

0 comments on commit 3b0e8b6

Please sign in to comment.