Faster, better, and more efficient type hinting fo

2019-03-16 23:35发布

问题:

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

回答1:

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:

  1. The container needs to implement the Container Interop project's Container\Interop\ContainerInterface which ZF2's ServiceManager already does.

  2. 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



回答2:

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