When dealing with legacy data, you often encounter arrays or associative arrays. These arrays are untyped, which PHPStan, of course, does not accept, resulting in numerous PHPStan errors.
PHPStan, by the way, is a static analysis tool for PHP that enforces strict typing and checks for compliance with PHPDoc annotations, ensuring code is robust and maintainable.
For any serious project you should use it.
For this code:
$array = $this->getUntypedArray();
$res = $this->funcWithInt($array['number']);
...
private function funcWithInt(int $number):int {
return $number++;
}
Code language: PHP (php)
A typical error could be:
Parameter #1 $number of method TestClass::funcWithInt expects int, mixed given.
Code language: PHP (php)
Continue reading “Typed Arrays with PHPStan types”