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

Call to method foo() on an unknown class FooBar when FooBar is a trait #7720

Closed
kylekatarnls opened this issue Jul 29, 2022 · 4 comments
Closed

Comments

@kylekatarnls
Copy link

Bug report

Code snippet that reproduces the problem

<?php declare(strict_types = 1);

trait FooBar
{
	public function foo(): string
	{
		return 'abc';
	}
}

class HelloWorld
{
	public function sayHello(mixed $value): void
	{
		if ($value instanceof FooBar) {
			echo $value->foo();
		}
	}
}

https://phpstan.org/r/21864fe8-ba76-4945-b9fc-9dc27e286ca1

Expected output

I expect no error for the code above.

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

This snippet system with try is still very cool to be sure in a blink if current version has the same output.

@canvural
Copy link
Contributor

But this code cannot really work, right? You can't initiate traits, so you can't pass them to a method. Also instanceof with a trait will always be false I think.

Maybe you can try to use class_uses check?

@kylekatarnls
Copy link
Author

kylekatarnls commented Jul 29, 2022

Thanks. Indeed, I though class using a trait FooBar would return true with instanceof FooBar but it seems not. I'll re-open if I find the reason behind this code.

@ondrejmirtes
Copy link
Member

I'd like the "impossible instanceof rule" to report it doesn't work with traits 😊

Also ObjectType::isSuperTypeOf() should return No if thisReflection or theirReflection is a trair.

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants