Skip to content

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

@damnedest

Description

@damnedest

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions