2 Files that Your Symfony Application Misses

Following files are supported by PHPStorm and Symfony plugin for years (since 2016) and they make working with a code so elegant. Yet, I came across them just recently.

They immediately became must-have of each repository with Symfony code.

Do You Know the Frustration of Clicking to Existing *.twig File?

Add directory with all your twig files to ide-twig.json in the project root:

You'll appreciate this feature in a project with multiple packages. It's real life-saver:

    "namespaces": [
        { "path": "templates" },
        { "path": "packages/Provision/templates" },
        { "path": "packages/Registration/templates" },
        { "path": "packages/Training/templates" },
        { "path": "packages/KnowHow/templates" },
        { "path": "packages/Marketing/templates" },
        { "path": "packages/User/templates" }

Note: You need to install Symfony Plugin first. Then enable it in each project (yes, they're 2 different steps).

You can use more magic like namespaces, but they're nothing better than explicit paths.

Why PHPStorm doesn't "get" It?

PHPStorm knows types of object passed by constructor injection:


// ...

public function __construct(Type $type)
    $type->someMethod(); // PHPStorm: object of "Type"

But what if you need to test service and get it from container?


$service = $this->container->get(Type::class);
$service; // PHPStorm: "object" type
$service; // you need: object of "Type"

To solve this, you need to spam your code with annotations:

 $service = $this->container->get(Type::class);
+/** @var Type $service */

Is there a better way?

The .phpstorm.meta.php configuration seems a bit magic at first, but you'll understand it:


namespace PHPSTORM_META;

// $container->get(Type::class) → instance of "Type"
override(\Psr\Container\ContainerInterface::get(0), type(0));

And your container calls are now type-aware:


$service = $this->container->get(Type::class);
$service; // PHPStorm: object of "Type"

Pretty cool, right?

You can use this for much more, like Doctrine repository autocomplete by entity class.

I'm using this for container and thanks to that there is 49 fewer annotations in Symplify code.

Do you use another metafile for PHPStorm? Let me know in the comments.

Happy coding!

Do you learn from my contents or use open-souce packages like Rector every day?
Consider supporting it on GitHub Sponsors. I'd really appreciate it!