nodeFactory = $nodeFactory;
}
/**
* @return \PhpParser\Node\Expr\BinaryOp\Identical|\PhpParser\Node\Expr\BinaryOp\BooleanOr|\PhpParser\Node\Expr\BinaryOp\NotIdentical|\PhpParser\Node\Expr\BooleanNot|\PhpParser\Node\Expr\Instanceof_|\PhpParser\Node\Expr\BinaryOp\BooleanAnd|null|\PhpParser\Node\Expr\FuncCall
*/
public function createIdenticalFalsyCompare(Type $exprType, Expr $expr, bool $treatAsNonEmpty, bool $isOnlyString = \true)
{
$result = null;
if ($exprType->isString()->yes()) {
if ($treatAsNonEmpty || !$isOnlyString) {
return new Identical($expr, new String_(''));
}
$result = new BooleanOr(new Identical($expr, new String_('')), new Identical($expr, new String_('0')));
} elseif ($exprType->isInteger()->yes()) {
return new Identical($expr, new LNumber(0));
} elseif ($exprType->isBoolean()->yes()) {
return new Identical($expr, $this->nodeFactory->createFalse());
} elseif ($exprType->isArray()->yes()) {
return new Identical($expr, new Array_([]));
} elseif ($exprType instanceof NullType) {
return new Identical($expr, $this->nodeFactory->createNull());
} elseif (!$exprType instanceof UnionType) {
return null;
} else {
$result = $this->createTruthyFromUnionType($exprType, $expr, $treatAsNonEmpty, \false);
}
if ($result instanceof BooleanOr && $expr instanceof CallLike && $result->left instanceof Identical && $result->right instanceof Identical) {
return new FuncCall(new Name('in_array'), [new Arg($expr), new Arg(new Array_([new ArrayItem($result->left->right), new ArrayItem($result->right->right)])), new Arg(new ConstFetch(new Name('true')))]);
}
return $result;
}
/**
* @return \PhpParser\Node\Expr\BinaryOp\Identical|\PhpParser\Node\Expr\Instanceof_|\PhpParser\Node\Expr\BinaryOp\BooleanOr|\PhpParser\Node\Expr\BinaryOp\NotIdentical|\PhpParser\Node\Expr\BinaryOp\BooleanAnd|\PhpParser\Node\Expr\BooleanNot|null
*/
public function createNotIdenticalFalsyCompare(Type $exprType, Expr $expr, bool $treatAsNotEmpty, bool $isOnlyString = \true)
{
$result = null;
if ($exprType->isString()->yes()) {
if ($treatAsNotEmpty || !$isOnlyString) {
return new NotIdentical($expr, new String_(''));
}
$result = new BooleanAnd(new NotIdentical($expr, new String_('')), new NotIdentical($expr, new String_('0')));
} elseif ($exprType->isInteger()->yes()) {
return new NotIdentical($expr, new LNumber(0));
} elseif ($exprType->isArray()->yes()) {
return new NotIdentical($expr, new Array_([]));
} elseif (!$exprType instanceof UnionType) {
return null;
} else {
$result = $this->createFromUnionType($exprType, $expr, $treatAsNotEmpty, \false);
}
if ($result instanceof BooleanAnd && $expr instanceof CallLike && $result->left instanceof NotIdentical && $result->right instanceof NotIdentical) {
return new BooleanNot(new FuncCall(new Name('in_array'), [new Arg($expr), new Arg(new Array_([new ArrayItem($result->left->right), new ArrayItem($result->right->right)])), new Arg(new ConstFetch(new Name('true')))]));
}
return $result;
}
/**
* @return \PhpParser\Node\Expr\BinaryOp\Identical|\PhpParser\Node\Expr\Instanceof_|\PhpParser\Node\Expr\BinaryOp\BooleanAnd|null
*/
private function createFromUnionType(UnionType $unionType, Expr $expr, bool $treatAsNotEmpty, bool $isOnlyString)
{
$unionType = TypeCombinator::removeNull($unionType);
if ($unionType->isBoolean()->yes()) {
return new Identical($expr, $this->nodeFactory->createTrue());
}
if ($unionType instanceof TypeWithClassName) {
return new Instanceof_($expr, new FullyQualified($unionType->getClassName()));
}
$nullConstFetch = $this->nodeFactory->createNull();
$toNullNotIdentical = new NotIdentical($expr, $nullConstFetch);
if ($unionType instanceof UnionType) {
return $this->resolveFromCleanedNullUnionType($unionType, $expr, $treatAsNotEmpty);
}
$compareExpr = $this->createNotIdenticalFalsyCompare($unionType, $expr, $treatAsNotEmpty, $isOnlyString);
if (!$compareExpr instanceof Expr) {
return null;
}
if ($treatAsNotEmpty) {
return new BooleanAnd($toNullNotIdentical, $compareExpr);
}
if ($unionType->isString()->yes()) {
$booleanAnd = new BooleanAnd($toNullNotIdentical, $compareExpr);
return new BooleanAnd($booleanAnd, new NotIdentical($expr, new String_('0')));
}
return new BooleanAnd($toNullNotIdentical, $compareExpr);
}
private function resolveFromCleanedNullUnionType(UnionType $unionType, Expr $expr, bool $treatAsNotEmpty) : ?BooleanAnd
{
$compareExprs = $this->collectCompareExprs($unionType, $expr, $treatAsNotEmpty, \false);
return $this->createBooleanAnd($compareExprs);
}
/**
* @return array