Your SlideShare is downloading. ×
Back to basics - PHP_Codesniffer
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Back to basics - PHP_Codesniffer

1,626
views

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, …

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,626
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
27
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Back to basics PHP_Codesniffer Sebastian Marek
  • 2. Control your code
    • Keep it clean & tidy
    • 3. Avoid duplications
    • Minimize bugs numbers
    • 4. Minimize complexity
    • 5. Increase performance
  • 6. Right tools for the job
    • Keep it clean & tidy (PHP_Codesniffer)
    • 7. Avoid duplications (phpcpd)
    • Minimize bugs numbers (PHPUnit)
    • 8. Minimize complexity (pdepend)
    • 9. Increase performance (phpmd)
  • 10. PHP_Codesniffer Static code analysis
    • parse the code without executing it
    • 11. syntax checks
    • 12. coding standards
  • 13. Tokenizer http://uk2.php.net/manual/en/book.tokenizer.php
  • 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. 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. 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. 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. 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. 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. 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=&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. 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. Understanding the sniffs CodeSniffer - Standards - Zend - Sniffs - NamingConventions - ValidVariableNameSniff.php - Files - ClosingTagSniff.php - Debug - CodeAnalyzerSniff.php
  • 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. 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. 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. 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. PHPUnderControl integration
  • 38. PHP CodeBrowser integration
  • 39. Hudson integration
  • 40. Sonar integration
  • 41. Resources
    • Squiz Labs blog - http://www.squizlabs.com/php-codesniffer
    • 42. PHP_CodeSniffer homepage - http://pear.php.net/package/PHP_CodeSniffer
    • 43. Closure Linter - http://code.google.com/closure/utilities/docs/linter_howto.html
    • 44. PHP CodeBrowser - https://github.com/mayflowergmbh/PHP_CodeBrowser
    • 45. PHPUnderControl - http://phpundercontrol.org/
    • 46. Hudson - http://hudson-ci.org/
    • 47. Sonar - http://docs.codehaus.org/display/SONAR/PHP+Plugin
  • 48. Q&A ?

×