← All results

@phpstan-impure

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

PHPStan remembers return values of non-void functions by default. The tag opts a function out of that memory so a second call can still be nullable after a successful null check on the first call. References: - PHPStan phpdocs-basics: Impure functions - PHPStan blog: Remembering and forgetting returned values

Analyzer results

AnalyzerVersionResultDiagnostics
phan 6.0.7Phan 6.0.7 php-ast version 1.1.3 PHP version used to run Phan: 8.5.9 Recognized (no probes)
phpdoc_advanced_vendor_prefixed_impure_phpstan.php:35: Argument 1 ($string) is flip() of type ?string but \strlen() takes string (expected type to be non-nullable) [PhanTypeMismatchArgumentNullableInternal]
  • Recognition: spelling resolved
  • Enforcement: not measured — this test carries no rejection probes
phpstan 2.2.15PHPStan - PHP Static Analysis Tool 2.2.15 Enforced reported Lv.8+
phpdoc_advanced_vendor_prefixed_impure_phpstan.php:35: Parameter #1 $string of function strlen expects string, string|null given. [reported-from-level=8]
With strict-rules
phpdoc_advanced_vendor_prefixed_impure_phpstan.php:35: Parameter #1 $string of function strlen expects string, string|null given.
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
psalm 6.18.0Psalm 6.18.0@536dd6236b39a5115385b0307b42d6d1ad431a02
next: 7.0.0-beta22
Recognized (no probes)
phpdoc_advanced_vendor_prefixed_impure_phpstan.php:35: Argument 1 of strlen cannot be null, possibly null value provided [PossiblyNullArgument]
  • Recognition: spelling resolved
  • Enforcement: not measured — this test carries no rejection probes
mago 1.50.0mago 1.50.0 Recognized (no probes)
phpdoc_advanced_vendor_prefixed_impure_phpstan.php:35: Argument #1 of function `strlen` is possibly `null`, but parameter type `string` does not accept it. [possibly-null-argument]
  • Recognition: spelling resolved
  • Enforcement: not measured — this test carries no rejection probes
mir 0.78.0mir 0.78.0 Recognized (no probes)
phpdoc_advanced_vendor_prefixed_impure_phpstan.php:35: PossiblyNullArgument: Argument $string of strlen() might be null [MIR0104]
  • Recognition: spelling resolved
  • Enforcement: not measured — this test carries no rejection probes
phpantom 0.10.0phpantom_lsp 0.10.0 Recognized (no probes)

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: not measured — this test carries no rejection probes
intelephense 1.18.5intelephense 1.18.5 Recognized (no probes)
phpdoc_advanced_vendor_prefixed_impure_phpstan.php:35: Expected type 'string'. Found 'null|string'. [P1006]
  • Recognition: spelling resolved
  • Enforcement: not measured — this test carries no rejection probes
phpactor 2026.06.23.0Phpactor 2026.06.23.0 Recognized (no probes)

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: not measured — this test carries no rejection probes
phpy 1.0.19274phpy 1.0.19274 Recognized (no probes)

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: not measured — this test carries no rejection probes
qodana 262.9437.196Qodana 262.9437.196 Recognized (no probes)

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: not measured — this test carries no rejection probes
noverify 0.5.5NoVerify, version 0.5.5: built on: 2025.04.22 14:36:46 OS: r-kuchinskas arm64 Commit: 4774b82f8adc53fbef38f95e97af4b953b4d1529 Recognized (no probes)

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: not measured — this test carries no rejection probes
steins 0.1.8steins 0.1.8 (2026-09-21 revision 76306a9) Recognized (no probes)

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: not measured — this test carries no rejection probes
phpstan-strict 2.2.15PHPStan - PHP Static Analysis Tool 2.2.15 Enforced
phpdoc_advanced_vendor_prefixed_impure_phpstan.php:35: Parameter #1 $string of function strlen expects string, string|null given.
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
psalm-next 7.0.0-beta22Psalm 7.0.0-beta22@de27e5724ee5997a1e3baee4d394ca6f4e46eba0 Recognized (no probes)
phpdoc_advanced_vendor_prefixed_impure_phpstan.php:35: Argument 1 of strlen cannot be null, possibly null value provided [PossiblyNullArgument]
  • Recognition: spelling resolved
  • Enforcement: not measured — this test carries no rejection probes

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\PhpdocAdvancedVendorPrefixedImpurePhpStan;

/**
 * Cross-tool handling of `@phpstan-impure`.
 *
 * PHPStan remembers return values of non-void functions by default. The tag
 * opts a function out of that memory so a second call can still be nullable
 * after a successful null check on the first call.
 *
 * References:
 * - PHPStan phpdocs-basics: Impure functions
 * - PHPStan blog: Remembering and forgetting returned values
 */

/**
 * @phpstan-impure
 */
function flip(): ?string // T: @phpstan-impure
{
    return \rand(0, 1) === 1 ? 'heads' : null;
}

function demo(): void
{
    if (flip() !== null) {
        // Under @phpstan-impure the second call is still ?string, so strlen()
        // rejects it. Only PHPStan memoizes return values by default, so this is
        // the one tool the tag can be measured on: every other analyzer already
        // treats a second call as ?string regardless of the tag, so its
        // diagnostic here is baseline behaviour, not tag enforcement.
        echo \strlen(flip()); // E?<phpstan> // E?<phpstan-strict> // E?[noise]
    }
}