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

Generic object factory should be constrained to return an object #1738

Closed
bdsl opened this issue Jun 4, 2019 · 1 comment
Closed

Generic object factory should be constrained to return an object #1738

bdsl opened this issue Jun 4, 2019 · 1 comment

Comments

@bdsl
Copy link
Contributor

bdsl commented Jun 4, 2019

Psalm currently reports no issues with the following code:

class GenericObjectFactory 
{
   /**
    * @psalm-template T
    * @psalm-param class-string<T> $type
    * @psalm-return T
    */
    public function getObject(string $type)
    {
        return 3;
    }
}

But since we have a class-string<T>, I think psalm could know that T must be an object type, and so interpret the code as equivalent to the following, for which it does report InvalidReturnStatement and InvalidReturnType:

<?php

class GenericObjectFactory 
{
   /**
    * @psalm-template T of object
    * @psalm-param class-string<T> $type
    * @psalm-return T
    */
    public function getObject(string $type)
    {
        return 3;
    }
}

This is motivated by a docblock I added to jms/serializer, and the hope that implementing this feature would catch possible errors in implementations of that interface.

@bdsl bdsl changed the title Implementation of generic object factory should be constrained to return an object Generic object factory should be constrained to return an object Jun 4, 2019
@muglug muglug closed this as completed in 395057c Jun 8, 2019
@bdsl
Copy link
Contributor Author

bdsl commented Jun 8, 2019

Thanks!

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

No branches or pull requests

2 participants