josef.van.niekerk
josef.van.niekerk

Reputation: 12121

Preserving auto-completion abilities with Symfony2 Dependency Injection

I'm using PHP Storm as my IDE, but I believe that other IDE's such as Netbeans will have the same issue as I'll explain below.

When using a framework like Symfony2, we have the wonderful world of Dependency Injection added. So objects can simply be instantiated using code like the following snippet:

$myThingy = $this->get('some_cool_service');

This is very handy, as objects are already configured beforehand. The one problem is, that auto-completion breaks entirely in basically any PHP IDE, as the IDE does not know what type the get() method is returning.

Is there a way to preserve auto-completion? Would creating for example an extension of Controller be the answer? For example:

class MyController extends Controller {
    /**
     * @return \MyNamespace\CoolService
     */
    public getSomeCoolService() {
        return new CoolService();
    }
}

and then for application controllers, specify MyController as the base class instead of Controller?

What about using a Factory class, or any other possible methods?

Upvotes: 15

Views: 4443

Answers (5)

Marius Balčytis
Marius Balčytis

Reputation: 2651

I use base Controller class for bundle. You need to annotate the return in method. At least that works on Eclipse.

/**
 * Gets SomeCoolService
 *
 * @return \Namespace\To\SomeCoolService
 */
protected function getSomeCoolService()
{
    return $this->get('some_cool_service');
}

I don't like /*var ... */, because it gets too much into code. I don't like private properties, because you can wrongly assume that services are already loaded.

Upvotes: 6

belhassine
belhassine

Reputation: 151

working with netbeans IDE 7.1.2 PHP

Upvotes: 0

greg0ire
greg0ire

Reputation: 23265

It is more involving, but you can still do this with eclipse PDT:

$myThingy = $this->get('some_cool_service');
/* @var $myThingy \MyNamespace\CoolService */

UPDATE: The example on this page shows you may also use the other way round with phpStorm:

$myThingy = $this->get('some_cool_service');
/* @var \MyNamespace\CoolService $myThingy */

Upvotes: 15

tuxedo25
tuxedo25

Reputation: 4828

I use Komodo Studio, and tagging variables with @var, even inside methods, preserves auto completion for me.

namespace MyProject\MyBundle\Controller;

use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\Request;

class WelcomeController extends ContainerAware
{
    public function indexAction()
    {
        /*@var Request*/$request = $this->container->get('request');
        $request->[autocomplete hint list appears here]
    }
}

Upvotes: 1

kgilden
kgilden

Reputation: 10356

You could define private properties in your controllers

class MyController extends Controller
{
    /**
     * @var \Namespace\To\SomeCoolService;
     */
    private $my_service;

    public function myAction()
    {
        $this->my_service = $this->get('some_cool_service');
        /**
         * enjoy your autocompletion :)
         */
    }
}

Upvotes: 7

Related Questions