Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncatched exception if psalm-assert references unknown class/interface #8373

Closed
Daeroni opened this issue Aug 5, 2022 · 2 comments · Fixed by #8376
Closed

Uncatched exception if psalm-assert references unknown class/interface #8373

Daeroni opened this issue Aug 5, 2022 · 2 comments · Fixed by #8376

Comments

@Daeroni
Copy link
Contributor

Daeroni commented Aug 5, 2022

$class_storage = $codebase->classlike_storage_provider->get($input_type_part->value);

Might fail if the referenced class-like $input_type_part->value does not exists.

introduced in af5c191

This can happen when a library has a @psalm-assert using a class/interface that it and the main application does not explicitly require.

An example is when using amphp/amp function all https://github.com/amphp/amp/blob/9d5100cebffa729aaffecd3ad25dc5aeea4f13bb/lib/functions.php#L384 which uses React\Promise\PromiseInterface in @psalm-assert but React is not hard requirement for Amp nor our application.

Wrapping the code inside a try-catch seems to fix the issue, but is this a correct way to fix this issue and is an empty array a good fallback for $input_type_params?

            try {
                $class_storage = $codebase->classlike_storage_provider->get($input_type_part->value);

                $container_class = $container_type_part->value;

                if (strtolower($input_type_part->value) === strtolower($container_type_part->value)) {
                    $input_type_params = $class_storage->getClassTemplateTypes();
                } elseif (!empty($class_storage->template_extended_params[$container_class])) {
                    $input_type_params = array_values($class_storage->template_extended_params[$container_class]);
                } else {
                    $input_type_params = array_fill(0, count($class_storage->template_types ?? []), Type::getMixed());
                }
            } catch (Throwable $e) {
                $input_type_params = [];
            }
@psalm-github-bot
Copy link

Hey @Daeroni, can you reproduce the issue on https://psalm.dev ?

@AndrolGenhald
Copy link
Collaborator

Thanks for tracking this down!

Wrapping the code inside a try-catch seems to fix the issue, but is this a correct way to fix this issue and is an empty array a good fallback for $input_type_params?

I would probably do if ($codebase->classlike_storage_provider->has(...)) instead of try-catch, but empty array should be fine for a fallback. Feel free to provide a PR, otherwise I'll get around to it probably early next week.

Daeroni added a commit to Daeroni/psalm that referenced this issue Aug 5, 2022
Daeroni added a commit to Daeroni/psalm that referenced this issue Aug 8, 2022
Daeroni added a commit to Daeroni/psalm that referenced this issue Aug 8, 2022
Undefined classes in function dockblocks should not crash psalm.
Test provided by @AndrolGenhald
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants