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
Show file tree
Hide file tree
Changes from all commits
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,63 @@ 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) {
return null;
} elseif ($stmt_expr_type->isAlwaysFalsy()) {
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
54 changes: 54 additions & 0 deletions tests/ConstantTest.php
Expand Up @@ -998,6 +998,60 @@ class X {
'$c' => 'int',
]
],
'bitwiseAndClassConstant' => [
'code' => '<?php
class X {
public const A = 1;
public const B = 2;
public const C = self::A & self::B;
}
$c = X::C;',
'assertions' => [
'$c' => 'int',
AndrolGenhald marked this conversation as resolved.
Show resolved Hide resolved
]
],
'bitwiseXorClassConstant' => [
'code' => '<?php
class X {
public const A = 1;
public const B = 2;
public const C = self::A ^ self::B;
}
$c = X::C;',
'assertions' => [
'$c' => 'int',
]
],
'bitwiseNotClassConstant' => [
'code' => '<?php
class X {
public const A = ~0;
public const B = ~"aa";
}
$a = X::A;
$b = X::B;',
'assertions' => [
'$a' => 'int',
'$b' => 'string',
]
],
'booleanNotClassConstant' => [
'code' => '<?php
class X {
public const A = !true;
public const B = !false;
}
$a = X::A;
$b = X::B;',
'assertions' => [
'$a' => 'false',
'$b' => 'true',
]
],
'protectedClassConstantAccessibilitySameNameInChild' => [
'code' => '<?php
class A {
Expand Down