code completion and factory pattern in eclipse pdt
Asked Answered
B

4

8

Lets say I have a typical factory pattern in PHP code:

abstract class Model
{
    function m()
    {
    }
}

class model_A
{
    function a()
    {
    }
}

class model_B
{
    function b()
    {
    }
}

function modelFactory($name)
{
    $className = 'model_' . $name;
    $object = new $className();
    // ... do some magic stuff ...
    return $object;
}

I know about the @var and @return phpdoc tags, but is there any magic way so after typing this:

$x = modelFactory('A');

Eclipse will know $x is an instance of model_A?

Can I define somewhere a fixed vector of strings like this:

"modelFactory('A')" => "new model_A()"
"modelFactory('B')" => "new model_B()"

For Eclipse to replace in memory before processing with code completion.

Bolshevist answered 8/1, 2013 at 18:27 Comment(8)
Why would you want this? The factory should return "similar" objects, e.g. implements same interface or extends the same super class.Undertake
yes i wanted the example to be super simplified. i have edited it now and added super class. I know i can add @return Model above modelFactory but is it possible to get both m() and a() as a method hints for $xBolshevist
also: large part of my project depends on factory pattern and lack of code completion is so annoying i will need to refactor it if this is not possible.Bolshevist
Sorry, but I don't know the answer to your question. However, in the simple case above I would rename a() and b() to m() and have the model_A and model_B extend the Model class. In that way there would not be necessary to for outside users to know the difference between model_A and model_BUndertake
thanks but assume a(), b() and m() are doing totally different things in real code. Just like in my real code child classes are extending superclass by adding few functions to it each.Bolshevist
Add "@return Model|model_A|model_B" to modelFactory. AFAIK, there is no better way unfortunately.Crystallize
@Crystallize thanks i dint know about that. better than nothing. Please add it as answer so i could accept if no one will find better way.Bolshevist
In PHPStorm I use the following: /** @param model_A $x */ right before instantiating $x. Works perfectly for me, but not sure if Eclipse does this.Voelker
K
1

Short answer, this is not possible in the way you want it.

As you've already rightfully pointed out in another comment you should use @var manually to achieve code completion goodness, because it's impossible for the editor to understand the concept of programming patterns by itself

Although theoretically possible, manually authoring rules for type inference just seems backwards imho (let alone the required changes to the Docblock notation itself).

Kashakashden answered 17/1, 2013 at 8:44 Comment(0)
H
1

The only IDE that supports this is PHPStorm (via a .phpstorm.meta.php file).

I filed a feature request asking to support this. You can vote for it at https://bugs.eclipse.org/bugs/show_bug.cgi?id=463110

Humility answered 10/12, 2015 at 11:38 Comment(0)
I
0

i use eclipse and pdt from almost 3+ years,but dont have perfect answer for your question

i think Zend Studio 9 can solve your problem,for code completion it is far more better than pdt.

http://www.zend.com/en/products/studio/downloads

Iinde answered 11/1, 2013 at 6:22 Comment(1)
Hmm, i could try Zend. But why do you think it will handle? They are also recommending @var for factory model forums.zend.com/viewtopic.php?f=59&t=5454 so guess there is also no magic way for this.Bolshevist
A
0

Currently, I'm not aware of an IDE/Editor that does this either, but I'm keeping track of the same functionality for PhpStorm (they're currently 'working' on it): http://youtrack.jetbrains.com/issue/WI-6027

Acrolein answered 26/1, 2013 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.