-
-
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.
[Downgrade PHP 7.3] Add DowngradeIsCountableRector (#128)
Co-authored-by: kaizen-ci <info@kaizen-ci.org>
- Loading branch information
1 parent
2fa48d7
commit 6fe5a9f
Showing
5 changed files
with
126 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
33 changes: 33 additions & 0 deletions
33
...ngradePhp73/Rector/FuncCall/DowngradeIsCountableRector/DowngradeIsCountableRectorTest.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\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'; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...ests/DowngradePhp73/Rector/FuncCall/DowngradeIsCountableRector/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,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; | ||
|
||
?> |
12 changes: 12 additions & 0 deletions
12
...ests/DowngradePhp73/Rector/FuncCall/DowngradeIsCountableRector/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\DowngradePhp73\Rector\FuncCall\DowngradeIsCountableRector; | ||
|
||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$services = $containerConfigurator->services(); | ||
$services->set(DowngradeIsCountableRector::class); | ||
}; |
63 changes: 63 additions & 0 deletions
63
rules/DowngradePhp73/Rector/FuncCall/DowngradeIsCountableRector.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,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); | ||
} | ||
} |