if ($x::class === A::class) { throw; }, the fall-through has excluded A (which is
final), so $x is B and return $x satisfies the : B return type. Analyzers diverge
on whether they narrow the *negative* branch of a ::class identity comparison.
PHPStan narrows the positive branch of such comparisons but not the negative one — even
though it narrows both directions for instanceof, is_a(), and gettype() — so it
keeps A|B on the fall-through and reports a false-positive return.type.
Source lead: Mago analyzer test narrow_non_final_class_string_match.| Analyzer | Version | Result | Diagnostics |
|---|---|---|---|
| phan | 6.0.7Phan 6.0.7 php-ast version 1.1.3 PHP version used to run Phan: 8.5.9 | Pass | No diagnostics reported. |
| phpstan | 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 | Pass reported Lv.7+ | regressions_class_string_negative_narrowing.php:30: Function Conformance\Tests\RegressionsClassStringNegativeNarrowing\fromClassString() should return Conformance\Tests\RegressionsClassStringNegativeNarrowing\B but returns Conformance\Tests\RegressionsClassStringNegativeNarrowing\A|Conformance\Tests\RegressionsClassStringNegativeNarrowing\B. [identifier=return.type] [reported-from-level=7] With strict-rulesregressions_class_string_negative_narrowing.php:30: Function Conformance\Tests\RegressionsClassStringNegativeNarrowing\fromClassString() should return Conformance\Tests\RegressionsClassStringNegativeNarrowing\B but returns Conformance\Tests\RegressionsClassStringNegativeNarrowing\A|Conformance\Tests\RegressionsClassStringNegativeNarrowing\B. [identifier=return.type] |
| psalm | 6.16.1Psalm 6.16.1@f1f5de594dc76faf8784e02d3dc4716c91c6f6ac next: 7.0.0-beta19 |
Pass (pzoom≠) | regressions_class_string_negative_narrowing.php:24: The declared return type 'Conformance\Tests\RegressionsClassStringNegativeNarrowing\B' for Conformance\Tests\RegressionsClassStringNegativeNarrowing\fromClassString is incorrect, got 'Conformance\Tests\RegressionsClassStringNegativeNarrowing\A|Conformance\Tests\RegressionsClassStringNegativeNarrowing\B' [InvalidReturnType] regressions_class_string_negative_narrowing.php:30: The inferred type 'Conformance\Tests\RegressionsClassStringNegativeNarrowing\A|Conformance\Tests\RegressionsClassStringNegativeNarrowing\B' does not match the declared return type 'Conformance\Tests\RegressionsClassStringNegativeNarrowing\B' for Conformance\Tests\RegressionsClassStringNegativeNarrowing\fromClassString [InvalidReturnStatement] pzoom (Psalm port)regressions_class_string_negative_narrowing.php:30: The type Conformance\Tests\RegressionsClassStringNegativeNarrowing\A|Conformance\Tests\RegressionsClassStringNegativeNarrowing\B does not match the declared return type Conformance\Tests\RegressionsClassStringNegativeNarrowing\B [InvalidReturnStatement] |
| mago | 1.46.0mago 1.46.0 | Pass | No diagnostics reported. |
| mir | 0.70.1mir 0.70.1 | Pass | No diagnostics reported. |
| phpantom | 0.9.0phpantom_lsp 0.9.0 | Pass | No diagnostics reported. |
| intelephense | 1.18.5intelephense 1.18.5 | Fail | regressions_class_string_negative_narrowing.php:30: Expected type 'Conformance\Tests\RegressionsClassStringNegativeNarrowing\B'. Found 'Conformance\Tests\RegressionsClassStringNegativeNarrowing\A|Conformance\Tests\RegressionsClassStringNegativeNarrowing\B'. [P1006] Expectation diffLine 30: Unexpected errors ["Expected type 'Conformance\\Tests\\RegressionsClassStringNegativeNarrowing\\B'. Found 'Conformance\\Tests\\RegressionsClassStringNegativeNarrowing\\A|Conformance\\Tests\\RegressionsClassStringNegativeNarrowing\\B'. [P1006]"] |
| phpy | 0.2.0phpy 0.2.0 | Pass | No diagnostics reported. |
| qodana | 262.8665.325Qodana 262.8665.325 | Pass | No diagnostics reported. |
| noverify | 0.5.5NoVerify, version 0.5.5: built on: 2025.04.22 14:36:46 OS: r-kuchinskas arm64 Commit: 4774b82f8adc53fbef38f95e97af4b953b4d1529 | Pass | No diagnostics reported. |
| steins | 0.1.5steins 0.1.5 (2026-08-12 revision 39b4cc1) | Pass | No diagnostics reported. |
| phpstan-strict | 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 | Pass | regressions_class_string_negative_narrowing.php:30: Function Conformance\Tests\RegressionsClassStringNegativeNarrowing\fromClassString() should return Conformance\Tests\RegressionsClassStringNegativeNarrowing\B but returns Conformance\Tests\RegressionsClassStringNegativeNarrowing\A|Conformance\Tests\RegressionsClassStringNegativeNarrowing\B. [identifier=return.type] |
| psalm-next | 7.0.0-beta19Psalm 7.0.0-beta19@7e751c06a756fa64dc4c759c09fe4a173afcb433 | Pass | regressions_class_string_negative_narrowing.php:24: The declared return type 'Conformance\Tests\RegressionsClassStringNegativeNarrowing\B' for Conformance\Tests\RegressionsClassStringNegativeNarrowing\fromClassString is incorrect, got 'Conformance\Tests\RegressionsClassStringNegativeNarrowing\A|Conformance\Tests\RegressionsClassStringNegativeNarrowing\B' [InvalidReturnType] regressions_class_string_negative_narrowing.php:30: The inferred type 'Conformance\Tests\RegressionsClassStringNegativeNarrowing\A|Conformance\Tests\RegressionsClassStringNegativeNarrowing\B' does not match the declared return type 'Conformance\Tests\RegressionsClassStringNegativeNarrowing\B' for Conformance\Tests\RegressionsClassStringNegativeNarrowing\fromClassString [InvalidReturnStatement] |
<?php
declare(strict_types=1);
namespace Conformance\Tests\RegressionsClassStringNegativeNarrowing;
/**
* Negative-branch narrowing via a class-string identity comparison.
*
* After `if ($x::class === A::class) { throw; }`, the fall-through has excluded A (which is
* final), so `$x` is `B` and `return $x` satisfies the `: B` return type. Analyzers diverge
* on whether they narrow the *negative* branch of a `::class` identity comparison.
*
* PHPStan narrows the positive branch of such comparisons but not the negative one — even
* though it narrows both directions for `instanceof`, `is_a()`, and `gettype()` — so it
* keeps `A|B` on the fall-through and reports a false-positive return.type.
*
* Source lead: Mago analyzer test `narrow_non_final_class_string_match`.
*/
final class A {}
final class B {}
function fromClassString(A|B $x): B // E<psalm>: declares the return invalid because $x is not narrowed to B (InvalidReturnType)
{
if ($x::class === A::class) {
throw new \LogicException('not A');
}
return $x; // E<phpstan>: negative branch of ::class === keeps A|B (final A not subtracted), so return.type fires // E<phpstan-strict>: same // E<psalm>: InvalidReturnStatement, $x stays A|B. Mago and Phan narrow to B and stay clean
}