Registry Pattern Hinting with PHPStorm

You probably use the registry pattern. Having a common place to store components you’ll need throughout your application is a must. The most frustrating part about this pattern is your IDE probably doesn’t know what you are putting in or getting back out.

Our most common case is the Doctrine EntityManager, but this easily applies to Redis clients, Monolog Logger and tools like Bugsnag. In our code, we would frequently declare the returned value in a comment above the registry get.

/** @var EntityManager $em */
$em = Registry->get('em');

That’s pretty ugly to put all over the codebase, but definitely help the failings of the IDE and pattern. Well, fortunately for us, JetBrains added a way to indicate what our registry returns for arbitrary strings like “em”.

To make use of this, add a file to the root of your project called .phpstorm.meta.php. Within that file, following our EntityManager example:

<?php
namespace PHPSTORM_META
{
    override( \PrivCo\Components\Registry::get(0),
        map([
            "em" => Doctrine\ORM\EntityManager::class,
        ])
    );
}

You can use this format for any number of values that come out of your registry and you’ll always get proper code completions in your IDE (assuming you use PHPStorm).