0

I need to add $variable in my php return document for example I have this function :

/**
* @return \Panel\Model\{$Service}
*/
public function getService($Service){
    // I call service `foo` from Model folder
}

I see this post : What's the meaning of @var in php comments but it has no information about how to do it , and also study @var-phpdoc but that has nothing for me.

if you ask me why I should do that , because I want use phpStorm Ctrl+Click advantage on $this->getService('foo')->bar()

thanks in advance

4
  • 3
    It does work like that. In PhpStorm you may utilize Advanced Metadata functionality for such dynamic type resolving. But PHPDoc comments have to be more real & concrete (e.g. return some common interface, a parent class or just mixed type). Commented Jan 14, 2018 at 23:32
  • thanks @LazyOne you mean adding {$Service} is the standard way to add variable in PHPDoc comment? and about phpStorm I'll test it and tell the result tomorrow . Commented Jan 15, 2018 at 17:28
  • Why not using php interfaces for that purpose? Commented Jan 16, 2018 at 8:48
  • "you mean adding {$Service} is the standard way to add variable in PHPDoc comment?" No -- I mean that \Panel\Model\{$Service} is WRONG and not supported. From PHPDoc side -- use interface/parent class (if applicable) .. or maybe just mixed (which means "any type" basically). From IDE side (for code completion purposes -- Advance Metadata should help -- look at output generated by github.com/barryvdh/laravel-ide-helper as an example) Commented Jan 16, 2018 at 20:58

3 Answers 3

2

As LazyOne said in the comments already PHP interfaces should solve your problem. You can 't use variables in PHPDoc formatted comments. Sure, if you use an IDE like PHPStorm with a plugin that enables the usage of variables in PHPDoc comments, the problem is solved for yourself. What, when other developers, which don 't use PHPStorm or the relevant plugin, want to work in the same project? In my view you should use php native functionality to solve your issue.

Here 's a short example how to use interfaces.

declare('strict_types=1');
namespace Application\Model;

interface ModelInterface
{
    public function getFoo() : string;

    public function setFoo() : ModelInterface;
}

The only thing you have to do now is using this interface with your models like in the following example.

declare('strict_types=1');
namespace Application\Model;

class FooModel implements ModelInterface
{
    protected $foo = '';

    public function getFoo() : string
    {
        return $this->foo;
    }

    public function setFoo(string $foo) : ModelInterface
    {
        $this->foo = $foo;
        return $this;
    }
}

As you can see the FooModel class implements the ModelInterface interface. So you have to use the methods declared in the interface in you model class. This means, that your getService Method could look like the following example.

/**
 * Some getter function to get a model
 * @return \Application\Model\ModelInterface
 */
public function getService($service) : ModelInterface
{
    return $service->get(\Application\Model\Foo::class);
}

Your IDE knows now which methods the returned class can use. It allows you to use chaining and some more features. While typing your IDE should know now, that the returned class can use getFoo and setFoo methods. Further the setFoo methods enables comfortable chaining for calls like ..

// variable contains the string 'foo'
// your ide knows all methods
$fooString = $this->getService($serviceLocator)->setFoo('foo')->getFoo();
Sign up to request clarification or add additional context in comments.

Comments

0

Are you using Symfony? Then you could use the Symfony plugin that solves this problem for you. For other frameworks, there should be similar solutions. If you use your own framework, you need to write such a plugin on your own, as PhpStorm can not resolve the given class otherwise.

1 Comment

I am using zend-framework2 , do you now how that plug in works ?
0

I think what you are looking for is phpdoc.

https://docs.phpdoc.org/guides/docblocks.html

/**
 * @param string $Service This is the description.
 * @return \Panel\Model\{$Service}
 */ 
public function getService($Service){
    // I call service `foo` from Model folder
}

1 Comment

I already add phpdoc link in my post but this doc : @return \Panel\Model\{$Service} has no sense to editor and it seems we add this : @return \Panel\Model

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.