2019-10-13 05:59:52 +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\CodingStyle\Rector\If_;
|
2018-11-03 09:35:05 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Identical;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
|
|
|
|
use PhpParser\Node\Expr\BooleanNot;
|
|
|
|
use PhpParser\Node\Stmt\If_;
|
2023-11-06 09:13:25 +00:00
|
|
|
use PHPStan\Type\TypeCombinator;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PHPStan\Type\UnionType;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodingStyle\Rector\If_\NullableCompareToNullRector\NullableCompareToNullRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class NullableCompareToNullRector extends AbstractRector
|
2018-11-03 09:35:05 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-11-03 09:35:05 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Changes negate of empty comparison of nullable value to explicit === or !== compare', [new CodeSample(<<<'CODE_SAMPLE'
|
2018-11-03 09:35:05 +00:00
|
|
|
/** @var stdClass|null $value */
|
2019-11-05 12:22:53 +00:00
|
|
|
if ($value) {
|
2018-11-03 09:35:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$value) {
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-11-03 09:35:05 +00:00
|
|
|
/** @var stdClass|null $value */
|
|
|
|
if ($value !== null) {
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($value === null) {
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2018-11-03 09:35:05 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-11-03 09:35:05 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-11-03 09:35:05 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [If_::class];
|
2018-11-03 09:35:05 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param If_ $node
|
2018-11-03 09:35:05 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2018-11-03 09:35:05 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node->cond instanceof BooleanNot && $this->isNullableNonScalarType($node->cond->expr)) {
|
|
|
|
$node->cond = new Identical($node->cond->expr, $this->nodeFactory->createNull());
|
2018-11-03 09:35:05 +00:00
|
|
|
return $node;
|
|
|
|
}
|
2019-02-27 11:52:00 +00:00
|
|
|
if ($this->isNullableNonScalarType($node->cond)) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$node->cond = new NotIdentical($node->cond, $this->nodeFactory->createNull());
|
2018-11-03 09:35:05 +00:00
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function isNullableNonScalarType(Expr $expr) : bool
|
2019-02-27 11:52:00 +00:00
|
|
|
{
|
2023-11-06 09:13:25 +00:00
|
|
|
$nativeType = $this->nodeTypeResolver->getNativeType($expr);
|
|
|
|
// is non-nullable?
|
|
|
|
if (!TypeCombinator::containsNull($nativeType)) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2019-02-27 12:14:20 +00:00
|
|
|
}
|
2023-11-06 09:13:25 +00:00
|
|
|
if (!$nativeType instanceof UnionType) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2019-02-27 12:14:20 +00:00
|
|
|
}
|
|
|
|
// is array?
|
2023-11-06 09:13:25 +00:00
|
|
|
foreach ($nativeType->getTypes() as $subType) {
|
2022-11-15 15:22:43 +00:00
|
|
|
if ($subType->isArray()->yes()) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2019-02-27 11:52:00 +00:00
|
|
|
}
|
2019-02-27 12:14:20 +00:00
|
|
|
}
|
2023-11-06 09:13:25 +00:00
|
|
|
$nativeType = TypeCombinator::removeNull($nativeType);
|
2023-11-06 09:18:43 +00:00
|
|
|
return !$nativeType->isScalar()->yes();
|
2019-02-27 11:52:00 +00:00
|
|
|
}
|
2018-11-03 09:35:05 +00:00
|
|
|
}
|