Error Identifier: property.nestedUnusedType
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 Processor
{
/** @var array<int, string|int> */
private array $items = []; // ERROR: Type array<int, int|string> of property Processor::$items can be narrowed to array<int, string>.
public function add(string $item): void
{
$this->items[] = $item;
}
}
Why is it reported? #
The property has a declared type that is wider than what is actually assigned to it. PHPStan analyses all assignments to the property and determines that a type within a nested position of the declared type is never actually assigned. In the example above, the property declares it can hold string|int values, but only string values are ever assigned.
This helps catch overly broad type declarations that make the code harder to reason about and can hide bugs.
How to fix it #
Narrow the property type to match what is actually assigned:
<?php declare(strict_types = 1);
class Processor
{
- /** @var array<int, string|int> */
+ /** @var array<int, string> */
private array $items = [];
public function add(string $item): void
{
$this->items[] = $item;
}
}
If the broader type is intentional because future code will assign values of additional types, add the missing assignment or adjust the design accordingly.
How to ignore this error #
You can use the identifier property.nestedUnusedType to ignore this error using a comment:
// @phpstan-ignore property.nestedUnusedType
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: property.nestedUnusedType
Rules that report this error #
- PHPStan\Rules\TooWideTypehints\TooWideArrowFunctionReturnTypehintRule [1]
- PHPStan\Rules\TooWideTypehints\TooWideClosureReturnTypehintRule [1]
- PHPStan\Rules\TooWideTypehints\TooWideFunctionParameterOutTypeRule [1]
- PHPStan\Rules\TooWideTypehints\TooWideFunctionReturnTypehintRule [1]
- PHPStan\Rules\TooWideTypehints\TooWideMethodParameterOutTypeRule [1]
- PHPStan\Rules\TooWideTypehints\TooWideMethodReturnTypehintRule [1]
- PHPStan\Rules\TooWideTypehints\TooWidePropertyTypeRule [1]