Code Igniter Code Sniffer


Published on

This is a small slide set for how to use CodeSniffer

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Code Igniter Code Sniffer

  1. 1. CodeIgniter<br />Code Sniffer<br />
  2. 2. Topic<br />Code Sniffer<br />The ins and out of making sure your code is up to a standard of your choosing.<br />
  3. 3.
  4. 4. INTRODUCTION<br />In our days we have come to code to our own way. Many of us have preferences of tabs to spaces, how we name public and private variables. Whether to use CamelCases or Underscores. I myself have been a more code as I code and go back to clean up later, be it with documentation, notes, and how my code is presented.<br />Many like myself want to make sure that All of our codebase is the same format, but we don't want to manually run through them. That could be so tedious and repetitive that one can take a whole other development cycle to just review the code they have written.<br />Luckily there is a pear package we can use to alleviate the stress, this package is called PhpCodeSniffer.<br />
  5. 5. INSTALLING PHP CODE SNIFFER <br />To Install PhpCodeSniffer, follow these steps:<br />Go to your command line and enter the following:<br />pear install PHP_CodeSniffer<br />Then enter<br />Phpcs –ito see the installed Coding Standards<br />
  6. 6. ABOUT CODE SNIFFER<br />Code Sniffer is used by many Open Source and Private development teams. Code Sniffer can sniff any file or directory you wish. Once it has finished Sniffing you will find a detailed report on all the files you have tested. Also CodeSniffer comes with the prepackaged coding standards (PEAR, Squiz, PHPCS and Zend ), I personally follow the Zend Coding Standards, but you can also customize and create your own coding standard taking bits from all over.<br />
  7. 7. CODE SNIFFER RESPONSE<br />Bellow is a typical report for a CodeSniff.<br />FILE: /home/lorna/phpcs/recipe.class.php<br />--------------------------------------------------------------------------------<br />FOUND 3 ERROR(S) AND 0 WARNING(S) AFFECTING 3 LINE(S)<br />--------------------------------------------------------------------------------<br />10 | ERROR | Variable "prep_time" is not in valid camel caps format<br />13 | ERROR | Spaces must be used to indent lines; tabs are not allowed<br />17 | ERROR | A closing tag is not permitted at the end of a PHP file<br />--------------------------------------------------------------------------------<br />
  8. 8. HOW TO USE CODE SNIFFER<br />Code Sniffer is run via command line, so if needed please shell into your server. At times it is best to know the path to the directory/project you want to sniff. Personally I create a reports folder within the project so All my files related to that project are together. I also want the full report to be printed / displayed.<br />Here is the line I use to generate the Reports from within the project reports folder:<br />phpcs /full/path/to/project --standard=Zend --report=full -n >> projectName.sniffer.txt<br />now we target just specific files as follows:<br />phpcs /full/path/to/project/ext/fileW.extension --standard=Zend --report=full -n >> projectName.sniffer.txt<br />now I use >> to pipe the results into a file but you can remove the the piping to render the results within the terminal/console. <br />
  9. 9. I GOT MY REPORT NOW WHAT<br />Now that you have the report in which ever format you decided as a file or on screen, just follow it's instructions, clear out all the errors and run the command again until there is nothing to report or you are satisfied with the current errors being defined.<br />
  10. 10. Something to Note<br />Code Sniffer does not catch all the errors on first pass especially if there are errors, on many cases once some errors clear up another set of errors become apparent and noticeable. It's good use to make sure you run code sniffer a couple of times to validate that your sniffer code is properly sniffed.<br />
  11. 11. THANKS<br />Thanks go to Adil and Rokkan for providing us with the Conference space.<br />
  12. 12. Next Months Topic<br />Vote at:<br />PhpUnit and Code Igniter with PHP 5.3<br />Code Sniffer Creating Custom Standards<br />Anything More important than testing and keeping that code clean.<br />
  13. 13. General Info<br />My Email:<br />AIM: albertrosa2000<br />Meetup:<br />Rokkan:<br />