Autocomplete a Silex Project in PHPStorm

The problem with Silex, and Pimple in general, is that when you do:

$app = new \Pimple\Container();
$app['Foo'] = function () { return new \Acme\Foo(); };
$app['Bar'] = function () { return new \Acme\Bar(); };

PHPStorm has no way of knowing what’s going on in, or how to auto-complete, $app.

I’ve gotten around this in the past by creating an “Inception Proxy” alongside a .phpstorm.meta.php configuration but for a new Silex project I’ve inherited this is not possible.

Pro-tip: If your IDE doesn’t know what’s going on then neither will the poor jerks who inherit your code.

Looking for a solution to this I discovered the PHPStorm Silex Plugin. It’s a bit wonky but it does the job. (sometimes the IDE doesn’t recognize $app and I don’t know why yet.)

For the Silex Plugin to work it requires a manually created configuration file in the project root named “pimple.json”. This file more or less duplicates the functionality of .phpstorm.meta.php but I digress… Pimple.json can be automatically generated using Pimple Dumper.

The format of “pimple.json” looks like:

[
    {
        "name": "routes",
        "type": "class",
        "value": "Symfony\\Component\\Routing\\RouteCollection"
    },
    {
        "name": "request.http_port",
        "type": "int",
        "value": 80
    },
    {
        "name": "charset",
        "type": "string",
        "value": "UTF-8"
    }
]

Once that file is in place, and you jiggle the IDE/Plugin, auto-complete comes alive! Horray for sanity.

3 thoughts on “Autocomplete a Silex Project in PHPStorm”

  1. Hi! Thanks for your post. It seems that you can help me..

    You’ve written:
    “sometimes the IDE doesn’t recognize $app and I don’t know why yet”

    I have the same problem. Plugin installed. pimple.json built, but still no autocomplition..

    Can you please tell how did you solve this issue?

    1. Make sure pimple.json is in the root directory of the project.

      Make sure you /** @return \Silex\Application */ in PHPDoc comments where appropriate.

      I fixed it by toggling the Silex plugin and restarting the IDE a few times.

      1. Thanks a lot. There was an issue with telling PHPStorm what the root directory was. Problem solved

Leave a Reply to Stas Cancel reply

Your email address will not be published. Required fields are marked *