Skip to content

Commit

Permalink
[Downgrade PHP 7.3] Add DowngradeIsCountableRector (#128)
Browse files Browse the repository at this point in the history
Co-authored-by: kaizen-ci <info@kaizen-ci.org>
  • Loading branch information
TomasVotruba and kaizen-ci committed May 31, 2021
1 parent 2fa48d7 commit 6fe5a9f
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/set/downgrade-php73.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
$services->set(DowngradeTrailingCommasInFunctionCallsRector::class);
$services->set(DowngradeArrayKeyFirstLastRector::class);
$services->set(SetCookieOptionsArrayToArgumentsRector::class);
$services->set(\Rector\DowngradePhp73\Rector\FuncCall\DowngradeIsCountableRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp73\Rector\FuncCall\DowngradeIsCountableRector;

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

final class DowngradeIsCountableRectorTest 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,17 @@
<?php

namespace Rector\Tests\DowngradePhp73\Rector\FuncCall\DowngradeIsCountableRector\Fixture;

$items = [];
return is_countable($items);

?>
-----
<?php

namespace Rector\Tests\DowngradePhp73\Rector\FuncCall\DowngradeIsCountableRector\Fixture;

$items = [];
return is_array($items) || $items instanceof \Countable;

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Rector\DowngradePhp73\Rector\FuncCall\DowngradeIsCountableRector;

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

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

declare(strict_types=1);

namespace Rector\DowngradePhp73\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Name\FullyQualified;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @changelog https://wiki.php.net/rfc/is-countable
*
* @see \Rector\Tests\DowngradePhp73\Rector\FuncCall\DowngradeIsCountableRector\DowngradeIsCountableRectorTest
*/
final class DowngradeIsCountableRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Downgrade is_countable() to former version', [
new CodeSample(
<<<'CODE_SAMPLE'
$items = [];
return is_countable($items);
CODE_SAMPLE

,
<<<'CODE_SAMPLE'
$items = [];
return is_array($items) || $items instanceof Countable;
CODE_SAMPLE
),
]);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}

/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node, 'is_countable')) {
return null;
}

$isArrayFuncCall = $this->nodeFactory->createFuncCall('is_array', $node->args);
$instanceof = new Instanceof_($node->args[0]->value, new FullyQualified('Countable'));

return new BooleanOr($isArrayFuncCall, $instanceof);
}
}

0 comments on commit 6fe5a9f

Please sign in to comment.