Skip to content

Add basic type narrowing in the loose equality with the empty string #3304

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

Merged
merged 6 commits into from
Nov 16, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 87 additions & 6 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ConstantScalarType;
Expand Down Expand Up @@ -1610,7 +1611,7 @@ private function processBooleanNotSureConditionalTypes(Scope $scope, SpecifiedTy
}

/**
* @return array{Expr, ConstantScalarType}|null
* @return array{Expr, ConstantScalarType, Type}|null
*/
private function findTypeExpressionsFromBinaryOperation(Scope $scope, Node\Expr\BinaryOp $binaryOperation): ?array
{
Expand All @@ -1632,13 +1633,13 @@ private function findTypeExpressionsFromBinaryOperation(Scope $scope, Node\Expr\
&& !$rightExpr instanceof ConstFetch
&& !$rightExpr instanceof ClassConstFetch
) {
return [$binaryOperation->right, $leftType];
return [$binaryOperation->right, $leftType, $rightType];
} elseif (
$rightType instanceof ConstantScalarType
&& !$leftExpr instanceof ConstFetch
&& !$leftExpr instanceof ClassConstFetch
) {
return [$binaryOperation->left, $rightType];
return [$binaryOperation->left, $rightType, $leftType];
}

return null;
Expand Down Expand Up @@ -1949,7 +1950,21 @@ public function resolveEqual(Expr\BinaryOp\Equal $expr, Scope $scope, TypeSpecif
if ($expressions !== null) {
$exprNode = $expressions[0];
$constantType = $expressions[1];
if (!$context->null() && ($constantType->getValue() === false || $constantType->getValue() === null)) {
$otherType = $expressions[2];

if (!$context->null() && $constantType->getValue() === null) {
$trueTypes = [
new NullType(),
new ConstantBooleanType(false),
new ConstantIntegerType(0),
new ConstantFloatType(0.0),
new ConstantStringType(''),
new ConstantArrayType([], []),
];
return $this->create($exprNode, new UnionType($trueTypes), $context, false, $scope, $rootExpr);
}

if (!$context->null() && $constantType->getValue() === false) {
return $this->specifyTypesInCondition(
$scope,
$exprNode,
Expand All @@ -1967,6 +1982,52 @@ public function resolveEqual(Expr\BinaryOp\Equal $expr, Scope $scope, TypeSpecif
);
}

if (!$context->null() && $constantType->getValue() === 0 && !$otherType->isInteger()->yes() && !$otherType->isBoolean()->yes()) {
/* There is a difference between php 7.x and 8.x on the equality
* behavior between zero and the empty string, so to be conservative
* we leave it untouched regardless of the language version */
if ($context->true()) {
$trueTypes = [
new NullType(),
new ConstantBooleanType(false),
new ConstantIntegerType(0),
new ConstantFloatType(0.0),
new StringType(),
];
} else {
$trueTypes = [
new NullType(),
new ConstantBooleanType(false),
new ConstantIntegerType(0),
new ConstantFloatType(0.0),
new ConstantStringType('0'),
];
}
return $this->create($exprNode, new UnionType($trueTypes), $context, false, $scope, $rootExpr);
}

if (!$context->null() && $constantType->getValue() === '') {
/* There is a difference between php 7.x and 8.x on the equality
* behavior between zero and the empty string, so to be conservative
* we leave it untouched regardless of the language version */
if ($context->true()) {
$trueTypes = [
new NullType(),
new ConstantBooleanType(false),
new ConstantIntegerType(0),
new ConstantFloatType(0.0),
new ConstantStringType(''),
];
} else {
$trueTypes = [
new NullType(),
new ConstantBooleanType(false),
new ConstantStringType(''),
];
}
return $this->create($exprNode, new UnionType($trueTypes), $context, false, $scope, $rootExpr);
}

if (
$exprNode instanceof FuncCall
&& $exprNode->name instanceof Name
Expand Down Expand Up @@ -2060,11 +2121,13 @@ public function resolveEqual(Expr\BinaryOp\Equal $expr, Scope $scope, TypeSpecif

public function resolveIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, TypeSpecifierContext $context, ?Expr $rootExpr): SpecifiedTypes
{
// Normalize to: fn() === expr
$leftExpr = $expr->left;
$rightExpr = $expr->right;
if ($rightExpr instanceof FuncCall && !$leftExpr instanceof FuncCall) {
[$leftExpr, $rightExpr] = [$rightExpr, $leftExpr];
}

$unwrappedLeftExpr = $leftExpr;
if ($leftExpr instanceof AlwaysRememberedExpr) {
$unwrappedLeftExpr = $leftExpr->getExpr();
Expand All @@ -2073,8 +2136,10 @@ public function resolveIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, Ty
if ($rightExpr instanceof AlwaysRememberedExpr) {
$unwrappedRightExpr = $rightExpr->getExpr();
}

$rightType = $scope->getType($rightExpr);

// (count($a) === $b)
if (
!$context->null()
&& $unwrappedLeftExpr instanceof FuncCall
Expand Down Expand Up @@ -2139,6 +2204,7 @@ public function resolveIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, Ty
}
}

// strlen($a) === $b
if (
!$context->null()
&& $unwrappedLeftExpr instanceof FuncCall
Expand Down Expand Up @@ -2175,6 +2241,7 @@ public function resolveIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, Ty
}
}

// preg_match($a) === $b
if (
$context->true()
&& $unwrappedLeftExpr instanceof FuncCall
Expand All @@ -2190,6 +2257,7 @@ public function resolveIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, Ty
);
}

// get_class($a) === 'Foo'
if (
$context->true()
&& $unwrappedLeftExpr instanceof FuncCall
Expand All @@ -2209,6 +2277,7 @@ public function resolveIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, Ty
}
}

// get_class($a) === 'Foo'
if (
$context->truthy()
&& $unwrappedLeftExpr instanceof FuncCall
Expand Down Expand Up @@ -2289,6 +2358,7 @@ public function resolveIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, Ty
}
}

// $a::class === 'Foo'
if (
$context->true() &&
$unwrappedLeftExpr instanceof ClassConstFetch &&
Expand All @@ -2311,6 +2381,8 @@ public function resolveIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, Ty
}

$leftType = $scope->getType($leftExpr);

// 'Foo' === $a::class
if (
$context->true() &&
$unwrappedRightExpr instanceof ClassConstFetch &&
Expand Down Expand Up @@ -2356,7 +2428,11 @@ public function resolveIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, Ty
$types = null;
if (
count($leftType->getFiniteTypes()) === 1
|| ($context->true() && $leftType->isConstantValue()->yes() && !$rightType->equals($leftType) && $rightType->isSuperTypeOf($leftType)->yes())
|| (
$context->true()
&& $leftType->isConstantValue()->yes()
&& !$rightType->equals($leftType)
&& $rightType->isSuperTypeOf($leftType)->yes())
) {
$types = $this->create(
$rightExpr,
Expand All @@ -2379,7 +2455,12 @@ public function resolveIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, Ty
}
if (
count($rightType->getFiniteTypes()) === 1
|| ($context->true() && $rightType->isConstantValue()->yes() && !$leftType->equals($rightType) && $leftType->isSuperTypeOf($rightType)->yes())
|| (
$context->true()
&& $rightType->isConstantValue()->yes()
&& !$leftType->equals($rightType)
&& $leftType->isSuperTypeOf($rightType)->yes()
)
) {
$leftTypes = $this->create(
$leftExpr,
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/TypeSpecifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,8 @@ public function dataCondition(): iterable
new Variable('foo'),
new Expr\ConstFetch(new Name('null')),
),
['$foo' => self::SURE_NOT_TRUTHY],
['$foo' => self::SURE_NOT_FALSEY],
['$foo' => '0|0.0|\'\'|array{}|false|null'],
['$foo' => '~0|0.0|\'\'|array{}|false|null'],
],
[
new Expr\BinaryOp\Identical(
Expand Down
Loading
Loading