7

In PHPStorm, I can type-hint a variable this way:

/** @var Point $point */
$point->x();

However, say I inherited a variable from a parent class, and want to type-hint it:

class PointProxy extends Proxy
{
    public function x()
    {
        ...

        /** @var Point $this->geometry */
        return $this->geometry->x();
    }
}

This doesn't work, PHPStorm acts as if I had type-hinted $this, and not $this->geometry.

Is there a way to make such a type-hint work without redeclaring the $geometry property in the subclass, or is this unsupported?

3
  • 1
    You cannot use such PHPDoc for complex (2nd level in hierarchy) objects -- only first level. This /** @var Point $this->geometry */ makes no sense .. as PHPDoc comment will be applied to first level ($this in this case) only. Right now you may try to declare it via @property in PHPDoc comment for the class -- should only make sense if it's a public property and not private/protected. Commented Mar 16, 2015 at 12:56
  • If at all possible the property should be type hinted in the parent class; this should propagate through to child classes. Commented Mar 19, 2015 at 9:56
  • 1
    @deceze It is type-hinted in the parent class, but as Geometry. The child class should document that it is in fact a Point, descendant of Geometry. Commented Mar 19, 2015 at 18:38

3 Answers 3

11

Try this code. Also you can press alt+enter at undefined properties and select Add @property it will help you to create phpdoc faster.

/**
 * @property Point $geometry
 */
class PointProxy extends Proxy {
  public function x() {
    return $this->geometry->
  }
}

add property completion

Sign up to request clarification or add additional context in comments.

3 Comments

The problem with @property, as mentioned by @LazyOne, is that the property is considered public, which is not the case here, and might bring confusion!
If this property is virtual and dynamic (we access it via __get) - it is public in real. If you want to make it protected or private declare it in class.
It is protected in the parent class!
0

If the parent object types the property as Geometry but you want it typed as a Point (which descends from Geometry) in your child class, I would recommend creating an accessor in your child class. Possibly with some type checking.

class PointProxy extends Proxy
{
    /**
     * Access the geometry object on parent class as a Point
     *
     * @return Point
     */
    private point()
    {
        if(!is_a($this->geometry, 'Point'))
        {
            // Log an error or something, this is not a state we should be in
        }
        else
        {
            return $this->geometry;
        }
    }

    public function x()
    {
    ...

        return $this->point->x();
    }
}

Comments

0

I ran into a very similar problem. I had a generic storage class that dealt with database operations and then a proxy class on top which proxied all storage class methods through a try catch (using __call()) so that I could handle exceptions in one location.

Now whenever I accessed the storage instance like $storage->retrievePhoto($id), the PHPStorm IDE could not typehint for me. My solution involved adding another class name annotation to the $storage object.

For example, see below. Since the specific proxy class is really just a wrapper over the original storage class, it doesn't present any problems although it is still not 100% to my liking but it works.

final class PhotoRepository
{
    /**
     * @var \Repositories\Photos\PhotoStorage
     *      \Repositories\Photos\PhotoStorageExceptionHandlerProxy
     */
    private $storage;

    /**
     * @param \Repositories\Photos\PhotosStorageExceptionHandlerProxy $storage
     */
    public function __construct(PhotosStorageExceptionHandlerProxy $storage)
    {
        $this->storage = $storage;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.