Error Identifier: selfOut.type
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 Collection
{
/**
* @phpstan-self-out int
*/
public function reset(): void
{
}
}
Why is it reported? #
The type specified in the @phpstan-self-out PHPDoc tag is not a subtype of the class that declares the method. The @phpstan-self-out tag narrows the type of $this after a method call, so the declared type must be compatible with (a subtype of) the declaring class.
In the example above, int is not a subtype of Collection.
How to fix it #
Change the @phpstan-self-out type to a valid subtype of the declaring class:
<?php declare(strict_types = 1);
+/**
+ * @template T
+ */
class Collection
{
/**
- * @phpstan-self-out int
+ * @phpstan-self-out self<int>
*/
public function reset(): void
{
}
}
How to ignore this error #
You can use the identifier selfOut.type to ignore this error using a comment:
// @phpstan-ignore selfOut.type
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: selfOut.type
Rules that report this error #
- PHPStan\Rules\PhpDoc\IncompatibleSelfOutTypeRule [1]