PHPDOC Abstract class does not insert extended class type <T> into methods returns

58 views Asked by At

I've got following code:

<?php

/**
 * @template T
 */
abstract class A {

    /**
     * @return T
     */
    public function read() {
        // return statement 
    }
}

class C {}

/**
 * @extends A<C>
 */
class B extends A {}

$b = new B();
$b->read();

Inside VSCode, when i hover over read method, i would like it to return C class back from method, currently comes @return T - it ignores @extends A<C> value.

I know there's a way to achieve this with @method, but It also makes doubled hints about a function if i declare it below @template T, and method declaration needs to be changed at 2 places if something changes.

Is it possible to achieve it somehow?

1

There are 1 answers

0
M-Zoldak On

As i realized right now, it works well. It didn't worked in my Code, because of declared class-string annotation, which seemed to override T value from @extends.

    /**
     * @var class-string<T> $className
     */
    private string $className;

After defining it as normal string property, just for use case in class, method returns right object.