![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/rector/rector/rules/CodingStyle/Rector/If_/ |
<?php declare (strict_types=1); namespace Rector\CodingStyle\Rector\If_; 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_; use PHPStan\Type\BooleanType; use PHPStan\Type\FloatType; use PHPStan\Type\IntegerType; use PHPStan\Type\MixedType; use PHPStan\Type\NullType; use PHPStan\Type\StringType; use PHPStan\Type\UnionType; use Rector\Core\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** * @see \Rector\Tests\CodingStyle\Rector\If_\NullableCompareToNullRector\NullableCompareToNullRectorTest */ final class NullableCompareToNullRector extends AbstractRector { public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Changes negate of empty comparison of nullable value to explicit === or !== compare', [new CodeSample(<<<'CODE_SAMPLE' /** @var stdClass|null $value */ if ($value) { } if (!$value) { } CODE_SAMPLE , <<<'CODE_SAMPLE' /** @var stdClass|null $value */ if ($value !== null) { } if ($value === null) { } CODE_SAMPLE )]); } /** * @return array<class-string<Node>> */ public function getNodeTypes() : array { return [If_::class]; } /** * @param If_ $node */ public function refactor(Node $node) : ?Node { if ($node->cond instanceof BooleanNot && $this->isNullableNonScalarType($node->cond->expr)) { $node->cond = new Identical($node->cond->expr, $this->nodeFactory->createNull()); return $node; } if ($this->isNullableNonScalarType($node->cond)) { $node->cond = new NotIdentical($node->cond, $this->nodeFactory->createNull()); return $node; } return null; } private function isNullableNonScalarType(Expr $expr) : bool { $staticType = $this->getType($expr); if ($staticType instanceof MixedType) { return \false; } if (!$staticType instanceof UnionType) { return \false; } // is non-nullable? if ($staticType->isSuperTypeOf(new NullType())->no()) { return \false; } // is array? foreach ($staticType->getTypes() as $subType) { if ($subType->isArray()->yes()) { return \false; } } // is string? if ($staticType->isSuperTypeOf(new StringType())->yes()) { return \false; } // is number? if ($staticType->isSuperTypeOf(new IntegerType())->yes()) { return \false; } // is bool? if ($staticType->isSuperTypeOf(new BooleanType())->yes()) { return \false; } return !$staticType->isSuperTypeOf(new FloatType())->yes(); } }