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

Generics does not respect type but fallback to basic types #6699

Closed
bastien-phi opened this issue Feb 25, 2022 · 5 comments
Closed

Generics does not respect type but fallback to basic types #6699

bastien-phi opened this issue Feb 25, 2022 · 5 comments
Labels
Milestone

Comments

@bastien-phi
Copy link

Bug report

Not sure if it was already reported but it looks like generics fallback to basic type and does not keep defined type.

Code snippet that reproduces the problem

/**
 * @template TValue
 */
class Foo
{
	/** @var TValue */
	public $value;
	
	/**
	 * @param TValue $value
	 */
	public function __construct($value) {
	  $this->value = $value;	
	}
}

/** @var class-string<Exception> $exceptionClass */
dumpType((new Foo($exceptionClass))->value);

https://phpstan.org/r/94faaba1-0ed2-4d54-8ff0-130a0bf2fb03

Expected output

We expect the value to be of type class-string<Exception>.

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

As always, amazing tool. It makes me more confident on my code. Great job !

@arnaud-lb
Copy link
Contributor

Related: #6505

@ondrejmirtes ondrejmirtes added this to the Generics milestone Feb 27, 2022
@phpstan-bot
Copy link
Contributor

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

@@ @@
-22: Dumped type: string
+22: Dumped type: class-string<Exception>
Full report
Line Error
22 Dumped type: class-string<Exception>

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@1b117f7

@bastien-phi
Copy link
Author

Thanks @ondrejmirtes and @arnaud-lb !

@github-actions
Copy link

github-actions bot commented Apr 1, 2022

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 Apr 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants