![]() 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/cartforge.co/vendor/rector/rector/rules/CodeQuality/NodeManipulator/ |
<?php declare (strict_types=1); namespace Rector\CodeQuality\NodeManipulator; use PhpParser\Node\Expr; use PhpParser\Node\Expr\BinaryOp; use PhpParser\Node\Expr\BinaryOp\NotIdentical; use PhpParser\Node\Expr\BooleanNot; use PhpParser\Node\Expr\Cast\Bool_; use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; use PHPStan\Type\UnionType; use Rector\NodeTypeResolver\NodeTypeResolver; use Rector\NodeTypeResolver\PHPStan\Type\StaticTypeAnalyzer; use Rector\PhpParser\Node\NodeFactory; use Rector\PHPStanStaticTypeMapper\Utils\TypeUnwrapper; final class ExprBoolCaster { /** * @readonly * @var \Rector\NodeTypeResolver\NodeTypeResolver */ private $nodeTypeResolver; /** * @readonly * @var \Rector\PHPStanStaticTypeMapper\Utils\TypeUnwrapper */ private $typeUnwrapper; /** * @readonly * @var \Rector\NodeTypeResolver\PHPStan\Type\StaticTypeAnalyzer */ private $staticTypeAnalyzer; /** * @readonly * @var \Rector\PhpParser\Node\NodeFactory */ private $nodeFactory; public function __construct(NodeTypeResolver $nodeTypeResolver, TypeUnwrapper $typeUnwrapper, StaticTypeAnalyzer $staticTypeAnalyzer, NodeFactory $nodeFactory) { $this->nodeTypeResolver = $nodeTypeResolver; $this->typeUnwrapper = $typeUnwrapper; $this->staticTypeAnalyzer = $staticTypeAnalyzer; $this->nodeFactory = $nodeFactory; } public function boolCastOrNullCompareIfNeeded(Expr $expr) : Expr { $exprStaticType = $this->nodeTypeResolver->getType($expr); if (!TypeCombinator::containsNull($exprStaticType)) { if (!$this->isBoolCastNeeded($expr, $exprStaticType)) { return $expr; } return new Bool_($expr); } // if we remove null type, still has to be trueable if ($exprStaticType instanceof UnionType) { $unionTypeWithoutNullType = $this->typeUnwrapper->removeNullTypeFromUnionType($exprStaticType); if ($this->staticTypeAnalyzer->isAlwaysTruableType($unionTypeWithoutNullType)) { return new NotIdentical($expr, $this->nodeFactory->createNull()); } } elseif ($this->staticTypeAnalyzer->isAlwaysTruableType($exprStaticType)) { return new NotIdentical($expr, $this->nodeFactory->createNull()); } if (!$this->isBoolCastNeeded($expr, $exprStaticType)) { return $expr; } return new Bool_($expr); } private function isBoolCastNeeded(Expr $expr, Type $exprType) : bool { if ($expr instanceof BooleanNot) { return \false; } if ($exprType->isBoolean()->yes()) { return \false; } return !$expr instanceof BinaryOp; } }