← All results

@param-closure-this

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

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

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 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]
  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
phpstan 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Enforced reported Lv.0+
phpdoc_advanced_phpstan_param_closure_this.php:43: Access to an undefined property Conformance\Tests\PhpdocAdvancedPhpstanParamClosureThis\Host::$missing. [identifier=property.notFound] [reported-from-level=0]
phpdoc_advanced_phpstan_param_closure_this.php:43: Parameter #1 (mixed) of echo cannot be converted to string. [identifier=echo.nonString] [reported-from-level=10]
With strict-rules
phpdoc_advanced_phpstan_param_closure_this.php:43: Access to an undefined property Conformance\Tests\PhpdocAdvancedPhpstanParamClosureThis\Host::$missing. [identifier=property.notFound]
phpdoc_advanced_phpstan_param_closure_this.php:43: Parameter #1 (mixed) of echo cannot be converted to string. [identifier=echo.nonString]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
psalm 6.16.1Psalm 6.16.1@f1f5de594dc76faf8784e02d3dc4716c91c6f6ac
next: 7.0.0-beta19
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]
  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
mago 1.46.0mago 1.46.0 Enforced
phpdoc_advanced_phpstan_param_closure_this.php:30: Attempting to access a method on a non-object type (`(closure(...mixed=): mixed)`). [invalid-method-access]
phpdoc_advanced_phpstan_param_closure_this.php:43: Property `$missing` does not exist on class `Conformance\Tests\PhpdocAdvancedPhpstanParamClosureThis\Host`. [non-existent-property]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
mir 0.70.1mir 0.70.1 Recognized (no probes)

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: not measured — this test carries no rejection probes
phpantom 0.9.0phpantom_lsp 0.9.0 Recognized (no probes)
phpdoc_advanced_phpstan_param_closure_this.php:43: Property 'missing' not found on class 'Conformance\Tests\PhpdocAdvancedPhpstanParamClosureThis\Host' [unknown_member]
  • Recognition: spelling resolved
  • Enforcement: not measured — this test carries no rejection probes
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]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
phpy 0.2.0phpy 0.2.0 Enforced
phpdoc_advanced_phpstan_param_closure_this.php:43: Undefined property: Host::$missing
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
qodana 262.8665.325Qodana 262.8665.325 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.5steins 0.1.5 (2026-08-12 revision 39b4cc1) Recognized (no probes)

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: not measured — this test carries no rejection probes
phpstan-strict 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Enforced
phpdoc_advanced_phpstan_param_closure_this.php:43: Access to an undefined property Conformance\Tests\PhpdocAdvancedPhpstanParamClosureThis\Host::$missing. [identifier=property.notFound]
phpdoc_advanced_phpstan_param_closure_this.php:43: Parameter #1 (mixed) of echo cannot be converted to string. [identifier=echo.nonString]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
psalm-next 7.0.0-beta19Psalm 7.0.0-beta19@7e751c06a756fa64dc4c759c09fe4a173afcb433 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]
  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported

Source

<?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
});