当前位置:网站首页>The best language is no match for Human Stupidity: using phpstan to catch PHP errors early through static analysis - made with love

The best language is no match for Human Stupidity: using phpstan to catch PHP errors early through static analysis - made with love

2021-05-04 12:36:37 Jiedao jdon

PHP Is a dynamic language , Although it certainly has its benefits , But it also means that it's not uncommon to see errors in the log calling undefined methods or invalid parameter counts . What's worse is , When these types of errors occur , The application will simply crash , Which leads to a bad user experience and frustrated customers .

The solution to this problem is static analysis . For things like Java and C# Such language , This is nothing new , It's a built-in feature , As long as you compile, the code will execute .

Recently there have been various tools , Allow in PHP Run static analysis on the code . Just a few examples. :Psalm,Phan and PHPStan. The goal of these tools is to reduce the number of errors before writing the first test .

PHPStan Requires that... Be installed in the environment PHP 7.1 To run , But the code it needs to analyze can be for PHP 5.6 Compiling .PHPStan Can pass Composer Install into any project :

composer require --dev phpstan/phpstan

After installation , The binary can be used to analyze the directory and output any errors encountered .

vendor/bin/phpstan analyze src

PHPStan There are different levels of rules , Each level is more stringent than before . By default , It will use levels 0 To check “ obvious ” Error of , For example, passing extra parameters or syntax errors . This level is a good place to start the legacy code base , as time goes on , The level can be raised for more rigorous analysis . One more    The biggest is for static trueness , This will always run at the highest level available .

PHPStan Some errors may be output , These are not real mistakes , It's the result of a magic action added by a third-party package or framework . Can pass Composer  Installation related Expand And in phpstan.neon File to fix these errors .

includes:
    - ../vendor/phpstan/phpstan-doctrine/extension.neon

Please be sure to read Complete documentation , Understand what's possible .

In many of our projects ,PHPStan As CI Part of the pipe is included . Doing so means that when developers write and review code , They can focus more on core functions , Instead of worrying about syntax and typology errors that make it produce .

 

版权声明
本文为[Jiedao jdon]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/05/20210504123223994o.html

随机推荐