Skip to content

Instantly share code, notes, and snippets.

@bcremer
Last active December 28, 2021 23:15
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcremer/be2a558a452d4f45dc04994ffe7b8292 to your computer and use it in GitHub Desktop.
Save bcremer/be2a558a452d4f45dc04994ffe7b8292 to your computer and use it in GitHub Desktop.
<?php
// see https://confluence.jetbrains.com/display/PhpStorm/PhpStorm+Advanced+Metadata
namespace PHPSTORM_META {
override(\Doctrine\ORM\EntityManager::find(0), map([
'' => '@',
]));
override(\Interop\Container\ContainerInterface::get(0), map([
'' => '@',
// custom mappings
'doctrine.entitymanager.orm_default' => \Doctrine\ORM\EntityManager::class,
'doctrine.connection.orm_default' => \Doctrine\DBAL\Connection::class,
'Logger' => Logging\Logger::class,
// default ZF services
'Application' => Zend\Mvc\ApplicationInterface::class,
'ControllerManager' => Zend\Mvc\Controller\ControllerManager::class,
'HttpRouter' => Zend\Mvc\Router\Http\TreeRouteStack::class,
'ValidatorManager' => \Zend\Validator\ValidatorPluginManager::class,
'ViewRenderer' => Zend\View\Renderer\RendererInterface::class,
]));
}

PHPStorm ContainerInterface return type detection

PhpStorm does not provide return type detection for Interop\Container\ContainerInterface-like DI-containers as used in Zend Framework or Zend Expressive for example.

This can be archived by adding a PhpStorm Advanced Metadata configuration file to your project.

Howto

  • Create a file named .phpstorm.meta.php in your project root directory
  • Add custom mappings if a service id is not equal to it's classname
<?php
namespace ZFApplication;
use Zend\ServiceManager\Factory\FactoryInterface;
use Interop\Container\ContainerInterface;
class TimeMachineFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null): TimeMachine
{
$em = $container->get('doctrine.entitymanager.apiv3'); // $em is detected as instance of Doctrine\ORM\EntityManager
$fluxKomponsator = $container->get(FluxKompensator::class) // $fluxKomponsator is detected as instance of ZFApplication\FluxKompensator
return new TimeMachine($fluxKomponsator, $em);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment