Error Identifier: offsetAssign.valueType
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 TypedCollection implements ArrayAccess
{
/** @var array<int, string> */
private array $items = [];
public function offsetExists(mixed $offset): bool { return isset($this->items[$offset]); }
public function offsetGet(mixed $offset): mixed { return $this->items[$offset]; }
public function offsetSet(mixed $offset, mixed $value): void { $this->items[$offset] = $value; }
public function offsetUnset(mixed $offset): void { unset($this->items[$offset]); }
}
function doFoo(TypedCollection $collection): void
{
$collection[] = 123; // ERROR: TypedCollection does not accept int.
}
Why is it reported? #
The value being assigned to an offset on the object is not compatible with the type that the object accepts. The object implements ArrayAccess (or has similar offset-access capabilities), but the value being assigned does not match the expected type. In the example above, the collection expects string values but receives an int.
How to fix it #
Assign a value of the correct type:
<?php declare(strict_types = 1);
function doFoo(TypedCollection $collection): void
{
- $collection[] = 123;
+ $collection[] = 'hello';
}
Or update the collection’s type to accept the value type being assigned:
<?php declare(strict_types = 1);
class TypedCollection implements ArrayAccess
{
- /** @var array<int, string> */
+ /** @var array<int, string|int> */
private array $items = [];
// ...
}
How to ignore this error #
You can use the identifier offsetAssign.valueType to ignore this error using a comment:
// @phpstan-ignore offsetAssign.valueType
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: offsetAssign.valueType
Rules that report this error #
- PHPStan\Rules\Arrays\OffsetAccessValueAssignmentRule [1]