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

Psalm crashes on switch-case statement with gettype call and 'resource (closed)' followed by 'unknown type' cases (misplaced brackets) #6088

Closed
tomdenbraber opened this issue Jul 14, 2021 · 1 comment · Fixed by #6089

Comments

@tomdenbraber
Copy link

tomdenbraber commented Jul 14, 2021

Hi!

We have been using Psalm for a while now, and already improved our codebase quite a bit!

However, we ran into a problem while adding Psalm to yet another project: the Psalm runner actually crashes. I managed to find out where the error occurs, and it is really quite specific.

A minimal example can be found here: https://psalm.dev/r/648ae887e8

The error occurs whenever a switch on the value of gettype has at least two case statements, namely resource (closed) and unknown type. The case statements should be in that order for the error to show, and the resource (closed)-case and all the other cases between case resource (closed): and case unknown type: should not have a statement which exits the switch-statement (i.e., a break, throw, return or even continue).

So the following fails:

<?php

$data = "foo";
switch (gettype($data)) {
	case "resource (closed)":
	case "unknown type":
		return "foo";
}

but this does not (https://psalm.dev/r/c95469669e):

<?php

$data = "foo";
switch (gettype($data)) {
	case "unknown type":
	case "resource (closed)":
		return "foo";
}

In our codebase we can easily change the order of the case-statements and thereby evade this error. I don't know whether this issue maybe signifies a larger bug, so I would like to report this issue nonetheless.

Keep up the good work :)

@psalm-github-bot
Copy link

psalm-github-bot bot commented Jul 14, 2021

I found these snippets:

https://psalm.dev/r/648ae887e8
<?php

$data = "foo";
switch (gettype($data)) {
	case "resource (closed)":
	case "unknown type":
		return "foo";
}
Psalm encountered an internal error:

/vendor/vimeo/psalm/src/Psalm/Internal/Type/TypeParser.php: Misplaced brackets
https://psalm.dev/r/c95469669e
<?php

$data = "foo";
switch (gettype($data)) {
	case "unknown type":
	case "resource (closed)":
		return "foo";
}
Psalm output (using commit 601c8ca):

No issues!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant