Skip to content

Instantly share code, notes, and snippets.

@rskuipers
Created August 31, 2016 10:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rskuipers/66c2b8e85c733915d38184aa7e40ee91 to your computer and use it in GitHub Desktop.
Save rskuipers/66c2b8e85c733915d38184aa7e40ee91 to your computer and use it in GitHub Desktop.
<?php
// This file is not a CODE, it makes no sense and won't run or validate
// Its AST serves IDE as DATA source to make advanced type inference decisions.
namespace PHPSTORM_META {
$STATIC_METHOD_TYPES = [
\Zend\ServiceManager\ServiceLocatorInterface::get('') => [
// STATIC call key to make static (1) & dynamic (2) calls work
"special" instanceof \Exception,
// "KEY" instanceof Class maps KEY to Class
],
];
}
@rskuipers
Copy link
Author

rskuipers commented Aug 31, 2016

When using a Service Manager and you supply the class name as argument for get(), you can use the above META to get automatic autocomplete working. Just put this file in the root of your project (the root being the PhpStorm project root), it will be picked up instantly).

Make sure your Service Manager is type-hinted and the type-hint matches the class name used in the META file.

function (\Zend\ServiceManager\ServiceLocatorInterface $serviceManager) {
    $order = $serviceManager->get(Order::class);
    $order->getNumber(); // This now autocompletes nicely
}

Zend Service Manager is used as an example but this works for pretty much anything.

Source: https://confluence.jetbrains.com/display/PhpStorm/PhpStorm+Advanced+Metadata

@Ocramius
Copy link

Note: this can be used also with any PSR-11 container 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment