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 | Fail | No diagnostics reported. Expectation diffLine 35: Expected 1 error(s) |
| phpstan | 2.2.11PHPStan - PHP Static Analysis Tool 2.2.11 | 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. [reported-from-level=7] regressions_class_string_negative_narrowing.php:35: Function Conformance\Tests\RegressionsClassStringNegativeNarrowing\stillAB() should return Conformance\Tests\RegressionsClassStringNegativeNarrowing\B but returns Conformance\Tests\RegressionsClassStringNegativeNarrowing\A|Conformance\Tests\RegressionsClassStringNegativeNarrowing\B. [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. regressions_class_string_negative_narrowing.php:35: Function Conformance\Tests\RegressionsClassStringNegativeNarrowing\stillAB() should return Conformance\Tests\RegressionsClassStringNegativeNarrowing\B but returns Conformance\Tests\RegressionsClassStringNegativeNarrowing\A|Conformance\Tests\RegressionsClassStringNegativeNarrowing\B. |
| 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] regressions_class_string_negative_narrowing.php:33: The declared return type 'Conformance\Tests\RegressionsClassStringNegativeNarrowing\B' for Conformance\Tests\RegressionsClassStringNegativeNarrowing\stillAB is incorrect, got 'Conformance\Tests\RegressionsClassStringNegativeNarrowing\A|Conformance\Tests\RegressionsClassStringNegativeNarrowing\B' [InvalidReturnType] regressions_class_string_negative_narrowing.php:35: 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\stillAB [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] regressions_class_string_negative_narrowing.php:35: The type Conformance\Tests\RegressionsClassStringNegativeNarrowing\A|Conformance\Tests\RegressionsClassStringNegativeNarrowing\B does not match the declared return type Conformance\Tests\RegressionsClassStringNegativeNarrowing\B [InvalidReturnStatement] |
| mago | 1.47.4mago 1.47.4 | Pass | regressions_class_string_negative_narrowing.php:35: Invalid return type for function `Conformance\Tests\RegressionsClassStringNegativeNarrowing\stillAB`: expected `Conformance\Tests\RegressionsClassStringNegativeNarrowing\B`, but found `Conformance\Tests\RegressionsClassStringNegativeNarrowing\A|Conformance\Tests\RegressionsClassStringNegativeNarrowing\B`. [invalid-return-statement] |
| mir | 0.72.1mir 0.72.1 | Fail | No diagnostics reported. Expectation diffLine 35: Expected 1 error(s) |
| phpantom | 0.10.0phpantom_lsp 0.10.0 | Pass | regressions_class_string_negative_narrowing.php:35: Return type Conformance\Tests\RegressionsClassStringNegativeNarrowing\A|Conformance\Tests\RegressionsClassStringNegativeNarrowing\B is incompatible with declared return type Conformance\Tests\RegressionsClassStringNegativeNarrowing\B [type_mismatch_return] |
| intelephense | 1.18.5intelephense 1.18.5 | Pass | regressions_class_string_negative_narrowing.php:30: Expected type 'Conformance\Tests\RegressionsClassStringNegativeNarrowing\B'. Found 'Conformance\Tests\RegressionsClassStringNegativeNarrowing\A|Conformance\Tests\RegressionsClassStringNegativeNarrowing\B'. [P1006] regressions_class_string_negative_narrowing.php:35: Expected type 'Conformance\Tests\RegressionsClassStringNegativeNarrowing\B'. Found 'Conformance\Tests\RegressionsClassStringNegativeNarrowing\A|Conformance\Tests\RegressionsClassStringNegativeNarrowing\B'. [P1006] |
| phpactor | 2026.07.22.0Phpactor 2026.07.22.0 | Fail | No diagnostics reported. Expectation diffLine 35: Expected 1 error(s) |
| phpy | 0.2.0phpy 0.2.0 | Fail | No diagnostics reported. Expectation diffLine 35: Expected 1 error(s) |
| qodana | 262.9437.196Qodana 262.9437.196 | Fail | No diagnostics reported. Expectation diffLine 35: Expected 1 error(s) |
| noverify | 0.5.5NoVerify, version 0.5.5: built on: 2025.04.22 14:36:46 OS: r-kuchinskas arm64 Commit: 4774b82f8adc53fbef38f95e97af4b953b4d1529 | Fail | No diagnostics reported. Expectation diffLine 35: Expected 1 error(s) |
| steins | 0.1.6steins 0.1.6 (2026-08-25 revision c13b4c7) | Fail | No diagnostics reported. Expectation diffLine 35: Expected 1 error(s) |
| phpstan-strict | 2.2.11PHPStan - PHP Static Analysis Tool 2.2.11 | 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. regressions_class_string_negative_narrowing.php:35: Function Conformance\Tests\RegressionsClassStringNegativeNarrowing\stillAB() should return Conformance\Tests\RegressionsClassStringNegativeNarrowing\B but returns Conformance\Tests\RegressionsClassStringNegativeNarrowing\A|Conformance\Tests\RegressionsClassStringNegativeNarrowing\B. |
| 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] regressions_class_string_negative_narrowing.php:33: The declared return type 'Conformance\Tests\RegressionsClassStringNegativeNarrowing\B' for Conformance\Tests\RegressionsClassStringNegativeNarrowing\stillAB is incorrect, got 'Conformance\Tests\RegressionsClassStringNegativeNarrowing\A|Conformance\Tests\RegressionsClassStringNegativeNarrowing\B' [InvalidReturnType] regressions_class_string_negative_narrowing.php:35: 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\stillAB [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 // E<pzoom>: same return-type mismatch as Psalm // E<intelephense>: same A|B on the return. Mago narrows to B and stays clean. Phan is silent on the unguarded return too, so it never checked
}
function stillAB(A|B $x): B // E<psalm>: InvalidReturnType on the unguarded return, same finding as fromClassString
{
return $x; // E: A|B is not B — a tool that never checks return types would also stay silent on the guarded return
}