PHPStorm Automatically populate array keys (dynamically inserted)

I use a Pimple dependency injector, and every time I use a container dependency, I can't help checking the spelling of the key used to get the dependency:

$ioc = new Pimple();

// 1. Define some object
$ioc["some-key"] = $ioc->share(function($c){ /* ... */});

// 2. Use it
$ioc["som... // Open config file and check spelling...

Does PHPStorm have some way to find these properties and provide auto-complete? I examined the definition of all these keys using something like

define('SOME_KEY', 'some-key');

// ...

$ioc[SOME_KEY] = $ioc->share(/* ... */);

but I wonder if there is a better way.

Edit

Here is a sample code:

// project_root/library/App/Injector/Ioc.php
require_once "Pimple.php";

/** @var array|Pimple $ioc */
$ioc = new Pimple();

$ioc["version"] = "1.0.1650.63";

$ioc["location-service"] = $ioc->share(function ($c) {
     return new Application_Service_Location();
   }
);

It turns out that line completion works fine, regardless of whether I include the / ** @var array | Pimple $ ioc * / before declaring $ ioc in the same file since $ ioc is declared. However, since I use the Zend Framework, I usually use $ ioc:

// project_root/Application/Bootstrap.php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
   protected function _initInjector() {
     $ioc = null;
     require_once LIBRARY_PATH . "/MFM/Injector/ioc.php";
     Zend_Registry::set("ioc", $ioc);
   }
}

// project_root/Application/Controllers/SomeController.php
class Application_Controller_SomeController extends Zend_Controller_Action {
   public function IndexAction() {
      /** @var Pimple $ioc */
      $ioc = Zend_Registry::get("ioc");

      // No IDE assistance for the string "location-service"
      $service = $ioc["location-service"];
   }
}
+4
2

DynamicReturnTypePlugin PhpStorm dynamicReturnTypeMeta.json:

{
    "methodCalls": [
        {
            "class": "\\MyOwnWrapperOfDIContainer",
            "method": "get",
            "position": 0
        },
        {
            "class": "\\Pimple\\Container",
            "method": "offsetGet",
            "position": 0
        }
    ]
}

: , Pimple\Container ( ArrayAccess::offsetGet), PhpStorm , . . :.

$c = new Pimple\Container;
$c['MyClass']->/*here autocompletion works*/methodOfMyClass();

:

$myClassInstance = MyOwnWrapperOfDIContainer::get(MyClass::class);
$myClassInstance->methodOfMyClass(); // autocompletion works here

Pimple , , . E. g., :

$c = new Pimple\Container;
$c['my-favourite-var'] = new MyClass(1);
$c[MyClass::class] = new MyClass(2);
$c['my-favourite-var']->/*here autocompletion doesn't work*/methodOfMyClass();
$c['MyClass']->/*here autocompletion works*/methodOfMyClass();

:

class MyFavouriteVar extends MyClass;
$c[MyFavouriteVar::class] = new MyFavouriteVar(2);
// or even like this:
$c[MyFavouriteVar::class] = new MyClass(2);
$c[MyFavouriteVar::class]->/*now autocompletion works fine*/methodOfMyClass();

...

1

: http://confluence.jetbrains.com/display/PhpStorm/PhpStorm+Advanced+Metadata

2

https://github.com/Sorien/silex-idea-plugin

edit 3

, :

class MyPimple extends Pimple\Container
{
    public function get($type, $desc = null) {
        return $this[$type . (isset($desc) ? ':' . $desc : '')];
    }
}
$c = new MyPimple;
$c[MyClass::class] = new MyClass('default');
$c[MyClass::class . ':' . 'my-special-value'] = new MyClass('special');
$c->get(MyClass::class, 'my-special-value')->/*autocompletion should work*/methodOfMyClass();

dynamicReturnTypeMeta.json :

{
    "methodCalls": [
        {
            "class": "\\MyPimple",
            "method": "get",
            "position": 0
        }
    ]
}
+2

".phpstorm.meta.php" . .phpstorm.meta.php:

<?php
namespace PHPSTORM_META {
    override( \Container::get(0), map([]));
}

\Container:: get - , :

\Container::get('My\Super')

\Container::get(My\Super::class)

.

0

All Articles