1

I have the following class structure

Class Structure

I'm using Symfony2's service to manage the BaseHandler and BaseManager classes

class BaseHandler implements IHandler
{
    /**
     * @var IManager
     */
    protected $manager;

    ...

    public function setManager(IManager $manager)
    {
        $this->manager = $manager;
    }
    ...
}

All works fine, but the problem is when I try to use some methods of SpecializedManager class and the PhpStorm's suggestions doesn't show their methods, only methods belonging to IManager.

For example in my controller

public function indexAction(){
    $handler = $this->get('appbundle.handler.specialized_handler');
    $items = $this->manager->mySubClassMethod(); // PHPStorm shows warning
    ...
}

I don't know how to set in the handler subclass the annotation of var manager to change to something like this @var SpecializedHandler $manager, neither another alternative ways for methods appear in the suggestions.

NOTE: the base class or extended for multiples specialized subclasses.

LazyOne
  • 158,824
  • 45
  • 388
  • 391
rck6982
  • 229
  • 2
  • 17
  • Have a look at [this](http://stackoverflow.com/questions/24105260/php-upcasting-object) SO question. – Jeroen Heier Jul 23 '16 at 06:01
  • 1
    You may need to use intermediate variable (`$manager = $this->manager; $manager->mySubClassMethod()`) which you can typehint via inline `@var` as you have suggested. Another approach (better one, as it relies on PHPDoc alone) is to re-declare `$manager` property in current/parent class via `@property` (must be located in PHPDoc for that class). All of this because with PHPDoc (and PhpStorm's static analysis) you can type hint only 1st-level objects (`e.g. `$this`, `$manager` etc) and cannot do n-level (`$this->manager` etc). – LazyOne Jul 23 '16 at 10:45
  • 1
    There also other approaches like: introduce `getManager()` method in this/parent class which will contain correct `@return` tag and use it instead of `$this->manager` in your code. – LazyOne Jul 23 '16 at 10:48

1 Answers1

0

As you're using PHPStorm, you might install the Symfony2 plugin for PHPStorm: https://plugins.jetbrains.com/plugin/7219?pr=phpStorm

With this plugin installed (and enabled on the project), you should automatically have full auto completion on services.

Christian
  • 199
  • 1
  • 11