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

Intersections appear not to be templated properly #2581

Closed
muglug opened this issue Nov 10, 2019 · 6 comments
Closed

Intersections appear not to be templated properly #2581

muglug opened this issue Nov 10, 2019 · 6 comments

Comments

@muglug
Copy link
Contributor

muglug commented Nov 10, 2019

Code snippet that reproduces the problem

class MockObject
{
	public function checkExpectations() : void
	{
	}
}

/**
 * @template RequestedType
 * @param class-string<RequestedType> $className
 * @return RequestedType&MockObject
 */
function mock(string $className)
{
	$instance = eval("there be dragons");

	return $instance;
}

class A {
	public function foo() : void {}
}

/**
 * @template UnknownType
 * @param class-string<UnknownType> $className
 */
function useMockTemplated(string $className) : void
{
	mock($className)->checkExpectations();
}

mock(A::class)->foo(); // good
mock(A::class)->checkExpectations(); // good
mock(A::class)->notFoo(); // bad

Expected output

Method (A&MockObject)::notFoo does not exist

Actual output

No issues

@ondrejmirtes
Copy link
Member

PHPStan is confused because you're creating intersection from two classes. If you make MockObject an interface, it works: https://phpstan.org/r/9149e4de-55cc-45e5-a1ac-13d6bdd9d750

Your playground link (missing from the description): https://phpstan.org/r/10bd4532-0406-4a64-a82c-5f317372fa7c

There should probably be a rule that checks if the final return type after inferring types isn't NeverType...

@arnaud-lb
Copy link
Contributor

What is happening here is that RequestedType is being resolved to A, however the type A&MockObject is impossible (they are unrealated classes). In the type system, this results in a "never type" or "error type", which is usually spotted later by rules and reported as an error.

@ondrejmirtes
Copy link
Member

It’s not spotted in this case because the NeverType rule is called on method declaration but here it happens in a specific method call.

@phpstan-bot
Copy link
Contributor

@ondrejmirtes After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-No errors
+26: Return type of call to function mock contains unresolvable type.
+27: Return type of call to function mock contains unresolvable type.
+28: Return type of call to function mock contains unresolvable type.
Full report
Line Error
26 Return type of call to function mock contains unresolvable type.
27 Return type of call to function mock contains unresolvable type.
28 Return type of call to function mock contains unresolvable type.

@ondrejmirtes
Copy link
Member

Implemented: phpstan/phpstan-src@58c29fd

@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 Oct 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants