⌛ This article is now 2 years and 7 months old, a quite long time during which techniques and tools might have evolved. Please contact us to get a fresh insight of our expertise!
Using PHPStan to analyse Symfony Console Application
If you want to use the PHPStan Symfony analysis and encounter an error with PHPParser, you should create a new Symfony environment with inlining deactivated. Here is why!
PHPStan is a very handy tool that will analyze your code and tell you what leftover errors there might be.
It also provides us with the possibility of analyzing our own project console application, as explained in the documentation.
However, after trying to use it in one of our projects, we encountered an unexpected error coming from the vendors after running php vendor/bin/phpstan analyse
:
Compile Error: Cannot Declare interface PhpParser\NodeVisitor, because the name is already in use.
This seemed pretty weird and we didn’t know where it was coming from. Still, we were sure it was due to the console analysis because the regular PHPStan analysis was working fine. So, we decided to open an issue on PHPStan which led to an issue on Symfony.
That’s where we learnt that the issue was due to Symfony’s inlining trying to load an external library that was already loaded by PHPStan. So, in order to make it work, we need to disable this inlining.
However, you don’t want to turn off inlining for your application because it greatly improves performance. Or, at least, you don’t want to turn it off for the regular environments of your application. But nothing prevents you from creating a special environment with the inlining disabled that will be used by PHPStan only.
So, head to your config directory and create a new environment for PHPStan:
Add a file that will disable inlining. You may give it any name:
parameters:
container.dumper.inline_class_loader: false
And call it from the file that you use to load the console application (Your console_application_loader
in the official documentation):
require __DIR__.'../../../config/bootstrap.php';
$kernel = new \App\Kernel('phpstan', true);
return new \Symfony\Bundle\FrameworkBundle\Console\Application($kernel);
And you’re done, everything now works just fine! If we run php vendor/bin/phpstan analyse
again:
We also opened a pull request on PHPStan to update the documentation if you want to check it out!
Commentaires et discussions
Do you want more PHPStan violations?
Edit 2019–07–08: Good news! PHPStan 0.11.10 includes support for inferring private property type from constructor! https://github.com/phpstan/phpstan/releases/tag/0.11.10: Turn on with inferPrivatePropertyTypeFromConstructor: true We use PHPStan a lot and we love it. Some of us…
Lire la suite de l’article Do you want more PHPStan violations?
Nos articles sur le même sujet
Nos formations sur ce sujet
Notre expertise est aussi disponible sous forme de formations professionnelles !

Symfony
Formez-vous à Symfony, l’un des frameworks Web PHP les complet au monde
Ces clients ont profité de notre expertise

Dans le cadre d’une refonte complète de son architecture Web, le journal en ligne Mediapart a sollicité l’expertise de JoliCode afin d’accompagner ses équipes. Mediapart.fr est un des rares journaux 100% en ligne qui n’appartient qu’à ses lecteurs qui amène un fort traffic authentifiés et donc difficilement cachable. Pour effectuer cette migration, …

JoliCode accompagne l’équipe technique Dayuse dans l’optimisation des performances de sa plateforme. Nous sommes intervenus sur différents sujets : La fonctionnalité de recherche d’hôtels, en remplaçant MongoDB et Algolia par Redis et Elasticsearch. La mise en place d’un workflow de réservation, la migration d’un site en Twig vers une SPA à base de…

Afin de poursuivre son déploiement sur le Web, Arte a souhaité être accompagné dans le développement de son API REST “OPA” (API destinée à exposer les programmes et le catalogue vidéo de la chaine). En collaboration avec l’équipe technique Arte, JoliCode a mené un travail spécifique à l’amélioration des performances et de la fiabilité de l’API. Ces…