There are several tools out there that help to develop and maintain high quality PHP code. They allow you to identify the most fragile and messy parts of your codebase. PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards. Learn how it works, how you can use it and how can you bend it to meet your requirements!
20. Tokenize the code T_REQUIRE_ONCE T_WHITESPACE T_CONSTANT_ENCAPSED_STRING ; T_WHITESPACE <?php class Zend_Config_Ini extends Zend_Config { protected function _parseIniFile($filename) { if ( $this -> _loadFileErrorStr !== null ) { require_once 'Zend/Config/Exception.php' ; throw new Zend_Config_Exception( $this -> _loadFileErrorStr ); } return $iniArray; } }
21. Tokenize the code T_THROW T_WHITESPACE T_NEW T_WHITESPACE T_STRING ( T_WHITESPACE <?php class Zend_Config_Ini extends Zend_Config { protected function _parseIniFile($filename) { if ( $this -> _loadFileErrorStr !== null ) { require_once 'Zend/Config/Exception.php' ; throw new Zend_Config_Exception( $this -> _loadFileErrorStr ); } return $iniArray; } }
22. Tokenize the code T_VARIABLE T_OBJECT_OPERATOR T_STRING T_WHITESPACE <?php class Zend_Config_Ini extends Zend_Config { protected function _parseIniFile($filename) { if ( $this -> _loadFileErrorStr !== null ) { require_once 'Zend/Config/Exception.php' ; throw new Zend_Config_Exception( $this -> _loadFileErrorStr ); } return $iniArray; } }
23. Tokenize the code ) ; <?php class Zend_Config_Ini extends Zend_Config { protected function _parseIniFile($filename) { if ( $this -> _loadFileErrorStr !== null ) { require_once 'Zend/Config/Exception.php' ; throw new Zend_Config_Exception( $this -> _loadFileErrorStr ); } return $iniArray; } }
24. Tokenize the code T_WHITESPACE } <?php class Zend_Config_Ini extends Zend_Config { protected function _parseIniFile($filename) { if ( $this -> _loadFileErrorStr !== null ) { require_once 'Zend/Config/Exception.php' ; throw new Zend_Config_Exception( $this -> _loadFileErrorStr ); } return $iniArray; } }
25. Tokenize the code T_RETURN T_WHITESPACE T_VARIABLE ; <?php class Zend_Config_Ini extends Zend_Config { protected function _parseIniFile($filename) { if ( $this -> _loadFileErrorStr !== null ) { require_once 'Zend/Config/Exception.php' ; throw new Zend_Config_Exception( $this -> _loadFileErrorStr ); } return $iniArray; } }
26. Tokenize the code T_WHITESPACE } <?php class Zend_Config_Ini extends Zend_Config { protected function _parseIniFile($filename) { if ( $this -> _loadFileErrorStr !== null ) { require_once 'Zend/Config/Exception.php' ; throw new Zend_Config_Exception( $this -> _loadFileErrorStr ); } return $iniArray; } }
27. Tokenize the code T_WHITESPACE } <?php class Zend_Config_Ini extends Zend_Config { protected function _parseIniFile($filename) { if ( $this -> _loadFileErrorStr !== null ) { require_once 'Zend/Config/Exception.php' ; throw new Zend_Config_Exception( $this -> _loadFileErrorStr ); } return $iniArray; } }
28. Installation #> pear install PHP_CodeSniffer #> phpcs --version PHP_CodeSniffer version 1.3.0RC1 (beta) by Squiz Pty Ltd. (http://www.squiz.net) #> phpcs -i The installed coding standards are MySource, PEAR, PHPCS, Squiz and Zend
29. Quick user guide #> phpcs --standard=Zend ~/apps/ZendFramework-1.11.2-minimal/library/Zend/Auth.php FILE: /Users/smarek/apps/ZendFramework-1.11.2-minimal/library/Zend/Auth.php -------------------------------------------------------------------------------- FOUND 2 ERROR(S) AFFECTING 6 LINE(S) -------------------------------------------------------------------------------- 17 | WARNING | Line exceeds 80 characters; contains 87 characters 26 | WARNING | Line exceeds 80 characters; contains 87 characters 51 | ERROR | Closing brace must be on a line by itself 59 | ERROR | Closing brace must be on a line by itself 80 | WARNING | Line exceeds 80 characters; contains 90 characters 120 | WARNING | Line exceeds 80 characters; contains 87 characters -------------------------------------------------------------------------------- Time: 0 seconds, Memory: 4.25Mb Selecting a coding standard
30. Quick user guide #> phpcs --standard=Zend --report=checkstyle ~/apps/ZendFramework-1.11.2-minimal/library/Zend/Auth.php <?xml version="1.0" encoding="UTF-8"?> <checkstyle version="1.3.0RC1"> <file name="/Users/smarek/apps/ZendFramework-1.11.2-minimal/library/Zend/Auth.php"> <error line="17" column="1" severity="warning" message="Line exceeds 80 characters; contains 87 characters" source="Generic.Files.LineLength.TooLong"/> <error line="26" column="1" severity="warning" message="Line exceeds 80 characters; contains 87 characters" source="Generic.Files.LineLength.TooLong"/> <error line="51" column="6" severity="error" message="Closing brace must be on a line by itself" source="PEAR.WhiteSpace.ScopeClosingBrace.Line"/> <error line="59" column="6" severity="error" message="Closing brace must be on a line by itself" source="PEAR.WhiteSpace.ScopeClosingBrace.Line"/> <error line="80" column="1" severity="warning" message="Line exceeds 80 characters; contains 90 characters" source="Generic.Files.LineLength.TooLong"/> <error line="120" column="1" severity="warning" message="Line exceeds 80 characters; contains 87 characters" source="Generic.Files.LineLength.TooLong"/> </file> </checkstyle> Changing reporting format to checkstyle report
31. Quick user guide #> phpcs --standard=Zend --report=svnblame library/Zend/Auth.php PHP CODE SNIFFER SVN BLAME SUMMARY -------------------------------------------------------------------------------- AUTHOR (Author %) (Overall %) COUNT -------------------------------------------------------------------------------- darby (1.91) (50) 3 bkarwin (0) (33.33) 2 ralph (16.67) (16.67) 1 -------------------------------------------------------------------------------- A TOTAL OF 6 SNIFF VIOLATION(S) WERE COMMITTED BY 3 AUTHOR(S) -------------------------------------------------------------------------------- Time: 3 seconds, Memory: 4.25Mb Changing reporting format to svnblame report
33. Including existing sniffs CodeSniffer/Standards/Zend/Sniffs/ ruleset.xml <? xml version = "1.0" ?> < ruleset name = "Zend" > < description > A coding standard based on an early Zend Framework coding standard. Note that this standard is out of date. </ description > <!-- Include some sniffs from all around the place --> < rule ref = "Generic.Functions.FunctionCallArgumentSpacing" /> < rule ref = "Generic.Functions.OpeningFunctionBraceBsdAllman" /> < rule ref = "Generic.PHP.DisallowShortOpenTag" /> < rule ref = "Generic.WhiteSpace.DisallowTabIndent" /> < rule ref = "PEAR.Classes.ClassDeclaration" /> < rule ref = "PEAR.ControlStructures.ControlSignature" /> < rule ref = "PEAR.Functions.FunctionCallSignature" /> < rule ref = "PEAR.Functions.ValidDefaultValue" /> < rule ref = "PEAR.WhiteSpace.ScopeClosingBrace" /> < rule ref = "Squiz.Functions.GlobalFunction" /> </ ruleset >
34. Overriding sniffs options CodeSniffer/Standards/Zend/Sniffs/ ruleset.xml <? xml version = "1.0" ?> < ruleset name = "Zend" > < description > A coding standard based on an early Zend Framework coding standard. Note that this standard is out of date. </ description > <!-- Lines can be 80 chars long, show errors at 120 chars --> < rule ref = "Generic.Files.LineLength" > < properties > < property name = "lineLimit" value = "80" /> < property name = "absoluteLineLimit" value = "120" /> </ properties > </ rule > <!-- Use Unix newlines --> < rule ref = "Generic.Files.LineEndings" > < properties > < property name = "eolChar" value = "" /> </ properties > </ rule > </ ruleset >
35. Customizing sniffs CodeSniffer/Standards/Zend/Sniffs/ ruleset.xml <? xml version = "1.0" ?> < ruleset name = "Zend" > < description > A coding standard based on an early Zend Framework coding standard. Note that this standard is out of date. </ description > < rule ref = "Generic.Commenting.Todo.CommentFound" > < message > Ten komentarz wymaga dalszych zmian: %s </ message > < severity > 3 </ severity > </ rule > </ ruleset >
36. Closure Linter support Closure Linter installation #> sudo easy_install http://closure-linter.googlecode.com/files/closure_linter-latest.tar.gz PHP_Codesniffer integration #> phpcs --config-set gjslint_path /usr/local/bin/gjslint #> phpcs --standard=Squiz ~/git/LegacyCodebase/data/mis/doc_root/latest/assets/bookmark.js FILE: /Volumes/git/LegacyCodebase/data/mis/doc_root/latest/assets/bookmark.js -------------------------------------------------------------------------------- FOUND 6 ERROR(S) AFFECTING 5 LINE(S) -------------------------------------------------------------------------------- 1 | ERROR | Missing file doc comment 1 | ERROR | Equals sign not aligned with surrounding assignments; expected 3 | | spaces but found 1 space 4 | ERROR | Opening brace should be on a new line 5 | ERROR | Operator == prohibited; use === instead 7 | ERROR | Operator == prohibited; use === instead 12 | ERROR | Expected 1 blank line before closing function brace; 0 found -------------------------------------------------------------------------------- Time: 0 seconds, Memory: 8.25Mb