'Static analysis - array type in abstract method implementation
How to declare array type in child classes that implement abstract method? Different child classes may expect different values in given array, so method signature might change from class to class.
abstract class A
{
/** @param array<int|string, mixed> $parameters */
abstract public function validate(array $parameters): void;
}
class B extends A
{
/** @param array<int, string> $parameters */
public function validate(array $parameters): void
{
//CONTENT
}
}
class C extends A
{
/** @param array<int, int> $parameters */
public function validate(array $parameters): void
{
//CONTENT
}
}
... many more
In the example above PHPStan returns
Parameter #1 $parameters should be contravariant with parameter $parameters
Using PHP 7.4
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
