Error Identifier: missingType.iterableValue
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 UserRepository
{
/** @var array */
private $users;
/** @return array */
public function getAll(): array
{
return $this->users;
}
}
Why is it reported? #
An iterable type such as array is used without specifying the types of its values (and optionally keys). Without a value type, PHPStan cannot track what the iterable contains, reducing the effectiveness of type checking for any code that consumes the iterable.
This rule applies to properties, function/method return types, and function/method parameters. It is reported for any iterable type – array, iterable, Generator, and other traversable types – when the value type is missing.
How to fix it #
Specify the value type for the iterable using PHPDoc:
<?php declare(strict_types = 1);
class UserRepository
{
- /** @var array */
+ /** @var array<User> */
private $users;
- /** @return array */
+ /** @return array<User> */
public function getAll(): array
{
return $this->users;
}
}
If both keys and values matter, specify both:
<?php declare(strict_types = 1);
class UserRepository
{
- /** @var array */
+ /** @var array<int, User> */
private $users;
}
For a more specific array shape, use array shape syntax:
<?php declare(strict_types = 1);
-/** @return array */
+/** @return array{name: string, age: int} */
function getConfig(): array
{
return ['name' => 'app', 'age' => 1];
}
In PHPStan 1.x this error was possible to ignore with configuration parameter checkMissingIterableValueType: false but in PHPStan 2.0 this parameter was removed in favor of ignoring by identifier:
How to ignore this error #
You can use the identifier missingType.iterableValue to ignore this error using a comment:
// @phpstan-ignore missingType.iterableValue
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: missingType.iterableValue
Rules that report this error #
- PHPStan\Rules\Classes\LocalTypeAliasesRule [1]
- PHPStan\Rules\Classes\LocalTypeTraitAliasesRule [1]
- PHPStan\Rules\Classes\LocalTypeTraitUseAliasesRule [1]
- PHPStan\Rules\Classes\MethodTagRule [1]
- PHPStan\Rules\Classes\MethodTagTraitRule [1]
- PHPStan\Rules\Classes\MethodTagTraitUseRule [1]
- PHPStan\Rules\Classes\MixinRule [1]
- PHPStan\Rules\Classes\MixinTraitRule [1]
- PHPStan\Rules\Classes\MixinTraitUseRule [1]
- PHPStan\Rules\Classes\PropertyTagRule [1]
- PHPStan\Rules\Classes\PropertyTagTraitRule [1]
- PHPStan\Rules\Classes\PropertyTagTraitUseRule [1]
- PHPStan\Rules\Constants\MissingClassConstantTypehintRule [1]
- PHPStan\Rules\Functions\MissingFunctionParameterTypehintRule [1]
- PHPStan\Rules\Functions\MissingFunctionReturnTypehintRule [1]
- PHPStan\Rules\Methods\MissingMethodParameterTypehintRule [1]
- PHPStan\Rules\Methods\MissingMethodReturnTypehintRule [1]
- PHPStan\Rules\Methods\MissingMethodSelfOutTypeRule [1]
- PHPStan\Rules\PhpDoc\FunctionAssertRule [1]
- PHPStan\Rules\PhpDoc\InvalidPhpDocVarTagTypeRule [1]
- PHPStan\Rules\PhpDoc\MethodAssertRule [1]
- PHPStan\Rules\Properties\MissingPropertyTypehintRule [1]
- PHPStan\Rules\Properties\SetPropertyHookParameterRule [1]