cvs: peardoc /en/package/php php-codesniffer.xml /en/package/php/php-codesniffer advanced-usage.xml coding-standard-tutorial.xml faq.xml intro.xml
usage.xml
| From: | Greg Sherwood | Date: | Fri, 12 Jan 2007 05:18:32 +0000 |
| Subject: | cvs: peardoc /en/package/php php-codesniffer.xml /en/package/php/php-codesniffer advanced-usage.xml coding-standard-tutorial.xml faq.xml intro.xml usage.xml |
||
| Groups: | php.pear.doc | ||
| Request: | Send a blank email to pear-doc+get-9279@lists.php.net to get a copy of this message | ||
squiz Fri Jan 12 05:18:32 2007 UTC
Added files:
/peardoc/en/package/php php-codesniffer.xml
/peardoc/en/package/php/php-codesniffer advanced-usage.xml
coding-standard-tutorial.xml
faq.xml intro.xml usage.xml
Log:
The first bits of the PHP_CodeSniffer docs, ported from the external package homepage.
http://cvs.php.net/viewvc.cgi/peardoc/en/package/php/php-codesniffer.xml?view=markup&rev=1.1 Index: peardoc/en/package/php/php-codesniffer.xml +++ peardoc/en/package/php/php-codesniffer.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- $Revision: 1.1 $ --> <sect1 id="package.php.php-codesniffer"> <title>PHP_CodeSniffer</title> <para> PHP_CodeSniffer is a PHP5 script that tokenises and "sniffs" PHP code to detect violations of a defined set of coding standards. It is an essential development tool that ensures that your code remains clean and consistent. It can even help prevent some common semantic errors made by developers. </para> &package.php.php-codesniffer.intro; &package.php.php-codesniffer.usage; &package.php.php-codesniffer.advanced-usage; &package.php.php-codesniffer.coding-standard-tutorial; &package.php.php-codesniffer.faq; </sect1> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/viewvc.cgi/peardoc/en/package/php/php-codesniffer/advanced-usage.xml?view=markup&rev=1.1 Index: peardoc/en/package/php/php-codesniffer/advanced-usage.xml +++ peardoc/en/package/php/php-codesniffer/advanced-usage.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- $Revision: 1.1 $ --> <refentry id="package.php.php-codesniffer.advanced-usage"> <refnamediv> <refname>Advanced Usage</refname> <refpurpose>Advanced usage information</refpurpose> </refnamediv> <refsect1> <title>Specifying Valid File Extensions</title> <warning><simpara>This functionality is currently not documented</simpara></warning> </refsect1> <refsect1> <title>Printing Verbose Tokeniser Output</title> <warning><simpara>This functionality is currently not documented</simpara></warning> </refsect1> <refsect1> <title>Printing Verbose Sniffing Output</title> <warning><simpara>This functionality is currently not documented</simpara></warning> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/viewvc.cgi/peardoc/en/package/php/php-codesniffer/coding-standard-tutorial.xml?view=markup&rev=1.1 Index: peardoc/en/package/php/php-codesniffer/coding-standard-tutorial.xml +++ peardoc/en/package/php/php-codesniffer/coding-standard-tutorial.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- $Revision: 1.1 $ --> <refentry id="package.php.php-codesniffer.coding-standard-tutorial"> <refnamediv> <refname>Coding Standard Tutorial</refname> <refpurpose>A guide to writing your own code sniffs</refpurpose> </refnamediv> <refsect1> <title>Introduction</title> <para> In this tutorial, we will create a new coding standard with a single sniff. Our sniff will prohibit the use of Perl style hash comments. </para> </refsect1> <refsect1> <title>Creating the Coding Standard Directory</title> <para> All sniffs in PHP_CodeSniffer must belong to a coding standard. A coding standard is just a directory inside PHP_CodeSniffer, so we can create one very easily. Let's call our coding standard <emphasis>MyStandard</emphasis>. Run the following commands to create the coding standard directory structure: <programlisting> <![CDATA[ $ cd /path/to/PHP_CodeSniffer/CodeSniffer/Standards $ mkdir MyStandard $ mkdir MyStandard/Sniffs $ mkdir MyStandard/Tests ]]> </programlisting> </para> <para> The <filename>MyStandard</filename> directory represents our coding standard and is the name we will use on the command line to specify the new coding standard to use for checking. The <filename>Sniffs</filename> sub-directory is used to store all the sniff files for this coding standard. The <filename>Tests</filename> sub-directory is used to store unit tests for the sniffs. </para> </refsect1> <refsect1> <title>Creating the Sniff</title> <para> A sniff requires a single PHP file. It's name should clearly describe the standard that we are enforcing. For our sniff, we will name the PHP file <filename>DisalowHashComments.php</filename>, and place it into a <filename>Commenting</filename> sub-directory, to categorise this sniff as relating to commenting. Run the following commands to create the category and the sniff: <programlisting> <![CDATA[ $ cd /path/to/PHP_CodeSniffer/CodeSniffer/Standards/MyStandard/Sniffs $ mkdir Commenting $ touch Commenting/DisallowHashComments.php ]]> </programlisting> </para> <note> <simpara> It does not matter what sub-directories you use for categorising your sniffs. Just make them descriptive enough so you can find your sniffs again later when you want to modify them. </simpara> </note> <para> Each sniff must implement the <literal>PHP_CodeSniffer_Sniff</literal> interface so that the PHP_CodeSniffer knows that it should instantiate the sniff once it's invoked. The <literal>PHP_CodeSniffer_Sniff</literal> interface defines two methods that must be implemented; <function>register</function> and <function>process</function>. </para> <refsect2> <title>The <function>register</function> and <function>process</function> Methods</title> <para> The <function>register</function> method allows a sniff to subscribe to one or more token types that it may wish to process. Once PHP_CodeSniffer encounters one of those tokens, it calls the <function>process</function> method with the <literal>PHP_CodeSniffer_File</literal> object (a representation of the current file being checked) and the position in the stack where the token was found. </para> <para> For our sniff, we are interested in single line comments. The <function>token_get_all</function> method that PHP_CodeSniffer uses to acquire the tokens within a file distingushes doc comments and normal comments as two separate token types. Therefore, we don't have to worry about doc comments interfering with our test. The <function>register</function> method only needs to return one token type, <constant>T_COMMENT</constant>. </para> </refsect2> <refsect2> <title>The Token Stack</title> <para> A sniff can gather more information about a token by acquiring the token stack with a call to the <function>getTokens</function> method on the <literal>PHP_CodeSniffer_File</literal> object. This method returns an array, and is indexed by the position where the token occurs in the token stack. Each element in the array represents a token. All tokens have a <literal>code</literal>, <literal>type</literal> and a <literal>content</literal> index in their array. The <literal>code</literal> value is a unique integer for the type of token. The <literal>type</literal> value is a string representation of the token (eg. 'T_COMMENT' for comment tokens). The <literal>type</literal> has a corresponding globally defined integer with the same name. Finally, the <literal>content</literal> value contains the content of the token as it appears in the code. </para> <note> <simpara> Some Tokens have more indicies than those described above. Have a look in the <filename>PHP/CodeSniffer/File.php</filename> class comment for a list of tokens and their auxillery indicies. </simpara> </note> </refsect2> <refsect2> <title>Reporting Errors</title> <para> Once an error is detected, a sniff should indicate that an error has occured by calling the <function>addError</function> method on the <literal>PHP_CodeSniffer_File</literal> object, passing in an appropriate error message as the first argument and the position in the stack where the error was detected as the second. Alternatively, if the violation is considered not as critical as an error, the <function>addWarning</function> method can be used. </para> </refsect2> </refsect1> <refsect1> <title>HashComments.php</title> <para> We now have to write the content of our sniff. The content of the <filename>DisallowHashComments.php</filename> file should be the following: <programlisting role="php"> <![CDATA[ <?php /** * PHP_CodeSniffer tokenises PHP code and detects violations of a * defined set of coding standards. * * PHP version 5 * * @category PHP * @package PHP_CodeSniffer * @author Your Name <you@domain.net> * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence * @version CVS: $Id: coding-standard-tutorial.xml,v 1.1 2007/01/12 05:18:32 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ require_once 'PHP/CodeSniffer/Sniff.php'; /** * This sniff prohibits the use of Perl style hash comments. * * An example of a hash comment is: * * <code> * # This is a hash comment, which is prohibited. * $hello = 'hello'; * </code> * * @category PHP * @package PHP_CodeSniffer * @author Your Name <you@domain.net> * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence * @version Release: @package_version@ * @link http://pear.php.net/package/PHP_CodeSniffer */ class MyStandard_Sniffs_Commenting_DisallowHashComments implements PHP_CodeSniffer_Sniff { /** * Returns the token types that this sniff is interested in. * * @return array(int) */ public function register() { return(T_COMMENT); }//end register() /** * Processes the tokens that this sniff is interested in. * * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. * @param int $stackPtr The position in the stack where * the token was found. * * @return void */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); if ($tokens[$stackPtr]['content']{0} === '#') { $error = 'Hash comments are prohibited'; $phpcsFile->addError($error, $stackPtr); } }//end process() }//end class ?> ]]> </programlisting> </para> </refsect1> <refsect1> <title>Results</title> <para> Now that we have defined a coding standard, let's validate a file that contains hash comments. <programlisting> <![CDATA[ $ phpcs --standard=MyStandard Test.php FILE: Test.php -------------------------------------------------------------------------------- FOUND 3 ERROR(S) AND 1 WARNING(S) AFFECTING 3 LINE(S) -------------------------------------------------------------------------------- 3 | ERROR | Hash comments are prohibited 7 | ERROR | Hash comments are prohibited 9 | ERROR | Hash comments are prohibited -------------------------------------------------------------------------------- ]]> </programlisting> </para> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/viewvc.cgi/peardoc/en/package/php/php-codesniffer/faq.xml?view=markup&rev=1.1 Index: peardoc/en/package/php/php-codesniffer/faq.xml +++ peardoc/en/package/php/php-codesniffer/faq.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- $Revision: 1.1 $ --> <refentry id="package.php.php-codesniffer.faq"> <refnamediv> <refname>FAQ</refname> <refpurpose>Frequently asked questions</refpurpose> </refnamediv> <refsect1> <title>Does PHP_CodeSniffer perform any code coverage or unit testing?</title> <para> No. PHP_CodeSniffer is not a tool for testing that your PHP application works correctly. All PHP_CodeSniffer will do is ensure your PHP code meets the standards that you are following. </para> </refsect1> <refsect1> <title>My code is fine! Why do I need PHP_CodeSniffer?</title> <para> Maybe you don't, but if you want to ensure you adhere to a set of coding standards, PHP_CodeSniffer is a quick and easy way to do that. PHP_CodeSniffer is a replacement for the more manual task of checking coding standards in code reviews. With PHP_CodeSniffer, you can reserve code reviews for the checking of code correctness. </para> <note> <simpara> Coding standards are a good thing. They will make your code easier to read and maintain, especially when multiple developers are working on the same application. Consider using coding standards if you don't already. </simpara> </note> </refsect1> <refsect1> <title>Does PHP_CodeSniffer parse my code to ensure it will execute?</title> <para> No. PHP_CodeSniffer does not actually parse your code, and so cannot tell if your code contains parse errors. PHP_CodeSniffer may return an incorrect number of errors if checking code that does contain parse errors. </para> <tip> <para> You can easily check for parse errors in a file using the PHP command line interface and the <literal>-l</literal> (lowercase L) option. </para> <programlisting> <![CDATA[ $ php -l /path/to/code/myfile.inc No syntax errors detected in /path/to/code/myfile.inc ]]> </programlisting> </tip> </refsect1> <refsect1> <title>I don't agree with your coding standards! Can I make PHP_CodeSniffer enforce my own?</title> <para> Yes. At its core, PHP_CodeSniffer is just a framework for enforcing coding standards. We release PHP_CodeSniffer with the a couple of sample coding standards to help developers get started on projects where they do not have a standard. If you want to write your own standards, read the <link linkend="package.php.php-codesniffer.coding-standard-tutorial">tutorial on creating coding standards</link>. </para> </refsect1> <refsect1> <title>How come PHP_CodeSniffer reported errors, I fixed them, now I get even more?</title> <para> Sometimes, errors mask the existence of other errors, or new errors are created as you fix others. For example, PHP_CodeSniffer might tell you that an in-line IF statement needs to be defined with braces. Once you make this change, PHP_CodeSniffer may report that the braces you added are not correctly aligned. </para> <para> Always run PHP_CodeSniffer until you get a passing result. Once you've made the changes PHP_CodeSniffer recommends, run PHP_CodeSniffer again to ensure no new errors have been added. </para> </refsect1> <refsect1> <title>Why doesn't PHP_CodeSniffer just make the changes it suggests for me?</title> <para> As much as we trust PHP_CodeSniffer to check your code for coding standard errors, we don't trust any application to ever change code for us without reviewing it first. Considering we would have to check each change PHP_CodeSniffer made before releasing the source code, why not make the changes manually? </para> <para> Making the changes manually ensures a couple of positive things happen: <itemizedlist> <listitem> <para>Developers learn the coding standards and make less mistakes in the future.</para> </listitem> <listitem> <para>Developers can ensure that PHP_CodeSniffer is working correctly.</para> </listitem> <listitem> <para>Developers can decide if a coding standard doesn't fit a particular piece of code.</para> </listitem> </itemizedlist> </para> <para> So if you find yourself wishing PHP_CodeSniffer would just go ahead and make those changes for you, maybe you just need to read the coding standards and adhere to them a bit better. </para> <note> <simpara> No matter how small of a change you make, always test your code before committing it to your code repository or releasing it. Even changes suggested by PHP_CodeSniffer need to be tested, as small and insignificant as they may seem. </simpara> </note> </refsect1> <refsect1> <title>What does PHP_CodeSniffer use to tokenize my PHP code?</title> <para> PHP_CodeSniffer uses <ulink url="http://www.php.net/tokenizer">PHP's inbuilt tokenizer functions</ulink> to parse your PHP code. </para> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/viewvc.cgi/peardoc/en/package/php/php-codesniffer/intro.xml?view=markup&rev=1.1 Index: peardoc/en/package/php/php-codesniffer/intro.xml +++ peardoc/en/package/php/php-codesniffer/intro.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- $Revision: 1.1 $ --> <refentry id="package.php.php-codesniffer.intro"> <refnamediv> <refname>Introduction</refname> <refpurpose>Description and simple usage example</refpurpose> </refnamediv> <refsect1> <title>Description</title> <para> PHP_CodeSniffer is a PHP5 script that tokenises and "sniffs" PHP code to detect violations of a defined set of coding standards. It is an essential development tool that ensures that your code remains clean and consistent. It can even help prevent some common semantic errors made by developers. </para> <para> A coding standard, in PHP_CodeSniffer, is a collection of <emphasis>sniff</emphasis> files. Each sniff file checks one part of the coding standard only. Multiple coding standards can be used within PHP_CodeSniffer, so that the one installation can be used across multiple projects. The default coding standard used by PHP_CodeSniffer is the <emphasis>PEAR</emphasis> coding standard. </para> </refsect1> <refsect1> <title>Example</title> <para>To check a file against the PEAR coding standard, simply specify the file's location.</para> <example> <title>Checking a file with PHP_CodeSniffer</title> <programlisting> <![CDATA[ $ phpcs /path/to/code/myfile.php FILE: /path/to/code/myfile.php -------------------------------------------------------------------------------- FOUND 5 ERROR(S) AFFECTING 2 LINE(S) -------------------------------------------------------------------------------- 2 | ERROR | Missing file doc comment 20 | ERROR | PHP keywords must be lowercase; expected "false" but found "FALSE" 47 | ERROR | Line not indented correctly; expected 4 spaces but found 1 51 | ERROR | Missing function doc comment 88 | ERROR | Line not indented correctly; expected 9 spaces but found 6 -------------------------------------------------------------------------------- ]]> </programlisting> </example> <para>Or, if you wish to check an entire directory, you can specify the directory location instead of a file.</para> <example> <title>Checking a directory with PHP_CodeSniffer</title> <programlisting> <![CDATA[ $ phpcs /path/to/code FILE: /path/to/code/myfile.php -------------------------------------------------------------------------------- FOUND 5 ERROR(S) AFFECTING 5 LINE(S) -------------------------------------------------------------------------------- 2 | ERROR | Missing file doc comment 20 | ERROR | PHP keywords must be lowercase; expected "false" but found "FALSE" 47 | ERROR | Line not indented correctly; expected 4 spaces but found 1 51 | ERROR | Missing function doc comment 88 | ERROR | Line not indented correctly; expected 9 spaces but found 6 -------------------------------------------------------------------------------- FILE: /path/to/code/yourfile.php -------------------------------------------------------------------------------- FOUND 1 ERROR(S) AND 1 WARNING(S) AFFECTING 1 LINE(S) -------------------------------------------------------------------------------- 21 | ERROR | PHP keywords must be lowercase; expected "false" but found "FALSE" 21 | WARNING | Equals sign not aligned with surrounding assignments -------------------------------------------------------------------------------- ]]> </programlisting> </example> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/viewvc.cgi/peardoc/en/package/php/php-codesniffer/usage.xml?view=markup&rev=1.1 Index: peardoc/en/package/php/php-codesniffer/usage.xml +++ peardoc/en/package/php/php-codesniffer/usage.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- $Revision: 1.1 $ --> <refentry id="package.php.php-codesniffer.usage"> <refnamediv> <refname>Usage</refname> <refpurpose>Standard usage information</refpurpose> </refnamediv> <refsect1> <title>Getting Help from the Command Line</title> <para> Running PHP_CodeSniffer with the <emphasis>-h</emphasis> or <emphasis>--help</emphasis> command line argument will print a list of commands that PHP_CodeSniffer will respond to. The output of <emphasis>phpcs -h</emphasis> is shown below. </para> <programlisting> <![CDATA[ Usage: phpcs [-nvi] [--report=<report>] [--standard=<standard>] [--extensions=<extensions>] <file> ... <file> : one or more files and/or directories to check <standard> : the name of the coding standard to use <report> : print either the "full" or "summary" report <extensions> : a comma separated list of file extensions to check (only valid if checking a directory) -n : do not print warnings -v[v][v] : print verbose output -i : show a list of installed coding standards -h --help : print this help message --version : print version information ]]> </programlisting> <para> The <literal>--standard</literal> command line argument is optional, even if you have more than one coding standard installed. If no coding standard is specified, PHP_CodeSniffer will default to checking against the <emphasis>PEAR</emphasis> coding standard. </para> </refsect1> <refsect1> <title>Checking Files and Folders</title> <para> The simplest way of using PHP_CodeSniffer is to provide the location of a file or folder for PHP_CodeSniffer to check. If a folder is provided, PHP_CodeSniffer will check all files it finds in that folder and all its sub-folders. </para> <para> In the example below, the first command tells PHP_CodeSniffer to check the <filename>myfile.inc</filename> file for coding standard errors while the second command tells PHP_CodeSniffer to check all PHP files in the <filename>my_dir</filename> directory. </para> <example> <title>Checking a single file or folder</title> <programlisting> <![CDATA[ $ phpcs /path/to/code/myfile.inc $ phpcs /path/to/code/my_dir ]]> </programlisting> </example> <para> You can also specify multiple files and folders to check. The command below tells PHP_CodeSniffer to check the <filename>myfile.inc</filename> file and all files in the <filename>my_dir</filename> directory. </para> <example> <title>Checking multiple files and folders</title> <programlisting> <![CDATA[ $ phpcs /path/to/code/myfile.inc /path/to/code/my_dir ]]> </programlisting> </example> <para> After PHP_CodeSniffer has finished processing your files, you will get an error report. The report lists both errors and warnings for all files that violated the coding standard. The output looks like this: </para> <example> <title>Sample PHP_CodeSniffer output</title> <programlisting> <![CDATA[ $ phpcs /path/to/code/myfile.php FILE: /path/to/code/myfile.php -------------------------------------------------------------------------------- FOUND 5 ERROR(S) AND 1 WARNING(S) AFFECTING 5 LINE(S) -------------------------------------------------------------------------------- 2 | ERROR | Missing file doc comment 20 | ERROR | PHP keywords must be lowercase; expected "false" but found "FALSE" 47 | ERROR | Line not indented correctly; expected 4 spaces but found 1 47 | WARNING | Equals sign not aligned with surrounding assignments 51 | ERROR | Missing function doc comment 88 | ERROR | Line not indented correctly; expected 9 spaces but found 6 -------------------------------------------------------------------------------- ]]> </programlisting> </example> <para> If you don't want warnings included in the output, specify the <literal>-n</literal> command line argument. </para> <example> <title>Sample PHP_CodeSniffer output with no warnings</title> <programlisting> <![CDATA[ $ phpcs -n /path/to/code/myfile.php FILE: /path/to/code/myfile.php -------------------------------------------------------------------------------- FOUND 5 ERROR(S) AFFECTING 5 LINE(S) -------------------------------------------------------------------------------- 2 | ERROR | Missing file doc comment 20 | ERROR | PHP keywords must be lowercase; expected "false" but found "FALSE" 47 | ERROR | Line not indented correctly; expected 4 spaces but found 1 51 | ERROR | Missing function doc comment 88 | ERROR | Line not indented correctly; expected 9 spaces but found 6 -------------------------------------------------------------------------------- ]]> </programlisting> </example> </refsect1> <refsect1> <title>Printing a Summary Report</title> <para> By default, PHP_CodeSniffer will print a complete list of all errors and warnings it finds. This list can become quite long, especially when checking a large number of files at once. To print a summary report that only shows the number of errors and warnings for each file, use the <literal>--report=summary</literal> command line argument. The output will look like this: </para> <example> <title>Sample PHP_CodeSniffer summary output</title> <programlisting> <![CDATA[ $ phpcs --report=summary /path/to/code PHP CODE SNIFFER REPORT SUMMARY -------------------------------------------------------------------------------- FILE ERRORS WARNINGS -------------------------------------------------------------------------------- /path/to/code/myfile.inc 5 0 /path/to/code/yourfile.inc 1 1 /path/to/code/ourfile.inc 0 2 -------------------------------------------------------------------------------- A TOTAL OF 6 ERROR(S) AND 3 WARNING(S) WERE FOUND IN 3 FILE(S) -------------------------------------------------------------------------------- ]]> </programlisting> </example> <para> As with the full report, you can suppress the printing of warnings with the <literal>-n</literal> command line argument. </para> <example> <title>Sample PHP_CodeSniffer summary output with no warnings</title> <programlisting> <![CDATA[ $ phpcs -n --report=summary /path/to/code PHP CODE SNIFFER REPORT SUMMARY -------------------------------------------------------------------------------- FILE ERRORS -------------------------------------------------------------------------------- /path/to/code/myfile.inc 5 /path/to/code/yourfile.inc 1 -------------------------------------------------------------------------------- A TOTAL OF 6 ERROR(S) WERE FOUND IN 2 FILE(S) -------------------------------------------------------------------------------- ]]> </programlisting> </example> </refsect1> <refsect1> <title>Printing Verbose Output</title> <para> By default, PHP_CodeSniffer will run quietly, only printing the report of errors and warnings at the end. If you are checking a large number of files, you may have to wait a while to see the report. If you want to know what is happening, you can turn on verbose output. </para> <para> With verbose output enabled, PHP_CodeSniffer will print the file that it is checking, show you how many tokens and lines the file contains, and let you know how long it took to process. The output will look like this: </para> <example> <title>Sample PHP_CodeSniffer verbose output</title> <programlisting> <![CDATA[ $ phpcs /path/to/code/CodeSniffer -v Registering sniffs... DONE Processing AbstractDocElement.php [1093 tokens in 303 lines]... DONE in < 1 second Processing AbstractParser.php [2360 tokens in 558 lines]... DONE in 2 seconds Processing ClassCommentParser.php [923 tokens in 296 lines]... DONE in < 1 second Processing CommentElement.php [988 tokens in 218 lines]... DONE in < 1 second Processing FunctionCommentParser.php [525 tokens in 184 lines]... DONE in 1 second Processing File.php [10968 tokens in 1805 lines]... DONE in 5 seconds Processing Sniff.php [133 tokens in 94 lines]... DONE in < 1 second Processing SniffException.php [47 tokens in 36 lines]... DONE in < 1 second ]]> </programlisting> </example> </refsect1> <refsect1> <title>Specifying a Coding Standard</title> <para> PHP_CodeSniffer can have multiple coding standards installed to allow a single installation to be used with multiple projects. When checking PHP code, PHP_CodeSniffer can be told which coding standard to use. This is done using the <literal>--standard</literal> command line argument. </para> <para> The example below checks the <filename>myfile.inc</filename> file for violations of the <emphasis>PEAR</emphasis> coding standard (installed by default). </para> <example> <title>Specifying a coding standard to use</title> <programlisting> <![CDATA[ $ phpcs /path/to/code/myfile.inc --standard=PEAR ]]> </programlisting> </example> </refsect1> <refsect1> <title>Printing a List of Installed Coding Standards</title> <para> PHP_CodeSniffer can print you a list of the coding standards that are installed so that you can correctly specify a coding standard to use for testing. You can print this list by specifying the <literal>-i</literal> command line argument. </para> <example> <title>Generating a list of installed coding standards</title> <programlisting> <![CDATA[ $ phpcs -i The installed coding standards are PEAR and Squiz. ]]> </programlisting> </example> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->
http://cvs.php.net/viewvc.cgi/peardoc/en/package/php/php-codesniffer.xml?view=markup&rev=1.1 Index: peardoc/en/package/php/php-codesniffer.xml +++ peardoc/en/package/php/php-codesniffer.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- $Revision: 1.1 $ --> <sect1 id="package.php.php-codesniffer"> <title>PHP_CodeSniffer</title> <para> PHP_CodeSniffer is a PHP5 script that tokenises and "sniffs" PHP code to detect violations of a defined set of coding standards. It is an essential development tool that ensures that your code remains clean and consistent. It can even help prevent some common semantic errors made by developers. </para> &package.php.php-codesniffer.intro; &package.php.php-codesniffer.usage; &package.php.php-codesniffer.advanced-usage; &package.php.php-codesniffer.coding-standard-tutorial; &package.php.php-codesniffer.faq; </sect1> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/viewvc.cgi/peardoc/en/package/php/php-codesniffer/advanced-usage.xml?view=markup&rev=1.1 Index: peardoc/en/package/php/php-codesniffer/advanced-usage.xml +++ peardoc/en/package/php/php-codesniffer/advanced-usage.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- $Revision: 1.1 $ --> <refentry id="package.php.php-codesniffer.advanced-usage"> <refnamediv> <refname>Advanced Usage</refname> <refpurpose>Advanced usage information</refpurpose> </refnamediv> <refsect1> <title>Specifying Valid File Extensions</title> <warning><simpara>This functionality is currently not documented</simpara></warning> </refsect1> <refsect1> <title>Printing Verbose Tokeniser Output</title> <warning><simpara>This functionality is currently not documented</simpara></warning> </refsect1> <refsect1> <title>Printing Verbose Sniffing Output</title> <warning><simpara>This functionality is currently not documented</simpara></warning> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/viewvc.cgi/peardoc/en/package/php/php-codesniffer/coding-standard-tutorial.xml?view=markup&rev=1.1 Index: peardoc/en/package/php/php-codesniffer/coding-standard-tutorial.xml +++ peardoc/en/package/php/php-codesniffer/coding-standard-tutorial.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- $Revision: 1.1 $ --> <refentry id="package.php.php-codesniffer.coding-standard-tutorial"> <refnamediv> <refname>Coding Standard Tutorial</refname> <refpurpose>A guide to writing your own code sniffs</refpurpose> </refnamediv> <refsect1> <title>Introduction</title> <para> In this tutorial, we will create a new coding standard with a single sniff. Our sniff will prohibit the use of Perl style hash comments. </para> </refsect1> <refsect1> <title>Creating the Coding Standard Directory</title> <para> All sniffs in PHP_CodeSniffer must belong to a coding standard. A coding standard is just a directory inside PHP_CodeSniffer, so we can create one very easily. Let's call our coding standard <emphasis>MyStandard</emphasis>. Run the following commands to create the coding standard directory structure: <programlisting> <![CDATA[ $ cd /path/to/PHP_CodeSniffer/CodeSniffer/Standards $ mkdir MyStandard $ mkdir MyStandard/Sniffs $ mkdir MyStandard/Tests ]]> </programlisting> </para> <para> The <filename>MyStandard</filename> directory represents our coding standard and is the name we will use on the command line to specify the new coding standard to use for checking. The <filename>Sniffs</filename> sub-directory is used to store all the sniff files for this coding standard. The <filename>Tests</filename> sub-directory is used to store unit tests for the sniffs. </para> </refsect1> <refsect1> <title>Creating the Sniff</title> <para> A sniff requires a single PHP file. It's name should clearly describe the standard that we are enforcing. For our sniff, we will name the PHP file <filename>DisalowHashComments.php</filename>, and place it into a <filename>Commenting</filename> sub-directory, to categorise this sniff as relating to commenting. Run the following commands to create the category and the sniff: <programlisting> <![CDATA[ $ cd /path/to/PHP_CodeSniffer/CodeSniffer/Standards/MyStandard/Sniffs $ mkdir Commenting $ touch Commenting/DisallowHashComments.php ]]> </programlisting> </para> <note> <simpara> It does not matter what sub-directories you use for categorising your sniffs. Just make them descriptive enough so you can find your sniffs again later when you want to modify them. </simpara> </note> <para> Each sniff must implement the <literal>PHP_CodeSniffer_Sniff</literal> interface so that the PHP_CodeSniffer knows that it should instantiate the sniff once it's invoked. The <literal>PHP_CodeSniffer_Sniff</literal> interface defines two methods that must be implemented; <function>register</function> and <function>process</function>. </para> <refsect2> <title>The <function>register</function> and <function>process</function> Methods</title> <para> The <function>register</function> method allows a sniff to subscribe to one or more token types that it may wish to process. Once PHP_CodeSniffer encounters one of those tokens, it calls the <function>process</function> method with the <literal>PHP_CodeSniffer_File</literal> object (a representation of the current file being checked) and the position in the stack where the token was found. </para> <para> For our sniff, we are interested in single line comments. The <function>token_get_all</function> method that PHP_CodeSniffer uses to acquire the tokens within a file distingushes doc comments and normal comments as two separate token types. Therefore, we don't have to worry about doc comments interfering with our test. The <function>register</function> method only needs to return one token type, <constant>T_COMMENT</constant>. </para> </refsect2> <refsect2> <title>The Token Stack</title> <para> A sniff can gather more information about a token by acquiring the token stack with a call to the <function>getTokens</function> method on the <literal>PHP_CodeSniffer_File</literal> object. This method returns an array, and is indexed by the position where the token occurs in the token stack. Each element in the array represents a token. All tokens have a <literal>code</literal>, <literal>type</literal> and a <literal>content</literal> index in their array. The <literal>code</literal> value is a unique integer for the type of token. The <literal>type</literal> value is a string representation of the token (eg. 'T_COMMENT' for comment tokens). The <literal>type</literal> has a corresponding globally defined integer with the same name. Finally, the <literal>content</literal> value contains the content of the token as it appears in the code. </para> <note> <simpara> Some Tokens have more indicies than those described above. Have a look in the <filename>PHP/CodeSniffer/File.php</filename> class comment for a list of tokens and their auxillery indicies. </simpara> </note> </refsect2> <refsect2> <title>Reporting Errors</title> <para> Once an error is detected, a sniff should indicate that an error has occured by calling the <function>addError</function> method on the <literal>PHP_CodeSniffer_File</literal> object, passing in an appropriate error message as the first argument and the position in the stack where the error was detected as the second. Alternatively, if the violation is considered not as critical as an error, the <function>addWarning</function> method can be used. </para> </refsect2> </refsect1> <refsect1> <title>HashComments.php</title> <para> We now have to write the content of our sniff. The content of the <filename>DisallowHashComments.php</filename> file should be the following: <programlisting role="php"> <![CDATA[ <?php /** * PHP_CodeSniffer tokenises PHP code and detects violations of a * defined set of coding standards. * * PHP version 5 * * @category PHP * @package PHP_CodeSniffer * @author Your Name <you@domain.net> * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence * @version CVS: $Id: coding-standard-tutorial.xml,v 1.1 2007/01/12 05:18:32 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ require_once 'PHP/CodeSniffer/Sniff.php'; /** * This sniff prohibits the use of Perl style hash comments. * * An example of a hash comment is: * * <code> * # This is a hash comment, which is prohibited. * $hello = 'hello'; * </code> * * @category PHP * @package PHP_CodeSniffer * @author Your Name <you@domain.net> * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence * @version Release: @package_version@ * @link http://pear.php.net/package/PHP_CodeSniffer */ class MyStandard_Sniffs_Commenting_DisallowHashComments implements PHP_CodeSniffer_Sniff { /** * Returns the token types that this sniff is interested in. * * @return array(int) */ public function register() { return(T_COMMENT); }//end register() /** * Processes the tokens that this sniff is interested in. * * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. * @param int $stackPtr The position in the stack where * the token was found. * * @return void */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); if ($tokens[$stackPtr]['content']{0} === '#') { $error = 'Hash comments are prohibited'; $phpcsFile->addError($error, $stackPtr); } }//end process() }//end class ?> ]]> </programlisting> </para> </refsect1> <refsect1> <title>Results</title> <para> Now that we have defined a coding standard, let's validate a file that contains hash comments. <programlisting> <![CDATA[ $ phpcs --standard=MyStandard Test.php FILE: Test.php -------------------------------------------------------------------------------- FOUND 3 ERROR(S) AND 1 WARNING(S) AFFECTING 3 LINE(S) -------------------------------------------------------------------------------- 3 | ERROR | Hash comments are prohibited 7 | ERROR | Hash comments are prohibited 9 | ERROR | Hash comments are prohibited -------------------------------------------------------------------------------- ]]> </programlisting> </para> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/viewvc.cgi/peardoc/en/package/php/php-codesniffer/faq.xml?view=markup&rev=1.1 Index: peardoc/en/package/php/php-codesniffer/faq.xml +++ peardoc/en/package/php/php-codesniffer/faq.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- $Revision: 1.1 $ --> <refentry id="package.php.php-codesniffer.faq"> <refnamediv> <refname>FAQ</refname> <refpurpose>Frequently asked questions</refpurpose> </refnamediv> <refsect1> <title>Does PHP_CodeSniffer perform any code coverage or unit testing?</title> <para> No. PHP_CodeSniffer is not a tool for testing that your PHP application works correctly. All PHP_CodeSniffer will do is ensure your PHP code meets the standards that you are following. </para> </refsect1> <refsect1> <title>My code is fine! Why do I need PHP_CodeSniffer?</title> <para> Maybe you don't, but if you want to ensure you adhere to a set of coding standards, PHP_CodeSniffer is a quick and easy way to do that. PHP_CodeSniffer is a replacement for the more manual task of checking coding standards in code reviews. With PHP_CodeSniffer, you can reserve code reviews for the checking of code correctness. </para> <note> <simpara> Coding standards are a good thing. They will make your code easier to read and maintain, especially when multiple developers are working on the same application. Consider using coding standards if you don't already. </simpara> </note> </refsect1> <refsect1> <title>Does PHP_CodeSniffer parse my code to ensure it will execute?</title> <para> No. PHP_CodeSniffer does not actually parse your code, and so cannot tell if your code contains parse errors. PHP_CodeSniffer may return an incorrect number of errors if checking code that does contain parse errors. </para> <tip> <para> You can easily check for parse errors in a file using the PHP command line interface and the <literal>-l</literal> (lowercase L) option. </para> <programlisting> <![CDATA[ $ php -l /path/to/code/myfile.inc No syntax errors detected in /path/to/code/myfile.inc ]]> </programlisting> </tip> </refsect1> <refsect1> <title>I don't agree with your coding standards! Can I make PHP_CodeSniffer enforce my own?</title> <para> Yes. At its core, PHP_CodeSniffer is just a framework for enforcing coding standards. We release PHP_CodeSniffer with the a couple of sample coding standards to help developers get started on projects where they do not have a standard. If you want to write your own standards, read the <link linkend="package.php.php-codesniffer.coding-standard-tutorial">tutorial on creating coding standards</link>. </para> </refsect1> <refsect1> <title>How come PHP_CodeSniffer reported errors, I fixed them, now I get even more?</title> <para> Sometimes, errors mask the existence of other errors, or new errors are created as you fix others. For example, PHP_CodeSniffer might tell you that an in-line IF statement needs to be defined with braces. Once you make this change, PHP_CodeSniffer may report that the braces you added are not correctly aligned. </para> <para> Always run PHP_CodeSniffer until you get a passing result. Once you've made the changes PHP_CodeSniffer recommends, run PHP_CodeSniffer again to ensure no new errors have been added. </para> </refsect1> <refsect1> <title>Why doesn't PHP_CodeSniffer just make the changes it suggests for me?</title> <para> As much as we trust PHP_CodeSniffer to check your code for coding standard errors, we don't trust any application to ever change code for us without reviewing it first. Considering we would have to check each change PHP_CodeSniffer made before releasing the source code, why not make the changes manually? </para> <para> Making the changes manually ensures a couple of positive things happen: <itemizedlist> <listitem> <para>Developers learn the coding standards and make less mistakes in the future.</para> </listitem> <listitem> <para>Developers can ensure that PHP_CodeSniffer is working correctly.</para> </listitem> <listitem> <para>Developers can decide if a coding standard doesn't fit a particular piece of code.</para> </listitem> </itemizedlist> </para> <para> So if you find yourself wishing PHP_CodeSniffer would just go ahead and make those changes for you, maybe you just need to read the coding standards and adhere to them a bit better. </para> <note> <simpara> No matter how small of a change you make, always test your code before committing it to your code repository or releasing it. Even changes suggested by PHP_CodeSniffer need to be tested, as small and insignificant as they may seem. </simpara> </note> </refsect1> <refsect1> <title>What does PHP_CodeSniffer use to tokenize my PHP code?</title> <para> PHP_CodeSniffer uses <ulink url="http://www.php.net/tokenizer">PHP's inbuilt tokenizer functions</ulink> to parse your PHP code. </para> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/viewvc.cgi/peardoc/en/package/php/php-codesniffer/intro.xml?view=markup&rev=1.1 Index: peardoc/en/package/php/php-codesniffer/intro.xml +++ peardoc/en/package/php/php-codesniffer/intro.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- $Revision: 1.1 $ --> <refentry id="package.php.php-codesniffer.intro"> <refnamediv> <refname>Introduction</refname> <refpurpose>Description and simple usage example</refpurpose> </refnamediv> <refsect1> <title>Description</title> <para> PHP_CodeSniffer is a PHP5 script that tokenises and "sniffs" PHP code to detect violations of a defined set of coding standards. It is an essential development tool that ensures that your code remains clean and consistent. It can even help prevent some common semantic errors made by developers. </para> <para> A coding standard, in PHP_CodeSniffer, is a collection of <emphasis>sniff</emphasis> files. Each sniff file checks one part of the coding standard only. Multiple coding standards can be used within PHP_CodeSniffer, so that the one installation can be used across multiple projects. The default coding standard used by PHP_CodeSniffer is the <emphasis>PEAR</emphasis> coding standard. </para> </refsect1> <refsect1> <title>Example</title> <para>To check a file against the PEAR coding standard, simply specify the file's location.</para> <example> <title>Checking a file with PHP_CodeSniffer</title> <programlisting> <![CDATA[ $ phpcs /path/to/code/myfile.php FILE: /path/to/code/myfile.php -------------------------------------------------------------------------------- FOUND 5 ERROR(S) AFFECTING 2 LINE(S) -------------------------------------------------------------------------------- 2 | ERROR | Missing file doc comment 20 | ERROR | PHP keywords must be lowercase; expected "false" but found "FALSE" 47 | ERROR | Line not indented correctly; expected 4 spaces but found 1 51 | ERROR | Missing function doc comment 88 | ERROR | Line not indented correctly; expected 9 spaces but found 6 -------------------------------------------------------------------------------- ]]> </programlisting> </example> <para>Or, if you wish to check an entire directory, you can specify the directory location instead of a file.</para> <example> <title>Checking a directory with PHP_CodeSniffer</title> <programlisting> <![CDATA[ $ phpcs /path/to/code FILE: /path/to/code/myfile.php -------------------------------------------------------------------------------- FOUND 5 ERROR(S) AFFECTING 5 LINE(S) -------------------------------------------------------------------------------- 2 | ERROR | Missing file doc comment 20 | ERROR | PHP keywords must be lowercase; expected "false" but found "FALSE" 47 | ERROR | Line not indented correctly; expected 4 spaces but found 1 51 | ERROR | Missing function doc comment 88 | ERROR | Line not indented correctly; expected 9 spaces but found 6 -------------------------------------------------------------------------------- FILE: /path/to/code/yourfile.php -------------------------------------------------------------------------------- FOUND 1 ERROR(S) AND 1 WARNING(S) AFFECTING 1 LINE(S) -------------------------------------------------------------------------------- 21 | ERROR | PHP keywords must be lowercase; expected "false" but found "FALSE" 21 | WARNING | Equals sign not aligned with surrounding assignments -------------------------------------------------------------------------------- ]]> </programlisting> </example> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/viewvc.cgi/peardoc/en/package/php/php-codesniffer/usage.xml?view=markup&rev=1.1 Index: peardoc/en/package/php/php-codesniffer/usage.xml +++ peardoc/en/package/php/php-codesniffer/usage.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- $Revision: 1.1 $ --> <refentry id="package.php.php-codesniffer.usage"> <refnamediv> <refname>Usage</refname> <refpurpose>Standard usage information</refpurpose> </refnamediv> <refsect1> <title>Getting Help from the Command Line</title> <para> Running PHP_CodeSniffer with the <emphasis>-h</emphasis> or <emphasis>--help</emphasis> command line argument will print a list of commands that PHP_CodeSniffer will respond to. The output of <emphasis>phpcs -h</emphasis> is shown below. </para> <programlisting> <![CDATA[ Usage: phpcs [-nvi] [--report=<report>] [--standard=<standard>] [--extensions=<extensions>] <file> ... <file> : one or more files and/or directories to check <standard> : the name of the coding standard to use <report> : print either the "full" or "summary" report <extensions> : a comma separated list of file extensions to check (only valid if checking a directory) -n : do not print warnings -v[v][v] : print verbose output -i : show a list of installed coding standards -h --help : print this help message --version : print version information ]]> </programlisting> <para> The <literal>--standard</literal> command line argument is optional, even if you have more than one coding standard installed. If no coding standard is specified, PHP_CodeSniffer will default to checking against the <emphasis>PEAR</emphasis> coding standard. </para> </refsect1> <refsect1> <title>Checking Files and Folders</title> <para> The simplest way of using PHP_CodeSniffer is to provide the location of a file or folder for PHP_CodeSniffer to check. If a folder is provided, PHP_CodeSniffer will check all files it finds in that folder and all its sub-folders. </para> <para> In the example below, the first command tells PHP_CodeSniffer to check the <filename>myfile.inc</filename> file for coding standard errors while the second command tells PHP_CodeSniffer to check all PHP files in the <filename>my_dir</filename> directory. </para> <example> <title>Checking a single file or folder</title> <programlisting> <![CDATA[ $ phpcs /path/to/code/myfile.inc $ phpcs /path/to/code/my_dir ]]> </programlisting> </example> <para> You can also specify multiple files and folders to check. The command below tells PHP_CodeSniffer to check the <filename>myfile.inc</filename> file and all files in the <filename>my_dir</filename> directory. </para> <example> <title>Checking multiple files and folders</title> <programlisting> <![CDATA[ $ phpcs /path/to/code/myfile.inc /path/to/code/my_dir ]]> </programlisting> </example> <para> After PHP_CodeSniffer has finished processing your files, you will get an error report. The report lists both errors and warnings for all files that violated the coding standard. The output looks like this: </para> <example> <title>Sample PHP_CodeSniffer output</title> <programlisting> <![CDATA[ $ phpcs /path/to/code/myfile.php FILE: /path/to/code/myfile.php -------------------------------------------------------------------------------- FOUND 5 ERROR(S) AND 1 WARNING(S) AFFECTING 5 LINE(S) -------------------------------------------------------------------------------- 2 | ERROR | Missing file doc comment 20 | ERROR | PHP keywords must be lowercase; expected "false" but found "FALSE" 47 | ERROR | Line not indented correctly; expected 4 spaces but found 1 47 | WARNING | Equals sign not aligned with surrounding assignments 51 | ERROR | Missing function doc comment 88 | ERROR | Line not indented correctly; expected 9 spaces but found 6 -------------------------------------------------------------------------------- ]]> </programlisting> </example> <para> If you don't want warnings included in the output, specify the <literal>-n</literal> command line argument. </para> <example> <title>Sample PHP_CodeSniffer output with no warnings</title> <programlisting> <![CDATA[ $ phpcs -n /path/to/code/myfile.php FILE: /path/to/code/myfile.php -------------------------------------------------------------------------------- FOUND 5 ERROR(S) AFFECTING 5 LINE(S) -------------------------------------------------------------------------------- 2 | ERROR | Missing file doc comment 20 | ERROR | PHP keywords must be lowercase; expected "false" but found "FALSE" 47 | ERROR | Line not indented correctly; expected 4 spaces but found 1 51 | ERROR | Missing function doc comment 88 | ERROR | Line not indented correctly; expected 9 spaces but found 6 -------------------------------------------------------------------------------- ]]> </programlisting> </example> </refsect1> <refsect1> <title>Printing a Summary Report</title> <para> By default, PHP_CodeSniffer will print a complete list of all errors and warnings it finds. This list can become quite long, especially when checking a large number of files at once. To print a summary report that only shows the number of errors and warnings for each file, use the <literal>--report=summary</literal> command line argument. The output will look like this: </para> <example> <title>Sample PHP_CodeSniffer summary output</title> <programlisting> <![CDATA[ $ phpcs --report=summary /path/to/code PHP CODE SNIFFER REPORT SUMMARY -------------------------------------------------------------------------------- FILE ERRORS WARNINGS -------------------------------------------------------------------------------- /path/to/code/myfile.inc 5 0 /path/to/code/yourfile.inc 1 1 /path/to/code/ourfile.inc 0 2 -------------------------------------------------------------------------------- A TOTAL OF 6 ERROR(S) AND 3 WARNING(S) WERE FOUND IN 3 FILE(S) -------------------------------------------------------------------------------- ]]> </programlisting> </example> <para> As with the full report, you can suppress the printing of warnings with the <literal>-n</literal> command line argument. </para> <example> <title>Sample PHP_CodeSniffer summary output with no warnings</title> <programlisting> <![CDATA[ $ phpcs -n --report=summary /path/to/code PHP CODE SNIFFER REPORT SUMMARY -------------------------------------------------------------------------------- FILE ERRORS -------------------------------------------------------------------------------- /path/to/code/myfile.inc 5 /path/to/code/yourfile.inc 1 -------------------------------------------------------------------------------- A TOTAL OF 6 ERROR(S) WERE FOUND IN 2 FILE(S) -------------------------------------------------------------------------------- ]]> </programlisting> </example> </refsect1> <refsect1> <title>Printing Verbose Output</title> <para> By default, PHP_CodeSniffer will run quietly, only printing the report of errors and warnings at the end. If you are checking a large number of files, you may have to wait a while to see the report. If you want to know what is happening, you can turn on verbose output. </para> <para> With verbose output enabled, PHP_CodeSniffer will print the file that it is checking, show you how many tokens and lines the file contains, and let you know how long it took to process. The output will look like this: </para> <example> <title>Sample PHP_CodeSniffer verbose output</title> <programlisting> <![CDATA[ $ phpcs /path/to/code/CodeSniffer -v Registering sniffs... DONE Processing AbstractDocElement.php [1093 tokens in 303 lines]... DONE in < 1 second Processing AbstractParser.php [2360 tokens in 558 lines]... DONE in 2 seconds Processing ClassCommentParser.php [923 tokens in 296 lines]... DONE in < 1 second Processing CommentElement.php [988 tokens in 218 lines]... DONE in < 1 second Processing FunctionCommentParser.php [525 tokens in 184 lines]... DONE in 1 second Processing File.php [10968 tokens in 1805 lines]... DONE in 5 seconds Processing Sniff.php [133 tokens in 94 lines]... DONE in < 1 second Processing SniffException.php [47 tokens in 36 lines]... DONE in < 1 second ]]> </programlisting> </example> </refsect1> <refsect1> <title>Specifying a Coding Standard</title> <para> PHP_CodeSniffer can have multiple coding standards installed to allow a single installation to be used with multiple projects. When checking PHP code, PHP_CodeSniffer can be told which coding standard to use. This is done using the <literal>--standard</literal> command line argument. </para> <para> The example below checks the <filename>myfile.inc</filename> file for violations of the <emphasis>PEAR</emphasis> coding standard (installed by default). </para> <example> <title>Specifying a coding standard to use</title> <programlisting> <![CDATA[ $ phpcs /path/to/code/myfile.inc --standard=PEAR ]]> </programlisting> </example> </refsect1> <refsect1> <title>Printing a List of Installed Coding Standards</title> <para> PHP_CodeSniffer can print you a list of the coding standards that are installed so that you can correctly specify a coding standard to use for testing. You can print this list by specifying the <literal>-i</literal> command line argument. </para> <example> <title>Generating a list of installed coding standards</title> <programlisting> <![CDATA[ $ phpcs -i The installed coding standards are PEAR and Squiz. ]]> </programlisting> </example> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->