phpstan/phpstan-webmozart-assert

Validating nested arrays ends in mixed

alexander-schranz opened this issue · 5 comments

A nested array like:

    /**
     * @param mixed[] $requestData
     *
     * @return array{
     *     accountId: int,
     *     errorColor: string|null,
     *     theme: array{
     *         backgroundColor: string|null,
     *         textColor: string|null,
     *         headerImage: array{id: int}|null,
     *     },
     * }
     */

https://phpstan.org/r/652f74f8-c926-4802-99b9-bab73da329dd

As requested the none webmozart assert implementation: https://phpstan.org/r/f535c1bb-82e3-4e49-a1d6-ee31c25339fe / https://phpstan.org/r/371acae7-1694-46bc-9d4e-2aa2c653134e (more dumps)

Is not possible to be validated as it seems to lost type after:

        Assert::keyExists($requestData['theme'], 'headerImage');
        Assert::nullOrIsArray($requestData['theme']['headerImage']);

        \Phpstan\dumpType($requestData);
        \Phpstan\dumpType($requestData['theme']);
        \Phpstan\dumpType($requestData['theme']['headerImage']);
4     Dumped type: array&hasOffset('errorColor')&hasOffset('theme')
5     Dumped type: mixed
6     Dumped type: mixed

Not sure if this is even possible. I see the complexness and also know that my solution is not the best. And should maybe go with json validation or something like that instead of validating array manually. Still I thought about reporting it.

PS: any possibility to enable Webmozart/Assert on https://phpstan.org/ ?

Can you try to extract the core problem with php functions on phpstan.org? Maybe it is a problem in phpstan itself we can improve

@herndlm https://phpstan.org/r/f535c1bb-82e3-4e49-a1d6-ee31c25339fe (edit version) / https://phpstan.org/r/371acae7-1694-46bc-9d4e-2aa2c653134e (more dumps)

Hopefully I did not make any error but think that should reflect what webmozart assert is doing.

Maybe this is enough as an example: https://phpstan.org/r/3cfc79ef-ea8f-4ad8-b6e6-6f4b881b931e but not sure.

So you want a general array being accepted in a place of a constant array. That's basically phpstan/phpstan#6552.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.