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

false positive: Variable $var might not be defined when using switch in catch block #1219

Closed
damnedest opened this issue Jul 13, 2018 · 6 comments

Comments

@damnedest
Copy link

damnedest commented Jul 13, 2018

If you use switch in catch block phpstan you get error that variable might not be defined.

Code snippet that reproduces the problem

https://phpstan.org/r/340e85cc81407dc0e0173b8cce5ada9a

<?php declare(strict_types = 1);

function test(): int
{
	try {
		$var = 1;
	} catch (\Throwable $e) {
		switch ($e->getCode()) {
			case 1:
				return 0;
			default:
				return -1;
		}
	}
	
	return $var;
}

Expected output

No errors

@ondrejmirtes
Copy link
Member

Hi,
please set polluteCatchScopeWithTryAssignments to true. See #503.

@ondrejmirtes ondrejmirtes added this to the Exception tracking milestone Jul 13, 2018
@adaamz
Copy link
Contributor

adaamz commented Jul 13, 2018

Or return that value in try block https://phpstan.org/r/a8c5d87ad1e198548331e704920c651d

@adaamz
Copy link
Contributor

adaamz commented Jul 13, 2019

@ondrejmirtes
Copy link
Member

Again, it might be easily tricked with different code. Needs full-fledged exception tracking. That's why it's still open.

@ondrejmirtes
Copy link
Member

Fixed by: phpstan/phpstan-src#481

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants