2021-01-29 16:24:20 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2021-01-29 16:24:20 +00:00
|
|
|
namespace Rector\Php73\Rector\BooleanOr;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
|
2021-02-28 07:47:48 +00:00
|
|
|
use PhpParser\Node\Name;
|
|
|
|
use PHPStan\Reflection\ReflectionProvider;
|
2021-01-29 16:24:20 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2021-01-30 21:16:37 +00:00
|
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
2021-01-29 16:24:20 +00:00
|
|
|
use Rector\Php71\IsArrayAndDualCheckToAble;
|
2021-09-24 14:11:08 +00:00
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2021-01-29 16:24:20 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php73\Rector\BinaryOr\IsCountableRector\IsCountableRectorTest
|
2021-01-29 16:24:20 +00:00
|
|
|
*/
|
2021-09-24 14:11:08 +00:00
|
|
|
final class IsCountableRector extends \Rector\Core\Rector\AbstractRector implements \Rector\VersionBonding\Contract\MinPhpVersionInterface
|
2021-01-29 16:24:20 +00:00
|
|
|
{
|
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Php71\IsArrayAndDualCheckToAble
|
2021-01-29 16:24:20 +00:00
|
|
|
*/
|
|
|
|
private $isArrayAndDualCheckToAble;
|
2021-02-28 07:47:48 +00:00
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @var \PHPStan\Reflection\ReflectionProvider
|
2021-02-28 07:47:48 +00:00
|
|
|
*/
|
|
|
|
private $reflectionProvider;
|
2021-05-10 22:23:08 +00:00
|
|
|
public function __construct(\Rector\Php71\IsArrayAndDualCheckToAble $isArrayAndDualCheckToAble, \PHPStan\Reflection\ReflectionProvider $reflectionProvider)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2021-01-29 16:24:20 +00:00
|
|
|
$this->isArrayAndDualCheckToAble = $isArrayAndDualCheckToAble;
|
2021-02-28 07:47:48 +00:00
|
|
|
$this->reflectionProvider = $reflectionProvider;
|
2021-01-29 16:24:20 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
2021-01-29 16:24:20 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Changes is_array + Countable check to is_countable', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
|
2021-01-29 16:24:20 +00:00
|
|
|
is_array($foo) || $foo instanceof Countable;
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2021-01-29 16:24:20 +00:00
|
|
|
is_countable($foo);
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2021-01-29 16:24:20 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2021-01-29 16:24:20 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2021-01-29 16:24:20 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
return [\PhpParser\Node\Expr\BinaryOp\BooleanOr::class];
|
2021-01-29 16:24:20 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-07-05 22:50:18 +00:00
|
|
|
* @param BooleanOr $node
|
2021-01-29 16:24:20 +00:00
|
|
|
*/
|
2021-07-05 22:50:18 +00:00
|
|
|
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
2021-01-29 16:24:20 +00:00
|
|
|
{
|
|
|
|
if ($this->shouldSkip()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->isArrayAndDualCheckToAble->processBooleanOr($node, 'Countable', 'is_countable') ?: $node;
|
|
|
|
}
|
2021-07-21 09:35:57 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
|
|
|
return \Rector\Core\ValueObject\PhpVersionFeature::IS_COUNTABLE;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
private function shouldSkip() : bool
|
2021-01-29 16:24:20 +00:00
|
|
|
{
|
2021-07-21 09:35:57 +00:00
|
|
|
return !$this->reflectionProvider->hasFunction(new \PhpParser\Node\Name('is_countable'), null);
|
2021-01-29 16:24:20 +00:00
|
|
|
}
|
|
|
|
}
|