@param-closure-this$this will be inside a closure argument when the callee
rebinds it. Honouring the tag makes $this->label resolve on Host.
Tools that do not model the tag typically reject $this in the closure as
out of scope (recorded as Fail / false positives on the valid access).
References:
- PHPStan phpdocs-basics: Callables / @param-closure-this| 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 | Not enforced | phpdoc_advanced_phpstan_param_closure_this.php:39: Variable $this is undeclared [PhanUndeclaredThis] phpdoc_advanced_phpstan_param_closure_this.php:43: Variable $this is undeclared [PhanUndeclaredThis]
|
| phpstan | 2.2.15PHPStan - PHP Static Analysis Tool 2.2.15 | Enforced reported Lv.0+ | phpdoc_advanced_phpstan_param_closure_this.php:43: Access to an undefined property Conformance\Tests\PhpdocAdvancedPhpstanParamClosureThis\Host::$missing. [reported-from-level=0] phpdoc_advanced_phpstan_param_closure_this.php:43: Parameter #1 (mixed) of echo cannot be converted to string. [reported-from-level=10] With strict-rulesphpdoc_advanced_phpstan_param_closure_this.php:43: Access to an undefined property Conformance\Tests\PhpdocAdvancedPhpstanParamClosureThis\Host::$missing. phpdoc_advanced_phpstan_param_closure_this.php:43: Parameter #1 (mixed) of echo cannot be converted to string.
|
| psalm | 6.18.0Psalm 6.18.0@536dd6236b39a5115385b0307b42d6d1ad431a02 next: 7.0.0-beta22 |
Not enforced | phpdoc_advanced_phpstan_param_closure_this.php:39: Invalid reference to $this in a non-class context [InvalidScope] phpdoc_advanced_phpstan_param_closure_this.php:39: Argument 1 of echo cannot be mixed, expecting string [MixedArgument] phpdoc_advanced_phpstan_param_closure_this.php:43: Invalid reference to $this in a non-class context [InvalidScope] phpdoc_advanced_phpstan_param_closure_this.php:43: Argument 1 of echo cannot be mixed, expecting string [MixedArgument]
|
| mago | 1.50.0mago 1.50.0 | Enforced | phpdoc_advanced_phpstan_param_closure_this.php:43: Property `$missing` does not exist on class `Conformance\Tests\PhpdocAdvancedPhpstanParamClosureThis\Host`. [non-existent-property]
|
| mir | 0.78.0mir 0.78.0 | Recognized (no probes) | No diagnostics reported.
|
| phpantom | 0.10.0phpantom_lsp 0.10.0 | Recognized (no probes) | phpdoc_advanced_phpstan_param_closure_this.php:43: Property 'missing' not found on class 'Conformance\Tests\PhpdocAdvancedPhpstanParamClosureThis\Host' [unknown_member]
|
| intelephense | 1.18.5intelephense 1.18.5 | Enforced | phpdoc_advanced_phpstan_param_closure_this.php:43: Expected type 'string|Stringable|null'. Found 'mixed'. [P1006] phpdoc_advanced_phpstan_param_closure_this.php:43: Undefined property '$missing'. [P1014]
|
| phpactor | 2026.06.23.0Phpactor 2026.06.23.0 | Recognized (no probes) | phpdoc_advanced_phpstan_param_closure_this.php:39: Undefined variable "$this" [undefined_variable] phpdoc_advanced_phpstan_param_closure_this.php:43: Undefined variable "$this" [undefined_variable]
|
| phpy | 1.0.19274phpy 1.0.19274 | Enforced | phpdoc_advanced_phpstan_param_closure_this.php:43: Undefined property: Host::$missing
|
| qodana | 262.9437.196Qodana 262.9437.196 | 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.8steins 0.1.8 (2026-09-21 revision 76306a9) | Recognized (no probes) | No diagnostics reported.
|
| phpstan-strict | 2.2.15PHPStan - PHP Static Analysis Tool 2.2.15 | Enforced | phpdoc_advanced_phpstan_param_closure_this.php:43: Access to an undefined property Conformance\Tests\PhpdocAdvancedPhpstanParamClosureThis\Host::$missing. phpdoc_advanced_phpstan_param_closure_this.php:43: Parameter #1 (mixed) of echo cannot be converted to string.
|
| psalm-next | 7.0.0-beta22Psalm 7.0.0-beta22@de27e5724ee5997a1e3baee4d394ca6f4e46eba0 | Not enforced | phpdoc_advanced_phpstan_param_closure_this.php:39: Invalid reference to $this in a non-class context [InvalidScope] phpdoc_advanced_phpstan_param_closure_this.php:39: Argument 1 of echo cannot be mixed, expecting string [MixedArgument] phpdoc_advanced_phpstan_param_closure_this.php:43: Invalid reference to $this in a non-class context [InvalidScope] phpdoc_advanced_phpstan_param_closure_this.php:43: Argument 1 of echo cannot be mixed, expecting string [MixedArgument]
|
<?php
declare(strict_types=1);
namespace Conformance\Tests\PhpdocAdvancedPhpstanParamClosureThis;
/**
* Cross-tool handling of `@param-closure-this`.
*
* Declares what `$this` will be inside a closure argument when the callee
* rebinds it. Honouring the tag makes `$this->label` resolve on `Host`.
*
* Tools that do not model the tag typically reject `$this` in the closure as
* out of scope (recorded as Fail / false positives on the valid access).
*
* References:
* - PHPStan phpdocs-basics: Callables / `@param-closure-this`
*/
final class Host
{
public string $label = 'host';
}
/**
* @param-closure-this Host $callback
*/
function runWithHost(\Closure $callback): void // T: @param-closure-this
{
$callback->call(new Host()); // E?[noise]: some tools reject Closure::call on the parameter type
}
runWithHost(function (): void {
// $this is Host: label is defined when the tag is honoured. Honouring the
// tag means silence here; a tool that ignores @param-closure-this rejects
// `$this` as out of scope, so a diagnostic means the tag was not applied.
// Scoped to the analyzers that model the tag so silence-by-default tools do
// not earn a free pass.
echo $this->label; // Q?<phpstan> // Q?<phpstan-strict> // Q?<psalm> // Q?<pzoom> // Q?<mago> // Q?<phan> // Q?<intelephense> // Q?<phpy> // E?[noise]
// A property that Host does not declare. Tools that ignore the tag also
// report this (via out-of-scope `$this`), so it is noise for the tag.
echo $this->missing; // E?[noise]: Host has no $missing under @param-closure-this
});