it's a bit boring to adding this in each ctp files with phpStorm :
/** #var Class $this */
I can not find a way to set globally the variable "$this" to the View class in ctp files, is it possible ?
AFAIK that's not possible (a PHPStorm plugin could of course add that functionality).
You could report this as an enhancement request for the CakePHP bake shell, having this added automatically to the baked view templates wouldn't hurt.
For better cross IDE compatibility it should however probably better be in
/* #var $this Type */
format.
CakePHP 2.x > https://github.com/cakephp/cakephp/issues
CakePHP 3.x > https://github.com/cakephp/bake/issues
Related
I've created new project using PhalconPHP 3.1.2. Everything works fine, but I have problem with IDE. In PhpStorm I've added ide/stubs/Phalcon from phalcon-devtools 3.1.2 as external libraries to dispose of warnings and errors.
But there is still one problem: in app/config/router.php (standard catalog structure created by devtools) I got line $router = $di->getRouter(); (also created by devtools) with warning: Method getRouter not found in Phalcon\Di\FactoryDefault.
There is no method in this class indeed: https://github.com/phalcon/phalcon-devtools/blob/3.1.x/ide/stubs/Phalcon/di/FactoryDefault.php -> https://github.com/phalcon/phalcon-devtools/blob/3.1.x/ide/stubs/Phalcon/Di.php
Now I don't have autocomplete of router's methods and this is problem for me. Did I do something wrong?
First of all -- I'm not familiar with Phalcon. These are my comments based on general PHP/PhpStorm experience.
So .. if you look at that PHPDoc in the last link you gave (stubs for Di.php) you will notice the code example there that has $request = $di->getRequest();.
If you just copy-paste that whole sample you will get the same "method not found" error ... as getRquest() is transformed at run time via magic __get() method.
The possible solution here is to create your own stub for your $di:
Create new class that will extend original Di, e.g.
class MyDi extends \Phalcon\Di
Add all those getRoute() / getRequest() etc methods there (could be real methods with empty bodies as in Phalcon's stub files .. or via #method PHPDoc tag)
Place such file anywhere in the project -- it will be used by IDE only.
When you are using your $di -- typehint it with your class, e.g.
/** #var \MyDi $di */
$di = new Di();
In the above code during runtime $di will be an instance of Di but for PhpStorm during development it will be MyDi so all type hints are in place.
As possible alternative -- instead of using magic via $di->getRoute() .. try $di->getShared('route') or similar.
If you use PhpStorm's Advanced Metadata functionality it will allow to get correct type based on parameter value.
$di->getRouter() is magic method, Phalcon\Di implements __call method and it gets router service this way if you use getRouter().
If you want you can use PHPStorm metadata and use $di->get() https://www.google.pl/search?client=opera&q=phpstorm+metadata&sourceid=opera&ie=UTF-8&oe=UTF-8
I have been looking for a way to do this for months. I am one of those developers that loves autocompletion. For every Service Locator call in zend framework 2 I type hint with the following:
Without global hinting file
/** #var \Module\Service\SuperService $superService */
$superService => $this->getServiceLocator()>get('\Module\Service\SuperService');
$superService->coolFunction();
This works, but the code can get messy when you start getting 2-4 Services in a single Controller. I am trying to find a better way to use PHP Storm and type hinting for service locator calls
About a month ago PHP Storm released a new feature that allows for a static file to be used for type hinting. I personally use this just for the service locator but it can be used for many other things that PHP Storm can't follow because it's not magic.
With global hinting file
$superService = $this->getServiceLocator()->get('\Module\Service\SuperService');
$superService->coolFunction();
This does a few things for us as developers:
Teamwork (others will be able to see all services at once from all
modules)
Coding
Speed
Code neatness
Code Standards
Click to go to the class faster
What to do
In the project ROOT (same place as composer.json), there should (create if not) be a file called .phpstorm.meta.php This file contains the static hinting and which instance they use.
To add your own, simply toss it inside the existing file
WARNING - If you declare the wrong class, you will end up writing the code wrong, and confused... it has happened to me a bunch
I am including my current Config which is personal to my project but gives more of an understanding of what to do.
namespace PHPSTORM_META {
$STATIC_METHOD_TYPES = [
\Zend\ServiceManager\ServiceLocatorInterface::get('') => [
/**
* Common services
*/
'doctrine.entitymanager.orm_default' instanceof \Doctrine\ORM\EntityManager,
'Zend\Db\Adapter\Adapter' instanceof \Zend\Db\Adapter\AdapterServiceFactory,
/** Custom to Project */
'RiotAdapter' instanceof \GameService\Service\RiotAdapter,
'SmiteAdapter' instanceof \GameService\Service\SmiteAdapter,
'GameService' instanceof \GameService\Service\GameService,
],
];
}
If you would like more information on this, you can look # a confluence post from PHP Storm here
While this doesn't help you now, it's useful for you to know that we are close to supporting this out of the box, with a few provisos:
The container needs to implement the Container Interop project's Container\Interop\ContainerInterface which ZF2's ServiceManager already does.
You need to use the ::class super-global-hyper-magic-contant to name and retrieve your classes. In your case, simply replace the string in your get call with Module\Service\SuperService::class (available from PHP 5.4 and above).
I'm expecting this functionality to be available in our next release, PhpStorm 2016.2 which is due around the summer.
Gary
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.
In CakePHP 1.3 I usually add model property definitions and PHPDoc to my models and controllers like so:
/**
* #var Vegetable
*/
public $Vegetable;
In Netbeans this gives "Intellisense"-style autosuggestion, displays PHPDoc information, and is generally a boon. Unfortunately in CakePHP 2 it seems that this causes the model lazy-loading to fail because the magic methods __isset() and __get() are never called for properties that already exist.
The lack of autosuggest would be a bitter pill to swallow - has anyone come across this issue, and can you see any workarounds?
Use #property annotation (in class decription).
I use Kohana 3.x as my Webapplication-Framework (which uses the MVC pattern) and use Propel as my ORM. Within my Controller I create an object that represents the profile that owns the current session:
$this->currentProfile = ProfileQuery::create()->findPK($profileId);
I pass the object to the views, that I use:
View::set_global('myProfile', $this->currentProfile); // c
Now I can use the object "myProfile" within my Views. But the problem is, that within this views, neither Netbeans nor Eclipse know the class of the object. So I cant use Intellisense anymore (which was one of the key-features for using Propel in the first place). So please help me: How can I tell Eclipse and/or Netbeans of which class my object "myProfile" is?
Netbeans solution: put this at the beginning of your template: /* #var $myProfile Profile */
Or: type vdoc and press tab.