phpphpstan

can PHPStan detect that a @param can be an array of int, an array of string but not a mix of int and string?


readonly class Identifier extends Regexp
{
    /**
     * @param array<int,int>|array<int,string> $possibleValues
     */
    public function __construct(array $possibleValues = [])
    {
        $regexp = implode('|', $possibleValues);
        parent::__construct('' !== $regexp ? $regexp : null);
    }
}

Everything works fine when i create an Identifierwith an array that contains a float, phpstan detects an error. But it does not detect any error when i create an Identifier with an array of int and string mixed like

$test = new Identifier([1,2,"test"];

Is there a way to tells phpStan that $possibleValues can be an array of int, an array of string but not a mix of int and string ?


Solution

  • Yes, union of arrays is collapsed into an array of union. There's a feature request to change that: https://github.com/phpstan/phpstan/issues/8963