1

Here's the code:

<?php declare(strict_types=1);

/**
 * @param  array{key?: string}  $options
 */
function hello($options)
{
    var_dump($options);
}

hello([
    'WRONG_KEY' => '...',
]);

I would expect phpstan analyze to fail on this code, complaining about unexpected WRONG_KEY in the passed array.

Why it's not failing? Do I miss some configuration or is it how PHPStan supposed to work?

1

1 Answer 1

3

This is about sealed vs. unsealed array shapes.

Unsealed array shapes allow extra keys. Right now PHPStan’s array shapes mostly act as unsealed, but sometimes it’s inconsistent.

There’s an open issue about it: https://github.com/phpstan/phpstan/issues/8438 (one of the most upvoted issues so it will get solved sooner or later)

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

2 Comments

Subscribed to that issue, thanks Ondřej!
It's actually the most upvoted one with 26 reactions. I guess people want that feature, it's so expected for those who are coming from TypeScript.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.