How to automatically complete methods from Symfony 2 DI in netbeans

I am starting to develop using symfony 2 and it uses a lot of dependencies. I would like to know if there is a way that netbeans detects the type of an object based on a string and automatically complements their methods?

For example, $this->container->get('doctrine') returns an instance of Doctrine\Bundle\DoctrineBundle\Registry . In the container, the doctrine key corresponds to Doctrine\Bundle\DoctrineBundle\Registry .

Something like this might be useful for zendframework 2 as well.

I do not want to create new methods in the controller and not use / * @var $ var Symfony ... * /, I would automatically detect.

+6
source share
4 answers

As far as I know, there is no way for the IDE to determine the type of object returned by your container. My solution is to wrap these calls in a container in private getter functions. IMHO also improves code readability - especially if you make this call more than once in a class.

 /** * @return \Doctrine\Bundle\DoctrineBundle\Registry */ private function getDoctrine() { return $this->container->get('doctrine'); } 
+4
source

The PhpStorm IDE allows you to offer "use" ads. And this IDE offers special features for Symfony2 and Drupal!

edited by JetBrains: http://www.jetbrains.com/phpstorm/

Not free, but strong enough to reduce development time (and time is money ...)

Enjoy :)

0
source

Phpstorm:

 $foobar= $this->get('foobar'); //returns mixed /* @var \MyNamespace\FooBar $foobar*/ 

or

 $foobar= $this->get('foobar'); //returns mixed /* @var FooBar $foobar*/ 

You can do this with eclipse PDT:

 $foobar= $this->get('foobar'); //returns mixed /* @var $foobar \MyNamespace\FooBar*/ 
0
source

(Walk) When comes to Symfony services:

Instead

 $doctrine = $this->container->get('doctrine'); 

using

 $doctrine = $this->getDoctrine(); 

As you can see, Symfony allows you to access most of its services directly from the $this variable. NetBeans will know which auto-complete to use.

Let's see why this works (inside the controller class)

enter image description here

This is possible because the controller class imports the registry class with the USE statement,

 use Doctrine\Bundle\DoctrineBundle\Registry; 

and then in the annotation of the method comment, it declares the type of the returned object with

 /* * @return Registry */ 

If you call $ this-> container-> get ('doctrine'); immediately after that, autocompletion will be omitted, and you will have to use something below.

(Answer) Magic auto-complete is not working yet. Use Php Storm (it does what you request). For those who want to stick with NetBeans, you need to use manual annotation, as in the example below:

We can point NetBeans to the class that it should use for automatic completion.

1) In the terminal from the project directory, find the service you want to import:

php bin / console debug: container

If you know what you're looking for, use this instead:

php bin / console d: container | Doctrine grep

...

doctrine ---------------------------------------------- --- ------- Doctrine \ Bundle \ DoctrineBundle \ Registry

...

2) If this is not a service, use the get_class () PHP built-in function to get the class name of the object, which it represents a specific variable. Or use a reflection class. It depends on you.

3) Once you know the class name, declare a USE statement for better readability

 use Doctrine\Bundle\DoctrineBundle\Registry; 

4) Now that we know what the class name of the object instance in a particular variable is, we are ready to tell NetBeans what we know using comment annotations so that it can turn on auto-completion.

  /** * @var $doctrine Registry */ $doctrine = $this->container->get('doctrine'); 

AutoFill is now enabled. A type

  $doctrine->| 

then press Ctrl + Space . See image below:

enter image description here

0
source

All Articles