diff --git a/src/Analyser/TypeSpecifier.php b/src/Analyser/TypeSpecifier.php index 0949e9eaf9e..ddb4a60c289 100644 --- a/src/Analyser/TypeSpecifier.php +++ b/src/Analyser/TypeSpecifier.php @@ -2400,6 +2400,54 @@ private function resolveNormalizedIdentical(Expr\BinaryOp\Identical $expr, Scope $rightType = $scope->getType($rightExpr); + // ($cond ? $trueExpr : $falseExpr) === $value + if ( + $context->true() + && $unwrappedLeftExpr instanceof Expr\Ternary + && $unwrappedLeftExpr->if !== null + ) { + $ternaryIf = $unwrappedLeftExpr->if; + $ternaryElse = $unwrappedLeftExpr->else; + $ternaryCond = $unwrappedLeftExpr->cond; + $falseBranchType = $scope->getType($ternaryElse); + // Evaluate true branch in scope where ternary condition is truthy, + // so that e.g. $request::class is evaluated with $request narrowed to non-null + $truthyScope = $scope->filterByTruthyValue($ternaryCond); + $trueBranchType = $truthyScope->getType($ternaryIf); + + if ($rightType->isSuperTypeOf($falseBranchType)->no()) { + // Only the true branch can produce a value === $rightType + // Therefore $cond is truthy AND $trueExpr === $value + $condTruthyTypes = $this->specifyTypesInCondition( + $scope, + $ternaryCond, + TypeSpecifierContext::createTruthy(), + ); + $trueNarrowingTypes = $this->resolveIdentical( + new Expr\BinaryOp\Identical($ternaryIf, $rightExpr), + $scope, + $context, + ); + return $condTruthyTypes->unionWith($trueNarrowingTypes)->setRootExpr($expr); + } + + if ($rightType->isSuperTypeOf($trueBranchType)->no()) { + // Only the false branch can produce a value === $rightType + // Therefore $cond is falsy AND $falseExpr === $value + $condFalsyTypes = $this->specifyTypesInCondition( + $scope, + $ternaryCond, + TypeSpecifierContext::createFalsey(), + ); + $falseNarrowingTypes = $this->resolveIdentical( + new Expr\BinaryOp\Identical($ternaryElse, $rightExpr), + $scope, + $context, + ); + return $condFalsyTypes->unionWith($falseNarrowingTypes)->setRootExpr($expr); + } + } + // (count($a) === $expr) if ( !$context->null() diff --git a/tests/PHPStan/Analyser/nsrt/bug-11923.php b/tests/PHPStan/Analyser/nsrt/bug-11923.php new file mode 100644 index 00000000000..150f1382026 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-11923.php @@ -0,0 +1,48 @@ += 8.2 + +declare(strict_types = 1); + +namespace Bug11923; + +use function PHPStan\Testing\assertType; + +final readonly class RequestA +{ + /** + * @param non-empty-string $phoneNumber + */ + public function __construct( + public string $phoneNumber, + public \DateTimeImmutable $birthAt, + ) { + } +} + +final readonly class RequestB +{ + /** + * @param non-empty-string $passport + */ + public function __construct( + public string $passport, + public \DateTimeImmutable $birthAt, + ) { + } +} + +function testNullableTernaryMatchSubject(?object $request): void +{ + match ($request ? $request::class : null) { + null => assertType('null', $request), + RequestA::class => assertType(RequestA::class, $request), + RequestB::class => assertType(RequestB::class, $request), + }; +} + +function testNonNullableMatchSubject(object $request): void +{ + match ($request::class) { + RequestA::class => assertType(RequestA::class, $request), + RequestB::class => assertType(RequestB::class, $request), + }; +}