Skip to content

Match statements throwing exceptions not interpreted correctly #6251

@SamMousa

Description

@SamMousa

Bug report

PHPStan 1.3 seems to not correctly identify that all arms of a match will throw an exception.

Code snippet that reproduces the problem

https://phpstan.org/r/b0a03c0c-b824-4839-a573-4334be9cec54

<?php declare(strict_types = 1);

function returnIntegerOrThrow(): int {
	if (rand(1, 100) > 50) {
		return 37;
	}
	
	match(rand(1, 1000)) {
		1 =>  throw new \Exception("that's random"),
		default => throw new \Exception("bad roll")
	};
	
}

Even a simplified version: https://phpstan.org/r/dbdc6ae4-4590-479c-9757-f1f6e83fd5f8 fails. This means that the issue is not that it's not handling the default arm properly.

<?php declare(strict_types = 1);

function returnIntegerOrThrow(): int {
	if (rand(1, 100) > 50) {
		return 37;
	}
	
	match(1) {
		1 =>  throw new \Exception("that's random"),
	};
	
}

Expected output

No output expected

Did PHPStan help you today? Did it make you happy in any way?

I love it, especially for small libraries that I don't work on often.
Investing the time once to get it up to the max phpstan level saves me so much time and stupid mistakes in the future.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions