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