Skip to content
Closed
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
48 changes: 48 additions & 0 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
48 changes: 48 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-11923.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php // lint >= 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),
};
}
Loading