@psalm-assert !SomeObjectType $barWidget|Gadget, after the call only Gadget remains, so
instanceof Widget is dead. Tools that parse !Widget as an unknown type
(or ignore the tag) leave the union intact.
References:
- Psalm assertion_syntax.md: Negated assertions (!SomeObjectType)| Analyzer | Version | Result | Diagnostics |
|---|---|---|---|
| phan | 6.0.11Phan 6.0.11 php-ast version 1.1.3 PHP version used to run Phan: 8.5.12 | Not enforced | No diagnostics reported.
|
| phpstan | 2.3.7PHPStan - PHP Static Analysis Tool 2.3.7 | Enforced reported Lv.4+ | phpdoc_advanced_psalm_assert_negated_object.php:50: Instanceof between Conformance\Tests\PhpdocAdvancedPsalmAssertNegatedObject\Gadget and Conformance\Tests\PhpdocAdvancedPsalmAssertNegatedObject\Widget will always evaluate to false. [reported-from-level=4] With strict-rulesphpdoc_advanced_psalm_assert_negated_object.php:50: Instanceof between Conformance\Tests\PhpdocAdvancedPsalmAssertNegatedObject\Gadget and Conformance\Tests\PhpdocAdvancedPsalmAssertNegatedObject\Widget will always evaluate to false.
|
| phpstan-strict | 2.3.7PHPStan - PHP Static Analysis Tool 2.3.7 | Enforced | phpdoc_advanced_psalm_assert_negated_object.php:50: Instanceof between Conformance\Tests\PhpdocAdvancedPsalmAssertNegatedObject\Gadget and Conformance\Tests\PhpdocAdvancedPsalmAssertNegatedObject\Widget will always evaluate to false.
|
| psalm | 6.21.4Psalm 6.21.4@72023213487628df0bc5b2e02d68cbac614b7ef5 | Enforced (pzoom≠) | phpdoc_advanced_psalm_assert_negated_object.php:50: Cannot resolve types for $value - Conformance\Tests\PhpdocAdvancedPsalmAssertNegatedObject\Gadget does not contain Conformance\Tests\PhpdocAdvancedPsalmAssertNegatedObject\Widget [TypeDoesNotContainType] pzoom (Psalm port)No diagnostics from pzoom.
|
| psalm 7 | 7.21.0Psalm 7.21.0@1a665f0114bd40cf12ad57a1a82066549d92f1b6 | Enforced | phpdoc_advanced_psalm_assert_negated_object.php:50: Cannot resolve types for $value - Conformance\Tests\PhpdocAdvancedPsalmAssertNegatedObject\Gadget does not contain Conformance\Tests\PhpdocAdvancedPsalmAssertNegatedObject\Widget [TypeDoesNotContainType]
|
| mago | 1.61.0mago 1.61.0 | Not enforced ⚠ 1 false positive | phpdoc_advanced_psalm_assert_negated_object.php:51: Redundant ternary operator: condition is always falsy. [impossible-condition]
Expectation diffLine 51: Unexpected errors ["Redundant ternary operator: condition is always falsy. [impossible-condition]"] |
| mir | 0.101.0mir 0.101.0 | Enforced | phpdoc_advanced_psalm_assert_negated_object.php:50: RedundantCondition: Condition is always false, so the then branch is never reached [MIR0400]
|
| phpantom | 1.1.0phpantom_lsp 1.1.0 | Not enforced | No diagnostics reported.
|
| pun | 0.4.3pun 0.4.3 | Enforced | phpdoc_advanced_psalm_assert_negated_object.php:50: Instanceof between Conformance\Tests\PhpdocAdvancedPsalmAssertNegatedObject\Gadget and Conformance\Tests\PhpdocAdvancedPsalmAssertNegatedObject\Widget will always evaluate to false. [instanceof.alwaysFalse]
|
| intelephense | 1.18.5intelephense 1.18.5 | Incidental (0/1) | phpdoc_advanced_psalm_assert_negated_object.php:48: Expected type 'Conformance\Tests\PhpdocAdvancedPsalmAssertNegatedObject\Gadget'. Found 'Conformance\Tests\PhpdocAdvancedPsalmAssertNegatedObject\Widget|Conformance\Tests\PhpdocAdvancedPsalmAssertNegatedObject\Gadget'. [P1006]
Expectation diffLine 48: Expected valid value to be accepted, got ["Expected type 'Conformance\\Tests\\PhpdocAdvancedPsalmAssertNegatedObject\\Gadget'. Found 'Conformance\\Tests\\PhpdocAdvancedPsalmAssertNegatedObject\\Widget|Conformance\\Tests\\PhpdocAdvancedPsalmAssertNegatedObject\\Gadget'. [P1006]"] |
| phpactor | 2026.11.22.0Phpactor 2026.11.22.0 | Not enforced | No diagnostics reported.
|
| phpy | 1.0.19566phpy 1.0.19566 | Not enforced | No diagnostics reported.
|
| qodana | 263.7307.215PhpStorm 263.7307.215 | Not enforced | 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 | Not enforced | phpdoc_advanced_psalm_assert_negated_object.php:48: potentially not safe call in function Conformance\Tests\PhpdocAdvancedPsalmAssertNegatedObject\takesGadget signature of param value [notSafeCall]
Expectation diffLine 48: Expected valid value to be accepted, got ["potentially not safe call in function Conformance\\Tests\\PhpdocAdvancedPsalmAssertNegatedObject\\takesGadget signature of param value [notSafeCall]"] |
| steins | 0.1.8steins 0.1.8 (2026-09-21 revision 76306a9) | Not enforced | No diagnostics reported.
|
<?php
declare(strict_types=1);
namespace Conformance\Tests\PhpdocAdvancedPsalmAssertNegatedObject;
/**
* Negated object assertion `@psalm-assert !SomeObjectType $bar`.
*
* Starting from `Widget|Gadget`, after the call only `Gadget` remains, so
* `instanceof Widget` is dead. Tools that parse `!Widget` as an unknown type
* (or ignore the tag) leave the union intact.
*
* References:
* - Psalm assertion_syntax.md: Negated assertions (`!SomeObjectType`)
*/
final class Widget
{
}
final class Gadget
{
public function ping(): string
{
return 'p';
}
}
/**
* @psalm-assert !Widget $value
*/
function assertNotWidget(Widget|Gadget $value): void // T: @psalm-assert !Widget
{
if ($value instanceof Widget) {
throw new \InvalidArgumentException('must not be Widget');
}
}
function takesGadget(Gadget $value): void
{
}
function example(Widget|Gadget $value): void
{
assertNotWidget($value);
takesGadget($value); // V
echo $value instanceof Widget ? 'widget' : 'gadget'; // E?: after @psalm-assert !Widget, instanceof Widget is always false
}