define definition of a variable in phpstorm in (php) yii application - php

in a Yii Application in PHPStorm i have the following Code:
$datasource = Yii::app()->someComponent;
PHPStorm is now not able to find the definition of method parameters in autocomplete, if i write
$datasource->aMethod(
Is there a way to tell phpstorm the source in order to have a working autocomplete?, e.g.
// #definition components/SomeComponent.php
$datasource = Yii::app()->someComponent;
Thanks,
markus

You could use a PHP Doc comment to tell PHPStorm which type the var has.
/** #var SomeClassName $datasource */
$datasource = Yii::app()->someComponent;

There's better solution. One which makes PhpStorm able to assign Yii::app()->someComponent to class for entire project.
Put in CAplication's php doc (above class):
/**
* #property Someclass $someComponent.
*/
Solved with one line of code. It works with PhpStorm 9.0, haven't tested earlier versions.

Related

Symfony2 comments above variables and functions

I begun learning Symfony2 one of the things that struck me are the numerous comments above variables and functions like so:
variables example:
/**
* #var string
*/
private $baseUrl = 'news/';
OR:
/**
* #var FeedRepository
*/
private $feedRepo;
Functions:
/**
* #param ArticleQuery $query
* #return QueryBuilder
*/
public function test(){
}
Could someone explain to me why use these and why do the do ..?
PHPDoc is an adaptation of Javadoc for the PHP programming language. It is still an informal standard for commenting PHP code, but it is in the progress of being formalized. It allows external document generators like phpDocumentor to generate documentation of APIs and helps some IDEs such as Zend Studio, NetBeans, JetBrains PhpStorm, ActiveState Komodo Edit and IDE, PHPEdit and Aptana Studio to interpret variable types and other ambiguities in the loosely typed language and to provide improved code completion, type hinting and debugging.
Try for example to use an IDE like PHPStorm and write the following PHP code:
/**
* #param array $test
*/
function mytest($test) {
// ... nothing here
}
Now when you start typing myte you get the autocompletion to suggest mytest(test : array).
As you can see now PHPStorm knows the type of the $test variable thanks to the PHPDoc above.

phpDocumentor document a variable accesable from every part of code Zend2

Hello i have a bootloader where i define some basis services like
/** #var $serviceManager Zend\ServiceManager\ServiceManager */
$serviceManager = new ServiceManagerComposite(new ConfigurationComposite($configuration['service_manager']));
But this type of documentation is not available trough my legacy code for hinting, as i found out this will define the variable only on tht one php file. But because i use it as a legacy acess point for my code i need it for the whole php project...
Any sugestions what type of documentation would work with the IDE hinting from Netbeans?
You'd probably need to duplicate at least the first use of $serviceManager with the same #var docblock in each file. If NetBeans is not already resolving scope for you via requires and includes, then this would be the only way to put a context on that "local" variable in the file you're in.
Sangoku is perfectly right!
/* #var $serviceManager Zend\ServiceManager\ServiceManager */
Should do the trick

PHP: documentation / hinting via #var with multiple classes/interfaces

I am making usage of
/* #var $var Type */
really often for NetBeans can then autocomplete methods and stuff in code.
Still I think its a very useful feature but sometimes I got objects of
classes extending one more class and implementing multiple interfaces.
Or I even got a transitive class hierarchy.
I don't know a way to tell NetBeans that it shall be using autocomplete for
all these interfaces and upper-layer parent classes.
I would like to do so for of course every of these interfaces / classes got
dedicated methods (which are defined somewhere in case of interfaces...)
I tried something like this:
/* #var $var TypeA|\TypeB|\TypeC */
because I saw NetBeans will generate a similar documentation for methods returning
different class objects due a switch/case. But this seems to work only for the
#return notation.
I also tried
/* #var $var TypeA|TypeB */
Also not working...
NetBeans will autocomplete the last told Type in this case but not a combination of both/all told classes.
How can I document so my autocomplete works as desired (a summary of methods of all classes /interfaces I listed)?
regards!
If I understand you correctly, you are asking to chain hint through your PHP code.
The problem is netbeans has no way of knowing what an object actually is; unless you tell it. The solution is to use the #property command in your object decleration to forward type define the objects members , be it a class or interface.
/**
#property classMyClass1 $clsMyClass1
#property classMyClass2 $clsMyClass2
*/
class baseClass{
public $clsMyclass1;
public $clsMyClass2;
public function __construct() {
$this->clsMyClass1 = new classMyClass1();
$this->clsMyClass2 = new classMyClass2();
}
}
$foo = new baseClass();
Now when you type your code in netbeans it will know what hinting to display on your last typed object
$foo->clsMyClass1->
So long as each class has a forward property decleration you can chain as long as you want
$foo->class1->class2->class3->...
The above code would need the autoload() function to load the correct class files....
Hope this helps you!

PhpStorm code auto completion in CakePHP

I have a controller in CakePHP 2.2 application.
I use PhpStorm 4.0.1 as IDE.
In MyController.php file I declare this:
/**
* #property MyUtilComponent $MyUtil
*/
Inside my controller, when I write $this-> I can select "MyUtilComponent" from drop down list.
But when I write $this->MyUtilComponent-> no function name option comes to select.
When I write $this->MyUtil->addThis(); and then click to addThis word and "Go to declaration", then PhpStorm goes to method's declaration successfully.
What should I do more to get function names autocompletion ?
Note: The behaviour is same for the core components.
Update your PHPStorm. Version 5.0.4 is currently released and works the way you want it.

code assistance inside included file for a variable defined outside

Let's say I create an object in index.php and save the reference in the variable $obj.
Later in index.php I include/require a file req.php where I want to use a method of the object.
Is there a way to make Aptana 3.0.4 show me the available methods of the object when I type $obj-> inside req.php?
I.e. Aptana should recognize that the variable $obj is already defined higher in the file hierarchy and that it holds a reference to an object.
May be it helps if you add comment before the first var using
/* #var YOUR_CLASS */
The correct syntax in version 3.0.4 is like that:
/**
* #var Foobar
*/
$obj; // You have to call the variable here (redundant, I know)
$obj-> // will code assist the FooBar functions.
Since this is not that great of a syntax, I'm working on having additional support, like with the PDT special #var syntax:
/* #var $obj Foobar */
$obj-> // will code assist the FooBar functions.
Both should be supported in the Studio's next release.
Hope that helps

Categories