PHPStan: unspecified value in iterable
Fabian Blechschmidt
Method Some\ClassDefinition::write() has parameter $record with no value type specified in iterable type array.
I got this error yesterday and it took hours until I figured out the problem.
Why you ask? Because the class looked like this:
<?php
declare(strict_types=1);
namespace Some;
/**
* @phpstan-type LogRecord array{
* message: string,
* context: array,
* level: 100|200|250|300|400|500|550|600,
* level_name: 'ALERT'|'CRITICAL'|'DEBUG'|'EMERGENCY'|'ERROR'|'INFO'|'NOTICE'|'WARNING',
* channel: string,
* datetime: \DateTimeImmutable,
* extra: array,
* formatted: mixed
* }
*/
class ClassDefinition
{
/**
* @param LogRecord $record
*/
public function write(array $record): void
{
// do something
}
}
My impression was: why is $record a problem? It has a type! After trying a simpler example at PHPStan’s playground, I found the problem:
/**
* @phpstan-type LogRecord array{
[...]
* context: array,
[...]
* extra: array,
[...]
* }
*/
While writing this blogpost I tried it again, and PHPStan is of course telling you, what the problem is:
Class Some\ClassDefinition has type alias LogRecord with no value type specified in iterable type array.
I just didn’t see it.
Hope that helps!
Other articles from this category
ENABLE_SERVICES: the Shopware env var that quietly installs apps on your shop
I was grepping through vendor/shopware/core for the hidden-gems series and stumbled over Shopware\Core\Service\AllServiceInstaller. Two-argument constructor, twelve lines of logic, @internal. Nothing dramatic — until I read what it actually does: it installs apps on your shop. By itself. In production. Once a day. Unless you told it not to. So this one isn’t going to […]
Shopware 6 Hidden Gems #24: Lightning round — ten small gems that didn’t need their own post
Twenty-three parts into this series, my research folder still has a stack of finds that are real, useful, undocumented — and about two paragraphs big. Instead of stretching each into a full post, here’s the lightning round. One problem, one gem, one file reference. Go. 1. rawUrl() — URLs for mail templates that actually work […]
Shopware 6 Hidden Gems #23: Why your sitemap silently stopped updating (and sitemap:generate –force)
A slow-burn bug report, verbatim from memory: „Google Search Console shows our sitemap is three months old. New categories aren’t in it.“ Three months. No errors in the log worth noticing, the scheduled task looks healthy, and regenerating „worked“ — or seemed to. The cause turned out to be a mechanism that’s completely reasonable and […]