← All results

Assertion target $this->method()

Group: Advanced PHPDoc types · Category: ecosystem · File: phpdoc_advanced_psalm_assert_this_method.php

Psalm documents that asserting a method return only holds when the class is @psalm-immutable (or memoizeMethodCallResults is on). This file uses the immutable annotation; the suite config does not enable memoization. References: - Psalm adding_assertions.md: $this->getException() + @psalm-immutable - PHPStan narrowing-types.md: returned values from other methods

Analyzer results

AnalyzerVersionResultDiagnostics
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.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
phpstan 2.3.7PHPStan - PHP Static Analysis Tool 2.3.7 Enforced reported Lv.4+
phpdoc_advanced_psalm_assert_this_method.php:48: Strict comparison using === between Exception and null will always evaluate to false. [reported-from-level=4]
With strict-rules
phpdoc_advanced_psalm_assert_this_method.php:48: Strict comparison using === between Exception and null will always evaluate to false.
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
phpstan-strict 2.3.7PHPStan - PHP Static Analysis Tool 2.3.7 Enforced
phpdoc_advanced_psalm_assert_this_method.php:48: Strict comparison using === between Exception and null will always evaluate to false.
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
psalm 6.21.4Psalm 6.21.4@72023213487628df0bc5b2e02d68cbac614b7ef5 Enforced
phpdoc_advanced_psalm_assert_this_method.php:48: Exception does not contain null [TypeDoesNotContainNull]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
psalm 7 7.21.0Psalm 7.21.0@1a665f0114bd40cf12ad57a1a82066549d92f1b6 Enforced
phpdoc_advanced_psalm_assert_this_method.php:48: Exception does not contain null [TypeDoesNotContainNull]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
mago 1.61.0mago 1.61.0 Not enforced ⚠ 1 false positive
phpdoc_advanced_psalm_assert_this_method.php:49: Redundant `===` comparison: left-hand side is never identical to right-hand side. [redundant-comparison]
phpdoc_advanced_psalm_assert_this_method.php:49: Redundant ternary operator: condition is always falsy. [impossible-condition]
  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
  • False positives: line(s) 49
Expectation diff
Line 49: Unexpected errors ["Redundant `===` comparison: left-hand side is never identical to right-hand side. [redundant-comparison]","Redundant ternary operator: condition is always falsy. [impossible-condition]"]
mir 0.101.0mir 0.101.0 Not enforced
phpdoc_advanced_psalm_assert_this_method.php:46: PossiblyNullMethodCall: Cannot call method getMessage() on possibly null value [MIR0107]
  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
Expectation diff
Line 46: Expected valid value to be accepted, got ["PossiblyNullMethodCall: Cannot call method getMessage() on possibly null value [MIR0107]"]
phpantom 1.1.0phpantom_lsp 1.1.0 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
pun 0.4.3pun 0.4.3 Enforced
phpdoc_advanced_psalm_assert_this_method.php:48: Strict comparison using === between Exception and null will always evaluate to false. [identical.alwaysFalse]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
intelephense 1.18.5intelephense 1.18.5 Incidental (0/1)
phpdoc_advanced_psalm_assert_this_method.php:46: Expected type 'object'. Found 'null|Exception'. [P1006]
  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported — incidental, since values the type admits were also rejected on line(s) 46
Expectation diff
Line 46: Expected valid value to be accepted, got ["Expected type 'object'. Found 'null|Exception'. [P1006]"]
phpactor 2026.11.22.0Phpactor 2026.11.22.0 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
phpy 1.0.19566phpy 1.0.19566 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
qodana 263.7307.215PhpStorm 263.7307.215 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations 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

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
steins 0.1.8steins 0.1.8 (2026-09-21 revision 76306a9) Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\PhpdocAdvancedPsalmAssertThisMethod;

/**
 * Assertion target `$this->method()`.
 *
 * Psalm documents that asserting a method return only holds when the class
 * is `@psalm-immutable` (or `memoizeMethodCallResults` is on). This file
 * uses the immutable annotation; the suite config does not enable memoization.
 *
 * References:
 * - Psalm adding_assertions.md: `$this->getException()` + `@psalm-immutable`
 * - PHPStan narrowing-types.md: returned values from other methods
 */

/**
 * @psalm-immutable
 */
final class Result
{
    public function __construct(
        private readonly ?\Exception $exception = null,
    ) {
    }

    /**
     * @psalm-assert-if-true \Exception $this->getException()
     */
    public function hasException(): bool // T: @psalm-assert-if-true
    {
        return $this->exception !== null;
    }

    public function getException(): ?\Exception
    {
        return $this->exception;
    }
}

function example(Result $result): void
{
    if ($result->hasException()) {
        echo $result->getException()->getMessage(); // V

        echo $result->getException() === null ? 'null' : 'ex'; // E?: after assert, getException() is not null
    }
}