Back to basics PHP_Codesniffer Sebastian Marek
Control your code <ul><li>Keep it clean & tidy
Avoid duplications </li></ul><ul><li>Minimize bugs numbers
Minimize complexity
Increase performance </li></ul>
Right tools for the job <ul><li>Keep it clean & tidy (PHP_Codesniffer)
Avoid duplications (phpcpd) </li></ul><ul><li>Minimize bugs numbers (PHPUnit)
Minimize complexity (pdepend)
Increase performance (phpmd) </li></ul>
PHP_Codesniffer Static code analysis <ul><li>parse the code without executing it
syntax checks
coding standards </li></ul>
Tokenizer http://uk2.php.net/manual/en/book.tokenizer.php
Tokenize the code T_OPEN_TAG <?php class  Zend_Config_Ini  extends  Zend_Config { protected function  _parseIniFile($filen...
Tokenize the code T_CLASS  T_WHITESPACE  T_STRING  T_WHITESPACE T_EXTENDS T_WHITESPACE  T_STRING  T_WHITESPACE <?php class...
Tokenize the code {  T_WHITESPACE <?php class  Zend_Config_Ini  extends  Zend_Config { protected function  _parseIniFile($...
Tokenize the code T_PROTECTED T_WHITESPACE T_FUNCTION T_WHITESPACE T_STRING ( T_VARIABLE ) T_WHITESPACE <?php class  Zend_...
Tokenize the code {  T_WHITESPACE <?php class  Zend_Config_Ini  extends  Zend_Config { protected function  _parseIniFile($...
Tokenize the code T_IF T_WHITESPACE ( T_VARIABLE T_OBJECT_OPERATOR T_STRING T_WHITESPACE T_IS_NOT_IDENTICAL T_WHITESPACE T...
Tokenize the code T_REQUIRE_ONCE T_WHITESPACE T_CONSTANT_ENCAPSED_STRING ; T_WHITESPACE <?php class  Zend_Config_Ini  exte...
Tokenize the code T_THROW T_WHITESPACE T_NEW T_WHITESPACE T_STRING ( T_WHITESPACE <?php class  Zend_Config_Ini  extends  Z...
Tokenize the code T_VARIABLE T_OBJECT_OPERATOR T_STRING T_WHITESPACE <?php class  Zend_Config_Ini  extends  Zend_Config { ...
Tokenize the code ) ; <?php class  Zend_Config_Ini  extends  Zend_Config { protected function  _parseIniFile($filename) { ...
Tokenize the code T_WHITESPACE } <?php class  Zend_Config_Ini  extends  Zend_Config { protected function  _parseIniFile($f...
Tokenize the code T_RETURN T_WHITESPACE T_VARIABLE ; <?php class  Zend_Config_Ini  extends  Zend_Config { protected functi...
Tokenize the code T_WHITESPACE } <?php class  Zend_Config_Ini  extends  Zend_Config { protected function  _parseIniFile($f...
Tokenize the code T_WHITESPACE } <?php class  Zend_Config_Ini  extends  Zend_Config { protected function  _parseIniFile($f...
Installation #> pear install PHP_CodeSniffer #> phpcs --version PHP_CodeSniffer version 1.3.0RC1 (beta) by Squiz Pty Ltd. ...
Quick user guide #> phpcs --standard=Zend ~/apps/ZendFramework-1.11.2-minimal/library/Zend/Auth.php   FILE: /Users/smarek/...
Quick user guide #> phpcs --standard=Zend --report=checkstyle   ~/apps/ZendFramework-1.11.2-minimal/library/Zend/Auth.php ...
Quick user guide #> phpcs --standard=Zend --report=svnblame library/Zend/Auth.php   PHP CODE SNIFFER SVN BLAME SUMMARY ---...
Understanding the sniffs CodeSniffer - Standards - Zend - Sniffs - NamingConventions - ValidVariableNameSniff.php - Files ...
Including existing sniffs CodeSniffer/Standards/Zend/Sniffs/ ruleset.xml <? xml   version = &quot;1.0&quot; ?> < ruleset  ...
Overriding sniffs options CodeSniffer/Standards/Zend/Sniffs/ ruleset.xml <? xml   version = &quot;1.0&quot; ?> < ruleset  ...
Upcoming SlideShare
Loading in...5
×

Back to basics - PHP_Codesniffer

1,689

Published on

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!

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,689
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
28
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Back to basics - PHP_Codesniffer"

  1. 1. Back to basics PHP_Codesniffer Sebastian Marek
  2. 2. Control your code <ul><li>Keep it clean & tidy
  3. 3. Avoid duplications </li></ul><ul><li>Minimize bugs numbers
  4. 4. Minimize complexity
  5. 5. Increase performance </li></ul>
  6. 6. Right tools for the job <ul><li>Keep it clean & tidy (PHP_Codesniffer)
  7. 7. Avoid duplications (phpcpd) </li></ul><ul><li>Minimize bugs numbers (PHPUnit)
  8. 8. Minimize complexity (pdepend)
  9. 9. Increase performance (phpmd) </li></ul>
  10. 10. PHP_Codesniffer Static code analysis <ul><li>parse the code without executing it
  11. 11. syntax checks
  12. 12. coding standards </li></ul>
  13. 13. Tokenizer http://uk2.php.net/manual/en/book.tokenizer.php
  14. 14. Tokenize the code T_OPEN_TAG <?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; } }
  15. 15. Tokenize the code T_CLASS T_WHITESPACE T_STRING T_WHITESPACE T_EXTENDS 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; } }
  16. 16. 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; } }
  17. 17. Tokenize the code T_PROTECTED T_WHITESPACE T_FUNCTION T_WHITESPACE T_STRING ( T_VARIABLE ) 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; } }
  18. 18. 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; } }
  19. 19. Tokenize the code T_IF T_WHITESPACE ( T_VARIABLE T_OBJECT_OPERATOR T_STRING T_WHITESPACE T_IS_NOT_IDENTICAL T_WHITESPACE T_STRING ) T_WHITESPACE { 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; } }
  20. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 30. Quick user guide #> phpcs --standard=Zend --report=checkstyle ~/apps/ZendFramework-1.11.2-minimal/library/Zend/Auth.php <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <checkstyle version=&quot;1.3.0RC1&quot;> <file name=&quot;/Users/smarek/apps/ZendFramework-1.11.2-minimal/library/Zend/Auth.php&quot;> <error line=&quot;17&quot; column=&quot;1&quot; severity=&quot;warning&quot; message=&quot;Line exceeds 80 characters; contains 87 characters&quot; source=&quot;Generic.Files.LineLength.TooLong&quot;/> <error line=&quot;26&quot; column=&quot;1&quot; severity=&quot;warning&quot; message=&quot;Line exceeds 80 characters; contains 87 characters&quot; source=&quot;Generic.Files.LineLength.TooLong&quot;/> <error line=&quot;51&quot; column=&quot;6&quot; severity=&quot;error&quot; message=&quot;Closing brace must be on a line by itself&quot; source=&quot;PEAR.WhiteSpace.ScopeClosingBrace.Line&quot;/> <error line=&quot;59&quot; column=&quot;6&quot; severity=&quot;error&quot; message=&quot;Closing brace must be on a line by itself&quot; source=&quot;PEAR.WhiteSpace.ScopeClosingBrace.Line&quot;/> <error line=&quot;80&quot; column=&quot;1&quot; severity=&quot;warning&quot; message=&quot;Line exceeds 80 characters; contains 90 characters&quot; source=&quot;Generic.Files.LineLength.TooLong&quot;/> <error line=&quot;120&quot; column=&quot;1&quot; severity=&quot;warning&quot; message=&quot;Line exceeds 80 characters; contains 87 characters&quot; source=&quot;Generic.Files.LineLength.TooLong&quot;/> </file> </checkstyle> Changing reporting format to checkstyle report
  31. 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
  32. 32. Understanding the sniffs CodeSniffer - Standards - Zend - Sniffs - NamingConventions - ValidVariableNameSniff.php - Files - ClosingTagSniff.php - Debug - CodeAnalyzerSniff.php
  33. 33. Including existing sniffs CodeSniffer/Standards/Zend/Sniffs/ ruleset.xml <? xml version = &quot;1.0&quot; ?> < ruleset name = &quot;Zend&quot; > < 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 = &quot;Generic.Functions.FunctionCallArgumentSpacing&quot; /> < rule ref = &quot;Generic.Functions.OpeningFunctionBraceBsdAllman&quot; /> < rule ref = &quot;Generic.PHP.DisallowShortOpenTag&quot; /> < rule ref = &quot;Generic.WhiteSpace.DisallowTabIndent&quot; /> < rule ref = &quot;PEAR.Classes.ClassDeclaration&quot; /> < rule ref = &quot;PEAR.ControlStructures.ControlSignature&quot; /> < rule ref = &quot;PEAR.Functions.FunctionCallSignature&quot; /> < rule ref = &quot;PEAR.Functions.ValidDefaultValue&quot; /> < rule ref = &quot;PEAR.WhiteSpace.ScopeClosingBrace&quot; /> < rule ref = &quot;Squiz.Functions.GlobalFunction&quot; /> </ ruleset >
  34. 34. Overriding sniffs options CodeSniffer/Standards/Zend/Sniffs/ ruleset.xml <? xml version = &quot;1.0&quot; ?> < ruleset name = &quot;Zend&quot; > < 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 = &quot;Generic.Files.LineLength&quot; > < properties > < property name = &quot;lineLimit&quot; value = &quot;80&quot; /> < property name = &quot;absoluteLineLimit&quot; value = &quot;120&quot; /> </ properties > </ rule > <!-- Use Unix newlines --> < rule ref = &quot;Generic.Files.LineEndings&quot; > < properties > < property name = &quot;eolChar&quot; value = &quot;n&quot; /> </ properties > </ rule > </ ruleset >
  35. 35. Customizing sniffs CodeSniffer/Standards/Zend/Sniffs/ ruleset.xml <? xml version = &quot;1.0&quot; ?> < ruleset name = &quot;Zend&quot; > < description > A coding standard based on an early Zend Framework coding standard. Note that this standard is out of date. </ description > < rule ref = &quot;Generic.Commenting.Todo.CommentFound&quot; > < message > Ten komentarz wymaga dalszych zmian: %s </ message > < severity > 3 </ severity > </ rule > </ ruleset >
  36. 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
  37. 37. PHPUnderControl integration
  38. 38. PHP CodeBrowser integration
  39. 39. Hudson integration
  40. 40. Sonar integration
  41. 41. Resources <ul><li>Squiz Labs blog - http://www.squizlabs.com/php-codesniffer
  42. 42. PHP_CodeSniffer homepage - http://pear.php.net/package/PHP_CodeSniffer
  43. 43. Closure Linter - http://code.google.com/closure/utilities/docs/linter_howto.html
  44. 44. PHP CodeBrowser - https://github.com/mayflowergmbh/PHP_CodeBrowser
  45. 45. PHPUnderControl - http://phpundercontrol.org/
  46. 46. Hudson - http://hudson-ci.org/
  47. 47. Sonar - http://docs.codehaus.org/display/SONAR/PHP+Plugin </li></ul>
  48. 48. Q&A ?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×