@phpstan-all-methods-impureif ($coin->flip() !== null), a second $coin->flip() is still
?string rather than narrowed to string. Tools that ignore the tag keep
treating non-void methods as pure by default and accept the second call as
string.
Available since PHPStan 2.1.39. The obsolete @phpstan-all-methods-are-impure
spelling is rejected as an unknown tag and is not tested here.
References:
- PHPStan phpdocs-basics: Impure functions / all-methods-impure
- PHPStan blog: Remembering and forgetting returned values| 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 | Recognized (no probes) | phpdoc_advanced_vendor_prefixed_all_methods_impure_phpstan.php:44: Argument 1 ($string) is $coin->flip() of type ?string but \strlen() takes string (expected type to be non-nullable) [PhanTypeMismatchArgumentNullableInternal]
|
| phpstan | 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 | Enforced reported Lv.8+ | phpdoc_advanced_vendor_prefixed_all_methods_impure_phpstan.php:44: Parameter #1 $string of function strlen expects string, string|null given. [identifier=argument.type] [reported-from-level=8] With strict-rulesphpdoc_advanced_vendor_prefixed_all_methods_impure_phpstan.php:44: Parameter #1 $string of function strlen expects string, string|null given. [identifier=argument.type]
|
| psalm | 6.16.1Psalm 6.16.1@f1f5de594dc76faf8784e02d3dc4716c91c6f6ac next: 7.0.0-beta19 |
Recognized (no probes) | phpdoc_advanced_vendor_prefixed_all_methods_impure_phpstan.php:44: Argument 1 of strlen cannot be null, possibly null value provided [PossiblyNullArgument]
|
| mago | 1.46.0mago 1.46.0 | Recognized (no probes) | phpdoc_advanced_vendor_prefixed_all_methods_impure_phpstan.php:44: Argument #1 of function `strlen` is possibly `null`, but parameter type `string` does not accept it. [possibly-null-argument]
|
| mir | 0.70.1mir 0.70.1 | Recognized (no probes) | No diagnostics reported.
|
| phpantom | 0.9.0phpantom_lsp 0.9.0 | Recognized (no probes) | No diagnostics reported.
|
| intelephense | 1.18.5intelephense 1.18.5 | Recognized (no probes) | phpdoc_advanced_vendor_prefixed_all_methods_impure_phpstan.php:44: Expected type 'string'. Found 'null|string'. [P1006]
|
| phpy | 0.2.0phpy 0.2.0 | Recognized (no probes) | No diagnostics reported.
|
| qodana | 262.8665.325Qodana 262.8665.325 | Recognized (no probes) | 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 | Recognized (no probes) | No diagnostics reported.
|
| steins | 0.1.5steins 0.1.5 (2026-08-12 revision 39b4cc1) | Recognized (no probes) | No diagnostics reported.
|
| phpstan-strict | 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 | Enforced | phpdoc_advanced_vendor_prefixed_all_methods_impure_phpstan.php:44: Parameter #1 $string of function strlen expects string, string|null given. [identifier=argument.type]
|
| psalm-next | 7.0.0-beta19Psalm 7.0.0-beta19@7e751c06a756fa64dc4c759c09fe4a173afcb433 | Recognized (no probes) | phpdoc_advanced_vendor_prefixed_all_methods_impure_phpstan.php:44: Argument 1 of strlen cannot be null, possibly null value provided [PossiblyNullArgument]
|
<?php
declare(strict_types=1);
namespace Conformance\Tests\PhpdocAdvancedVendorPrefixedAllMethodsImpurePhpStan;
/**
* Cross-tool handling of `@phpstan-all-methods-impure`
*
* Class-level impurity: method return values are not remembered across calls.
* After `if ($coin->flip() !== null)`, a second `$coin->flip()` is still
* `?string` rather than narrowed to `string`. Tools that ignore the tag keep
* treating non-void methods as pure by default and accept the second call as
* `string`.
*
* Available since PHPStan 2.1.39. The obsolete `@phpstan-all-methods-are-impure`
* spelling is rejected as an unknown tag and is not tested here.
*
* References:
* - PHPStan phpdocs-basics: Impure functions / all-methods-impure
* - PHPStan blog: Remembering and forgetting returned values
*/
/**
* @phpstan-all-methods-impure
*/
final class Coin // T: @phpstan-all-methods-impure
{
public function flip(): ?string
{
return \rand(0, 1) === 1 ? 'heads' : null;
}
}
function demo(Coin $coin): void
{
if ($coin->flip() !== null) {
// Under the class-level impure claim 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($coin->flip()); // E?<phpstan> // E?<phpstan-strict> // E?[noise]
}
}