Skip to content
Open
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
51 changes: 51 additions & 0 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -2934,6 +2934,21 @@ private function resolveNormalizedIdentical(Expr\BinaryOp\Identical $expr, Scope
}
}

// (cast)$expr === value - propagate narrowing to inner expression
if (
!$context->null()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be generalized for other casts?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests also confirmed passing — all 11708 tests green. Everything's been pushed.

&& $unwrappedLeftExpr instanceof Expr\Cast
) {
$castProducingType = $this->determineCastProducingType($unwrappedLeftExpr, $rightType);
if ($castProducingType !== null) {
$innerExpr = $unwrappedLeftExpr->expr;
$result = $this->create($leftExpr, $rightType, $context, $scope)->setRootExpr($expr);
return $result->unionWith(
$this->create($innerExpr, $castProducingType, $context, $scope)->setRootExpr($expr),
);
}
}

$expressions = $this->findTypeExpressionsFromBinaryOperation($scope, $expr);
if ($expressions !== null) {
$exprNode = $expressions[0];
Expand Down Expand Up @@ -3123,4 +3138,40 @@ private function resolveNormalizedIdentical(Expr\BinaryOp\Identical $expr, Scope
return (new SpecifiedTypes([], []))->setRootExpr($expr);
}

/**
* Given a cast expression and the value it's compared to,
* returns the union of types that produce that value when cast.
*/
private function determineCastProducingType(Expr\Cast $cast, Type $comparedType): ?Type
{
if ($cast instanceof Expr\Cast\String_) {
$constantStrings = $comparedType->getConstantStrings();
if (count($constantStrings) === 1 && $constantStrings[0]->getValue() === '') {
// Types that produce '' when cast to string: null, false, ''
return TypeCombinator::union(
new NullType(),
new ConstantBooleanType(false),
new ConstantStringType(''),
);
}
}

if ($cast instanceof Expr\Cast\Int_) {
$constantScalars = $comparedType->getConstantScalarValues();
if (count($constantScalars) === 1 && $constantScalars[0] === 0) {
// Types that produce 0 when cast to int: null, false, 0, 0.0, '', '0'
return TypeCombinator::union(
new NullType(),
new ConstantBooleanType(false),
new ConstantIntegerType(0),
new ConstantFloatType(0.0),
new ConstantStringType(''),
new ConstantStringType('0'),
);
}
}

return null;
}

}
75 changes: 75 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-8231.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug8231;

use function PHPStan\Testing\assertType;

function foo(string $x): void {}

function test(string|null $x): void {
if ((string)$x !== '') {
assertType('non-empty-string', $x);
foo($x);
}
}

function testIdentical(string|null $x): void {
if ((string)$x === '') {
assertType("''|null", $x);
} else {
assertType('non-empty-string', $x);
}
}

function testInt(int|null $x): void {
if ((string)$x !== '') {
assertType('int', $x);
}
}

function testIntString(int|string|null $x): void {
if ((string)$x !== '') {
assertType('int|non-empty-string', $x);
}
}

function testBool(bool|string|null $x): void {
if ((string)$x !== '') {
assertType('non-empty-string|true', $x);
}
}

// (int) cast narrowing
function testIntCast(int|null $x): void {
if ((int)$x !== 0) {
assertType('int<min, -1>|int<1, max>', $x);
}
}

function testIntCastIdentical(int|null $x): void {
if ((int)$x === 0) {
assertType('0|null', $x);
} else {
assertType('int<min, -1>|int<1, max>', $x);
}
}

function testIntCastWithString(int|string|null $x): void {
if ((int)$x !== 0) {
assertType("int<min, -1>|int<1, max>|non-falsy-string", $x);
}
}

function testIntCastWithFloat(float|null $x): void {
if ((int)$x !== 0) {
assertType('float', $x);
}
}

function testIntCastWithBool(bool|int|null $x): void {
if ((int)$x !== 0) {
assertType('int<min, -1>|int<1, max>|true', $x);
}
}
Loading