If you are working with legacy app which still uses $container->get()
or even $em->getRepotirory()
methods, phan will be unable to resolve types for you.
Phan provides you ability to override types for methods during analisys depending on context and arguments provided. But for this cases we also need somehow to gather type information from both container and doctrine mappings. So here is example of how this could be acheaved.
I use it as temporal solution until code cleanup process is incomplete for my current project, so I didn't prepare any ready-to-go library. If you want to do that - cool. But I don't have enough time and just wanted to share at least something with community.
First of all we need a way to gather information about types. I run cache:warmup
before running the analisys to generate xml dump of the container. It will be used to get mapping of types for service id's.
When you have source of this type map, you could prepare initialization script for this plugin. Something like that:
# .phan/plugins/ContainerReturnTypePlugin.php
<?php
return ContainerReturnTypePlugin::createFromXMLDump(
__DIR__ . '/../../var/cache/dev/appDevDebugProjectContainer.xml'
);
For doctrine I wrote small command (ExposeDoctrineRepositoriesCommand
) which allows me to dump map entity => repository, which will then be used to provide type information. I create this dump by simply running console app:doctrine:expose-mapping > .phan/doctrine.json
before starting phan. You could even start this in initialization script via exec, but this depends on how you want it to work. Anyway, here is example of my initialization script:
<?php
# .phan/plugins/DoctrineTypeResolutionPlugin.php
$doctrineMapping = json_decode(file_get_contents(__DIR__ . '/doctrine.json'), true);
return new DoctrineTypeResolutionPlugin($doctrineMapping);
Then we need to enable this plugins in phan's configuration file (which is located by default at .phan/config.php
):
// ...
'plugins' => [
'.phan/plugins/ContainerReturnTypePlugin.php',
'.phan/plugins/DoctrineTypeResolutionPlugin.php',
],
Probably it's possible to encounter types for generic methods like findBy
and find
of entity repository, but since I don't have much of them in my current code base, I didn't had a chanse to explore this abilities. So this plugin only resolves type of repository.
Since DoctrineBundle 1.8.0 you can also use the autowiring capabilitity of Symfony in order to have the correct type available. See doctrine/DoctrineBundle#727.