@phan-closure-scope$this type inside a returned/created closure (Phan-specific).
Parallel idea to PHPStan @param-closure-this, but for the closure's own
declared scope rather than a parameter rebinding.
References:
- Phan Annotating-Your-Source-Code: @phan-closure-scope| 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_phan_closure_scope.php:33: Variable $this is undeclared [PhanUndeclaredThis] phpdoc_advanced_phan_closure_scope.php:37: Variable $this is undeclared [PhanUndeclaredThis]
|
| phpstan | 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 | Recognized (no probes) reported Lv.0+ | phpdoc_advanced_phan_closure_scope.php:33: Cannot access property $label on mixed. [identifier=property.nonObject] [reported-from-level=10] phpdoc_advanced_phan_closure_scope.php:33: Parameter #1 (mixed) of echo cannot be converted to string. [identifier=echo.nonString] [reported-from-level=10] phpdoc_advanced_phan_closure_scope.php:33: Undefined variable: $this [identifier=variable.undefined] [reported-from-level=0] phpdoc_advanced_phan_closure_scope.php:37: Cannot access property $missing on mixed. [identifier=property.nonObject] [reported-from-level=10] phpdoc_advanced_phan_closure_scope.php:37: Parameter #1 (mixed) of echo cannot be converted to string. [identifier=echo.nonString] [reported-from-level=10] phpdoc_advanced_phan_closure_scope.php:37: Undefined variable: $this [identifier=variable.undefined] [reported-from-level=0] With strict-rulesphpdoc_advanced_phan_closure_scope.php:33: Cannot access property $label on mixed. [identifier=property.nonObject] phpdoc_advanced_phan_closure_scope.php:33: Parameter #1 (mixed) of echo cannot be converted to string. [identifier=echo.nonString] phpdoc_advanced_phan_closure_scope.php:33: Undefined variable: $this [identifier=variable.undefined] phpdoc_advanced_phan_closure_scope.php:37: Cannot access property $missing on mixed. [identifier=property.nonObject] phpdoc_advanced_phan_closure_scope.php:37: Parameter #1 (mixed) of echo cannot be converted to string. [identifier=echo.nonString] phpdoc_advanced_phan_closure_scope.php:37: Undefined variable: $this [identifier=variable.undefined]
|
| psalm | 6.16.1Psalm 6.16.1@f1f5de594dc76faf8784e02d3dc4716c91c6f6ac next: 7.0.0-beta19 |
Recognized (no probes) | phpdoc_advanced_phan_closure_scope.php:33: Invalid reference to $this in a non-class context [InvalidScope] phpdoc_advanced_phan_closure_scope.php:33: Argument 1 of echo cannot be mixed, expecting string [MixedArgument] phpdoc_advanced_phan_closure_scope.php:37: Invalid reference to $this in a non-class context [InvalidScope] phpdoc_advanced_phan_closure_scope.php:37: Argument 1 of echo cannot be mixed, expecting string [MixedArgument]
|
| mago | 1.46.0mago 1.46.0 | Recognized (no probes) | phpdoc_advanced_phan_closure_scope.php:33: Undefined variable: `$this`. [undefined-variable] phpdoc_advanced_phan_closure_scope.php:33: Attempting to access a property on a non-object type (`mixed`). [mixed-property-access] phpdoc_advanced_phan_closure_scope.php:33: The first value for `echo` is too general. [mixed-argument] phpdoc_advanced_phan_closure_scope.php:37: Undefined variable: `$this`. [undefined-variable] phpdoc_advanced_phan_closure_scope.php:37: Attempting to access a property on a non-object type (`mixed`). [mixed-property-access] phpdoc_advanced_phan_closure_scope.php:37: The first value for `echo` is too general. [mixed-argument] phpdoc_advanced_phan_closure_scope.php:42: Attempting to access a method on a non-object type (`(closure(...mixed): mixed)`). [invalid-method-access] phpdoc_advanced_phan_closure_scope.php:42: Assigning `mixed` type to a variable may lead to unexpected behavior. [mixed-assignment] phpdoc_advanced_phan_closure_scope.php:44: Expression of type `nonnull` cannot be called as a function or method. [invalid-callable]
|
| 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_phan_closure_scope.php:33: Expected type 'string|Stringable|null'. Found 'mixed'. [P1006] phpdoc_advanced_phan_closure_scope.php:37: Expected type 'string|Stringable|null'. Found 'mixed'. [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 | Recognized (no probes) | phpdoc_advanced_phan_closure_scope.php:33: Cannot access property $label on mixed. [identifier=property.nonObject] phpdoc_advanced_phan_closure_scope.php:33: Parameter #1 (mixed) of echo cannot be converted to string. [identifier=echo.nonString] phpdoc_advanced_phan_closure_scope.php:33: Undefined variable: $this [identifier=variable.undefined] phpdoc_advanced_phan_closure_scope.php:37: Cannot access property $missing on mixed. [identifier=property.nonObject] phpdoc_advanced_phan_closure_scope.php:37: Parameter #1 (mixed) of echo cannot be converted to string. [identifier=echo.nonString] phpdoc_advanced_phan_closure_scope.php:37: Undefined variable: $this [identifier=variable.undefined]
|
| psalm-next | 7.0.0-beta19Psalm 7.0.0-beta19@7e751c06a756fa64dc4c759c09fe4a173afcb433 | Recognized (no probes) | phpdoc_advanced_phan_closure_scope.php:33: Invalid reference to $this in a non-class context [InvalidScope] phpdoc_advanced_phan_closure_scope.php:33: Argument 1 of echo cannot be mixed, expecting string [MixedArgument] phpdoc_advanced_phan_closure_scope.php:37: Invalid reference to $this in a non-class context [InvalidScope] phpdoc_advanced_phan_closure_scope.php:37: Argument 1 of echo cannot be mixed, expecting string [MixedArgument]
|
<?php
declare(strict_types=1);
namespace Conformance\Tests\PhpdocAdvancedPhanClosureScope;
/**
* Cross-tool handling of `@phan-closure-scope`.
*
* Declares the `$this` type inside a returned/created closure (Phan-specific).
* Parallel idea to PHPStan `@param-closure-this`, but for the closure's own
* declared scope rather than a parameter rebinding.
*
* References:
* - Phan Annotating-Your-Source-Code: `@phan-closure-scope`
*/
final class Host
{
public string $label = 'host';
}
/**
* @return \Closure
* @phan-closure-scope Host
*/
function makePrinter(): \Closure // T: @phan-closure-scope
{
return function (): void {
// Tools that honour the tag resolve $this as Host; others reject $this.
// Silence is the discriminator, so this is scoped to Phan rather than
// awarding a free pass to tools that do not model the tag.
echo $this->label; // Q?<phan> // E?[noise]
// A tool that ignores @phan-closure-scope also reports this line because
// `$this` is unavailable, so the diagnostic is noise for tag enforcement.
echo $this->missing; // E?[noise]: Host has no $missing under @phan-closure-scope
};
}
$printer = makePrinter();
$bound = $printer->bindTo(new Host()); // E?[noise]: tooling noise on rebinding, not the tag under test
if ($bound !== null) {
$bound(); // E?[noise]: call-site tooling noise after bindTo
}