Skip to content

Internal error from UnionType.php:219 #2745

@cs278

Description

@cs278

In the process of switching to 0.12 and came across this internal error in some code that worked on 0.11 just fine. I've narrowed the issue down to performing assertions against $this in the constructor which seems to confuse PHPStan.

I have 3 classes:

  • abstract base
  • abstract frontend extends base
  • abstract backend extends base

In base::__construct() I was throwing an exception if $this was not an instance of frontend or backend, to prevent developers directly extending base.

abstract class base
{
	/** @var string */
	private $foo;
	
	public function __construct(string $foo)
	{
                // Remove one of these conditions and you get a different result
		\assert($this instanceof frontend || $this instanceof backend);
		
		$this->foo = $foo;
	}
}
				
class frontend extends base {}
class backend extends base {}

https://phpstan.org/r/fabfb5a1-8b9a-4f13-919e-0a1347869295

It seems to me that after the assertion PHPStan no longer accepts that $this is an instance of base which causes an error when checking if the property access is valid.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions