mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-01 17:00:51 +00:00
af39f1cc41
56e13466c3
[CI] Enable zend.assertions=1 on CI (#3823)
56 lines
1.6 KiB
PHP
56 lines
1.6 KiB
PHP
<?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;
|
|
}
|
|
if (!isset($node->getArgs()[0])) {
|
|
return null;
|
|
}
|
|
$isArrayFuncCall = $this->nodeFactory->createFuncCall('is_array', $node->getArgs());
|
|
$instanceof = new Instanceof_($node->getArgs()[0]->value, new FullyQualified('Countable'));
|
|
return new BooleanOr($isArrayFuncCall, $instanceof);
|
|
}
|
|
}
|