Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
38 changes: 37 additions & 1 deletion 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 @@ -1949,7 +1950,20 @@ 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)) {

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 +1981,28 @@ public function resolveEqual(Expr\BinaryOp\Equal $expr, Scope $scope, TypeSpecif
);
}

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
148 changes: 148 additions & 0 deletions tests/PHPStan/Analyser/nsrt/equal-narrow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php declare(strict_types = 1);

// phpcs:disable SlevomatCodingStandard.ControlStructures.DisallowYodaComparison.DisallowedYodaComparison
// phpcs:disable SlevomatCodingStandard.Operators.DisallowEqualOperators.DisallowedEqualOperator
// phpcs:disable SlevomatCodingStandard.Operators.DisallowEqualOperators.DisallowedNotEqualOperator
// phpcs:disable Squiz.Functions.GlobalFunction.Found
// phpcs:disable Squiz.Strings.DoubleQuoteUsage.NotRequired

namespace EqualTypeNarrowing;

use function PHPStan\Testing\assertType;

/**
* @param 0|0.0|1|''|'0'|'x'|array{}|bool|object|null $x
* @param int|string|null $y
* @param mixed $z
*/
function doNull($x, $y, $z): void
{
if ($x == null) {
assertType("0|0.0|''|array{}|false|null", $x);
} else {
assertType("1|'0'|'x'|object|true", $x);
}
if (null != $x) {
assertType("1|'0'|'x'|object|true", $x);
} else {
assertType("0|0.0|''|array{}|false|null", $x);
}

if ($y == null) {
assertType("0|''|null", $y);
} else {
assertType("int<min, -1>|int<1, max>|non-empty-string", $y);
}

if ($z == null) {
assertType("0|0.0|''|array{}|false|null", $z);
} else {
assertType("mixed~0|0.0|''|array{}|false|null", $z);
}
}

/**
* @param 0|0.0|1|''|'0'|'x'|array{}|bool|object|null $x
* @param int|string|null $y
* @param mixed $z
*/
function doFalse($x, $y, $z): void
{
if ($x == false) {
assertType("0|0.0|''|'0'|array{}|false|null", $x);
} else {
assertType("1|'x'|object|true", $x);
}
if (false != $x) {
assertType("1|'x'|object|true", $x);
} else {
assertType("0|0.0|''|'0'|array{}|false|null", $x);
}

if (!$x) {
assertType("0|0.0|''|'0'|array{}|false|null", $x);
} else {
assertType("1|'x'|object|true", $x);
}

if ($y == false) {
assertType("0|''|'0'|null", $y);
} else {
assertType("int<min, -1>|int<1, max>|non-falsy-string", $y);
}

if ($z == false) {
assertType("0|0.0|''|'0'|array{}|false|null", $z);
} else {
assertType("mixed~0|0.0|''|'0'|array{}|false|null", $z);
}
}

/**
* @param 0|0.0|1|''|'0'|'x'|array{}|bool|object|null $x
* @param int|string|null $y
* @param mixed $z
*/
function doTrue($x, $y, $z): void
{
if ($x == true) {
assertType("1|'x'|object|true", $x);
} else {
assertType("0|0.0|''|'0'|array{}|false|null", $x);
}
if (true != $x) {
assertType("0|0.0|''|'0'|array{}|false|null", $x);
} else {
assertType("1|'x'|object|true", $x);
}

if ($x) {
assertType("1|'x'|object|true", $x);
} else {
assertType("0|0.0|''|'0'|array{}|false|null", $x);
}

if ($y == true) {
assertType("int<min, -1>|int<1, max>|non-falsy-string", $y);
} else {
assertType("0|''|'0'|null", $y);
}

if ($z == true) {
assertType("mixed~0|0.0|''|'0'|array{}|false|null", $z);
} else {
assertType("0|0.0|''|'0'|array{}|false|null", $z);
}
}

/**
* @param 0|0.0|1|''|'0'|'x'|array{}|bool|object|null $x
* @param int|string|null $y
* @param mixed $z
*/
function doEmptyString($x, $y, $z): void
{
// PHP 7.x/8.x compatibility: Keep zero in both cases
if ($x == '') {
assertType("0|0.0|''|false|null", $x);
} else {
assertType("0|0.0|1|'0'|'x'|array{}|object|true", $x);
}
if ('' != $x) {
assertType("0|0.0|1|'0'|'x'|array{}|object|true", $x);
} else {
assertType("0|0.0|''|false|null", $x);
}

if ($y == '') {
assertType("0|''|null", $y);
} else {
assertType("int|non-empty-string", $y);
}

if ($z == '') {
assertType("0|0.0|''|false|null", $z);
} else {
assertType("mixed~''|false|null", $z);
}
}
Loading