Skip to content

Unclear error message when assigning literals that variable doesn't allow #3231

@canvural

Description

@canvural

Bug report

With the recent commits now it's possible to give variable literal values that it can hold. For example /** @var 0|1 $foo */ means $foo can only be 0 or 1. I don't know if this was intentional or was a side effect of something else. But it seems it's working.

But when a value that is out of the expected values is given, the error message is unclear.

Code snippet that reproduces the problem

<?php declare(strict_types = 1);

class HelloWorld
{
	/** @var 0|1 $foo */
	public $foo;
	
	public function foo(): void
	{
		$this->foo = 2;
	}
}

https://phpstan.org/r/b58a5065-812b-4a3f-9e3d-2f79fb788f6d

Expected output

The error message Property HelloWorld::$foo (int) does not accept int. is not clear. Maybe it could be improved with Property HelloWorld::$foo (int(0)|int(1)) does not accept int(2).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions