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 even donate each month some money via Patreon.
I faced an issue few months ago, and I hit the very same one today while I started a big refactoring of an application. And I totally forgot about it 😂 Memory sucks :)
So let’s talk about it, and see how to solve it. And now I hope I will remember to fix it on every project I’m working on.
Consider this piece of code:
class Model
{
}
class ServiceA
{
private $serviceB;
public function __construct(ServiceB $serviceB)
{
$this->serviceB = $serviceB;
}
public function init()
{
// This line will generate a FatalError (DateTime != Model)
$this->serviceB->process(new DateTime());
}
}
class ServiceB
{
public function process(Model $model)
{
}
}
I’m expecting to get this error:
Parameter #1 $model of method ServiceB::process() expects Model, DateTime given.
But why is it valid? Because PHPStan does not infer type from constructor argument. And this is totally valid to not do so.
A solution would be to add some PHPDoc:
class ServiceA
{
/** @var ServiceB */
private $serviceB;
}
In the Symfony community, we have some standards. Additionally, I hate useless PHPDoc. I don’t want to pollute my code by adding them everywhere.
Symfony’s coding standards state to not add the PHPDoc to a property if it can be inferred from the constructor, it is also the default configuration of PHP CS Fixer and it is considered a best practice by some.
Since PHPStan has a nice plugin system, I wrote a plugin to automatically add theses type-hint in PHPStan engine few months ago. I used it in my current application and I was able to find 45 new violations \o/
You can read the code and add it to your project if you want to.
Commentaires et discussions
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…
Lire la suite de l’article Using PHPStan to analyse Symfony Console Application
Nos articles sur le même sujet
Ces clients ont profité de notre expertise
L’équipe de Finarta a fait appel à JoliCode pour le développement de leur plateforme Web. Basée sur le framework Symfony 2, l’application est un réseau privé de galerie et se veut être une place de communication et de vente d’oeuvres d’art entre ses membres. Pour cela, de nombreuses règles de droits ont été mises en places et une administration poussée…
Dans le cadre du renouveau de sa stratégie digitale, Orpi France a fait appel à JoliCode afin de diriger la refonte du site Web orpi.com et l’intégration de nombreux nouveaux services. Pour effectuer cette migration, nous nous sommes appuyés sur une architecture de type « micro-services » à l’aide de PHP, Symfony, RabbitMQ, Elasticsearch et…
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…