Symfony services with autocomplete in IDE

I use PhpStorm as my IDE for PHP development. It has a nice framework integration for Symfony but lacks the autocomplete when you get services through the container:

$mailChimpService = $this->get('hype_mailchimp');

To let the IDE know which kind of object a variable is you can use comments like:

/** @var $mailChimpService \Hype\MailchimpBundle\Mailchimp\MailChimp */

UPDATE: You can also the the Symfony Plugin for PHPStorm. It will know which class is behind the service and also do the autocomplition. It is therfore a complete replacement for the rest of the article.

But this has to be done every time which isn’t that nice. Therefore I moved it to a extra method:

  * Get MailChimp service *
  * @return \Hype\MailchimpBundle\Mailchimp\MailChimp 
protected function getMailChimpService(){  
    return $this->get('hype_mailchimp'); 

To make it as reusable as possible I create a extra controller class MyProjectController.php (which extends from Controller.php), insert the method there and let all my controller classes extend from this class.

That way when I call

$mailChimpService = $this->getMailChimpService();

I get a nice autocomplete


A little tip when you don’t know which class hides behind a service name like hype_mailchimp:

Run the following command to see a list of all public services and there classes.

$ php app/console container:debug
comments powered by Disqus