Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BitwiseNot and BooleanNot operators to SimpleTypeInferer #8360

Merged
merged 3 commits into from Aug 4, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -203,6 +203,61 @@ public static function infer(
}
}

if ($stmt instanceof PhpParser\Node\Expr\BitwiseNot) {
$stmt_expr_type = self::infer(
$codebase,
$nodes,
$stmt->expr,
$aliases,
$file_source,
$existing_class_constants,
$fq_classlike_name
);

if ($stmt_expr_type === null) {
return null;
}

$invalidTypes = clone $stmt_expr_type;
$invalidTypes->removeType('string');
$invalidTypes->removeType('int');
$invalidTypes->removeType('float');
AndrolGenhald marked this conversation as resolved.
Show resolved Hide resolved

if (!$invalidTypes->isUnionEmpty()) {
return null;
}
AndrolGenhald marked this conversation as resolved.
Show resolved Hide resolved

$types = [];
if ($stmt_expr_type->hasString()) {
$types[] = Type::getString();
}
if ($stmt_expr_type->hasInt() || $stmt_expr_type->hasFloat()) {
$types[] = Type::getInt();
}

return $types ? Type::combineUnionTypeArray($types, null) : null;
}

if ($stmt instanceof PhpParser\Node\Expr\BooleanNot) {
$stmt_expr_type = self::infer(
$codebase,
$nodes,
$stmt->expr,
$aliases,
$file_source,
$existing_class_constants,
$fq_classlike_name
);

if ($stmt_expr_type === null || $stmt_expr_type->isAlwaysFalsy()) {
orklah marked this conversation as resolved.
Show resolved Hide resolved
return Type::getTrue();
} elseif ($stmt_expr_type->isAlwaysTruthy()) {
return Type::getFalse();
} else {
return Type::getBool();
}
}

if ($stmt instanceof PhpParser\Node\Expr\ConstFetch) {
$name = strtolower($stmt->name->parts[0]);
if ($name === 'false') {
Expand Down