Code Igniter Code Sniffer

13
CodeIgniter Code Sniffer

description

This is a small slide set for how to use CodeSniffer

Transcript of Code Igniter Code Sniffer

Page 1: Code Igniter  Code Sniffer

CodeIgniter

Code Sniffer

Page 2: Code Igniter  Code Sniffer

Topic

• Code Sniffer– The ins and out of making sure your code is up to

a standard of your choosing.

Page 3: Code Igniter  Code Sniffer
Page 4: Code Igniter  Code Sniffer

INTRODUCTION• 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.

• 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.

• Luckily there is a pear package we can use to alleviate the stress, this package is called PhpCodeSniffer.

Page 5: Code Igniter  Code Sniffer

INSTALLING PHP CODE SNIFFER

• To Install PhpCodeSniffer, follow these steps:– Go to your command line and enter the following:• pear install PHP_CodeSniffer

– Then enter• Phpcs –i to see the installed Coding Standards

Page 6: Code Igniter  Code Sniffer

ABOUT CODE SNIFFER

• 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.

Page 7: Code Igniter  Code Sniffer

CODE SNIFFER RESPONSE

• Bellow is a typical report for a CodeSniff.– FILE: /home/lorna/phpcs/recipe.class.php– --------------------------------------------------------------------------------– FOUND 3 ERROR(S) AND 0 WARNING(S) AFFECTING 3 LINE(S)– -------------------------------------------------------------------------------- – 10 | ERROR | Variable "prep_time" is not in valid camel caps format – 13 | ERROR | Spaces must be used to indent lines; tabs are not allowed – 17 | ERROR | A closing tag is not permitted at the end of a PHP file– --------------------------------------------------------------------------------

Page 8: Code Igniter  Code Sniffer

HOW TO USE CODE SNIFFER• 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.

• Here is the line I use to generate the Reports from within the project reports folder:– phpcs /full/path/to/project --standard=Zend --report=full -n >> projectName.sniffer.txt

• now we target just specific files as follows:– phpcs /full/path/to/project/ext/fileW.extension --standard=Zend --report=full -n >>

projectName.sniffer.txt

• 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.

Page 9: Code Igniter  Code Sniffer

I GOT MY REPORT NOW WHAT

• 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.

Page 10: Code Igniter  Code Sniffer

Something to Note

• 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.

Page 11: Code Igniter  Code Sniffer

THANKS

• Thanks go to Adil and Rokkan for providing us with the Conference space.

Page 12: Code Igniter  Code Sniffer

Next Months Topic

• Vote at: www.meetup.com/codeigniter/– PhpUnit and Code Igniter with PHP 5.3– Code Sniffer Creating Custom Standards– Anything More important than testing and

keeping that code clean.

Page 13: Code Igniter  Code Sniffer

General Info

• My Email: [email protected]• AIM: albertrosa2000• Meetup: www.meetup.com/codeigniter/• Rokkan: www.rokkan.com