Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

static return type loses information about intersection types #4849

Open
MatmaRex opened this issue Apr 4, 2024 · 0 comments
Open

static return type loses information about intersection types #4849

MatmaRex opened this issue Apr 4, 2024 · 0 comments

Comments

@MatmaRex
Copy link

MatmaRex commented Apr 4, 2024

static return type loses information about intersection types.

Example: https://phan.github.io/demo/?code=%3C%3Fphp%0A%0Aclass+Foo+{%0A%09function+a()%3A+static+{%0A%09%09return+%24this%3B%0A%09}%0A}%0A%0Ainterface+Woop+{%0A%09function+b()%3A+static%3B%0A}%0A%0Aclass+Bar+extends+Foo+implements+Woop+{%0A%09function+b()%3A+static+{%0A%09%09return+%24this%3B%0A%09}%0A}%0A%0Afunction+newBar()%3A+Foo%26Woop+{%0A%09return+new+Bar%3B%0A}%0A%0A(new+Bar)->a()->b()%3B%0AnewBar()->a()->b()%3B%0A

<?php

class Foo {
	function a(): static {
		return $this;
	}
}

interface Woop {
	function b(): static;
}

class Bar extends Foo implements Woop {
	function b(): static {
		return $this;
	}
}

function newBar(): Foo&Woop {
	return new Bar;
}

(new Bar)->a()->b();
newBar()->a()->b();

Outputs:

input:24: PhanUndeclaredMethod Call to undeclared method \Foo::b (Did you mean expr->a())

(Phan 5.4.1)

It seems to me that, if newBar()'s type is Foo&Woop, and Foo::a returns static, then newBar()->a()'s type should also be Foo&Woop – however, it is apparently Foo.

(The same thing happens when the method is documented with @return static, or @return $this, instead of the static PHP type.)

I've tested the same example using Psalm and PHPStan, and they both accept this code:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant