Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
class Foo
{
/**
* @param-closure-this Foo $callback
*/
public function register(string $callback): void
{
}
}
Why is it reported? #
The @param-closure-this PHPDoc tag is used on a parameter whose native type is not Closure. The @param-closure-this tag specifies the $this context inside a Closure, so it only makes sense for parameters typed as Closure. Other types (like string, callable, or array) do not bind a $this context in the same way.
How to fix it #
Change the parameter type to Closure:
class Foo
{
/**
* @param-closure-this Foo $callback
*/
- public function register(string $callback): void
+ public function register(\Closure $callback): void
{
}
}
Or remove the @param-closure-this tag if the parameter is not intended to be a Closure:
class Foo
{
- /**
- * @param-closure-this Foo $callback
- */
public function register(string $callback): void
{
}
}
How to ignore this error #
You can use the identifier paramClosureThis.nonClosure to ignore this error using a comment:
// @phpstan-ignore paramClosureThis.nonClosure
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: paramClosureThis.nonClosure