Is there a linter for PHP that makes all exceptions explicit, like Java?

Is there a nap / static analyzer for PHP that will warn if exceptions are not documented or caught? Consider an example:

// ERROR: InvalidArgumentException must be documented or caught inside method. function divide($a, $b) { if (0 == $b) { throw new InvalidArgumentException(); } return $a / $b; } 

To fix it:

 /** * @throws InvalidArgumentException if $b is zero. */ function divide($a, $b) 

Since it needs to be documented, it is similar to the explicit explicit Java throws in the prototype method. Then it should be possible:

 // ERROR: InvalidArgumentException must be documented or caught inside method. function calc() { print divide(6, 2); } 

There are some obvious caveats with PHP, but in most cases these defects should be detected earlier. Are there any linters that do this?

+5
source share
1 answer

You can use PHPCS

You will need to add your own rules for PHPDOC, here Sniff

I think you add it to your rules with:

 <?xml version="1.0"?> <ruleset name="My rules"> <rule ref="Squiz.Commenting.FunctionCommentThrowTag" /> </ruleset> 

But I have not tested this. Confirmed work ... now I have phpdoc to add.: /

My phpcs.xml:

 <?xml version="1.0"?> <ruleset name="PSR1/2"> <description>Example</description> <file>./api</file> <exclude-pattern>*/Database/Proxies/*</exclude-pattern> <rule ref="PSR1" /> <rule ref="PSR2" /> <rule ref="Squiz.Commenting.FunctionCommentThrowTag" /> </ruleset> 
  $ bin / phpcs

 FILE: ... ttpdocs / api / Api / Version1 / Software / AbstractSoftwareController.php
 -------------------------------------------------- --------------------
 FOUND 1 ERROR AFFECTING 1 LINE
 -------------------------------------------------- --------------------
  60 |  ERROR |  Missing @throws tag for "\ DomainException" exception
 -------------------------------------------------- --------------------

 Time: 5.55 secs;  Memory: 19.5Mb
+1
source

All Articles