6

I am starting to develop with symfony 2 and it uses a lot dependency injection. I would like to know if is there any way that makes netbeans detect the type of object based on the string and auto complete with their methods?

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

Something like it, could be useful for zendframework 2 also.

I don't want to create new methods in the controller and nor use /* @var $var Symfony...*/, I would automatic detection.

4 Answers 4

4

As far as I know, there's no way for an IDE to detect the type of the object your container returns. My solution is to wrap those calls to the container into private getter functions. IMHO this improves code readability as well – especially, if you do this call more than once per class.

/**
 * @return \Doctrine\Bundle\DoctrineBundle\Registry
 */
private function getDoctrine()
{
    return $this->container->get('doctrine');
}
2
  • Yes I know this solution but It's not yet what I am looking for. I opened an issue suggesting it at netbeans.org/bugzilla/show_bug.cgi?id=226725
    – dextervip
    Commented Feb 26, 2013 at 21:49
  • 1
    This is what I do as well, +1 - though in general protected should be preferred to private unless there is a specific reason why a (theoretical) child class should not use the getter.
    – halfer
    Commented Feb 27, 2013 at 12:05
0

The IDE "PhpStorm" permits to suggest "use" declarations. And this IDE propose specific features for Symfony2 and Drupal !

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

Not free but power full enough to reduce time developpement time (and time is money...)

Enjoy : )

0

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

( Walk around ) When comes to Symfony services:

Instead of

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

use

$doctrine = $this->getDoctrine();

As you can see, Symfony allows you to access most of it services directly from $this variable. NetBeans will know what auto completion to use.

Lets have a look why this works (inside Controller class)

enter image description here

It is possible because Controller class imports Registry class with USE statement,

use Doctrine\Bundle\DoctrineBundle\Registry;

and then in method comment annotation it declares the returning object type with

/*
* @return Registry
*/

If you call $this->container->get('doctrine'); directly then auto completion will be get omitted and you will have to use whats below.

( Answer ) No magic auto completion works so far. Use Php Storm (it does what you request). For those who pick to stick with NetBeans you need to use manual annotation like in example below:

We can point NetBeans to a class it should be using for auto completion.

1) In terminal from project directory search for service you want to import:

php bin/console debug:container

If you know what you looking for use this instead:

php bin/console d:container | grep doctrine

...

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

...

2) If this is not a service use get_class() PHP build in function to get class name of the object it particular variable. Or use reflection class. It's up to you.

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

use Doctrine\Bundle\DoctrineBundle\Registry;

4) Now wen we know what is the class name of the object instance in particular variable we are ready to inform NetBeans about what we know by using comment annotations so that it can enable auto completion.

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

Now auto completion is enabled. Type

    $doctrine->|

then press Ctrl+Space. See the image below:

enter image description here

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.