Skip to content

@method phpdoc overrides real method definition #135

@lo00l

Description

@lo00l

Suppose I have the following code

BaseClient.php

abstract class BaseClient
{
    abstract protected function getResultClassName(): string;

    /**
     * @return BaseResult
     *
     * @throws RuntimeException
     */
    public function getResult(): BaseResult
    {
        $className = $this->getResultClassName();

        if (!is_subclass_of($className, BaseResult::class)) {
            throw new RuntimeException('Result class must be a subclass of BaseResult.');
        }

        return new $className();
    }
}

Client1.php

/**
 * @method Result1 getResult()
 */
class Client1 extends BaseClient
{
    protected function getResultClassName(): string
    {
        return Result1::class;
    }
}

BaseResult.php

abstract class BaseResult
{

}

Result1.php

class Result1 extends BaseResult
{

}

As you can see I have added @method phpdoc to Client1 class to enable type hinting.

Then, if I do something like

$client = new Client1();
$result = $client->getResult();

and run phpstan, I do not get Missing @throws RuntimeException annotation error. Why? Can I fix it somehow?

Of course I could write this

class Client1 extends BaseClient
{
    public function getResult(): BaseResult
    {
        /** @var Result1 $result */
        $result = parent::getResult();
        
        return $result;
    }

    protected function getResultClassName(): string
    {
        return Result1::class;
    }
}

but there's too much code I think.

Thank you in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions