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

[BUG]: Redundant generic return annotations #16661

Open
ZanMaticPratnemer opened this issue Nov 7, 2024 · 0 comments
Open

[BUG]: Redundant generic return annotations #16661

ZanMaticPratnemer opened this issue Nov 7, 2024 · 0 comments
Labels
bug A bug report status: unverified Unverified

Comments

@ZanMaticPratnemer
Copy link

Describe the bug
Return annotations for some methods in the Model class say that the method returns T|ModelInterface<T>, where T is defined as @template T of static so the same as @template T of Model. Since Model implements ModelInterface, the return type of ModelInterface<T> is redundant and can cause problems with static analyzers if you were to try to call a method implemented in the child class of Model on the return value from methods that have this redundant type specified - for example findFirst().

To Reproduce
In the base model class of my application:

    /**
     * @return null|T
     */
    public static function get(string $id)
    {
        $ret = static::findFirst([
            'id = :id:',
            'bind' => [
                'id' => $id,
            ],
        ]);

        if ($ret instanceof Row)
        {
            throw new \LogicException("get() found multiple entries for id {$id} on model " . static::class, 500);
        }

        return $ret;
    }

image
Now I could specify that the get() method returns null|T|ModelInterface<T>, but that causes problems down the line, when I try to call model specific methods on the result of get().

This is just an example for the findFirst() method, but couldn't every method that returns a ModelInterface instance be annotated to return T instead since T in this scope always implements ModelInterface?

I think that the correct annotation for this specific method should be the following:

/**
 * @return T|\Phalcon\Mvc\Model\Row|null
 */

Expected behavior
There should be no static analysis error in this code.

Details

  • Phalcon version: 5.8.0
  • Phalcon ide stubs: v5.8.0
  • PHP version: 8.3
  • PHPStan version: 1.12.6
@ZanMaticPratnemer ZanMaticPratnemer added bug A bug report status: unverified Unverified labels Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug report status: unverified Unverified
Projects
None yet
Development

No branches or pull requests

1 participant