2
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 ?

1
  • Looking at the debug, it appears that it is collapsing the union down to array<int, int|string>. I tried with a List<int>|List<string> and some type aliases, but they all did the same thing, too. Commented Jun 26, 2024 at 13:12

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

I just wanted to suggest filing a feature request and that the PHPStan maintainer is really quick to react, but you beat me to it. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.