Skip to content

$this(Type~Subtracted) does not work on conditional return type as Type~Subtracted #12244

@EmielM

Description

@EmielM

Bug report

Easiest to illustrate with code:

<?php declare(strict_types = 1);

enum X: string {
	case A = 'a';
	case B = 'b';
	case C = 'c';

	/** @return ($this is self::A ? int : null) */
	public function get(): ?int {
		return ($this === self::A) ? null : 123;
	}

	public function doSomething(): void {
		if ($this !== self::A) {
			\PHPStan\dumpType($this); // $this(X~X::A)
			\PHPStan\dumpType($this->get()); // int|null
		}
	}

	public static function doSomethingFor(X $x): void {
		if ($x !== self::A) {
			\PHPStan\dumpType($x); // X~X::A
			\PHPStan\dumpType($x->get()); // int
		}
	}
}

I believe line 16 (in instance method doSomething) c/should be narrowed to int, like in the static function.

Code snippet that reproduces the problem

https://phpstan.org/r/0de4b773-c4a4-4d66-9ed0-601762d70c52

Expected output

Inference to int

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

Since I've starting using PHPStan, I've had a more fulfilling life

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions