cvs: peardoc /en/package/php/php-codesniffer coding-standard-tutorial.xml

From: Date: Tue, 16 Jan 2007 05:03:27 +0000
Subject: cvs: peardoc /en/package/php/php-codesniffer coding-standard-tutorial.xml
Groups: php.pear.doc 
Request: Send a blank email to pear-doc+get-9318@lists.php.net to get a copy of this message
squiz Tue Jan 16 05:03:27 2007 UTC Modified files: /peardoc/en/package/php/php-codesniffer coding-standard-tutorial.xml Log: Changed 'programlisting' sections to 'screen' sections + added sample file that can be used for testing new sniff + fixed class/filename + minor corrections.

http://cvs.php.net/viewvc.cgi/peardoc/en/package/php/php-codesniffer/coding-standard-tutorial.xml?r1=1.1&r2=1.2&diff_format=u Index: peardoc/en/package/php/php-codesniffer/coding-standard-tutorial.xml diff -u peardoc/en/package/php/php-codesniffer/coding-standard-tutorial.xml:1.1 peardoc/en/package/php/php-codesniffer/coding-standard-tutorial.xml:1.2 --- peardoc/en/package/php/php-codesniffer/coding-standard-tutorial.xml:1.1 Fri Jan 12 05:18:32 2007 +++ peardoc/en/package/php/php-codesniffer/coding-standard-tutorial.xml Tue Jan 16 05:03:27 2007 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="ISO-8859-1" ?> -<!-- $Revision: 1.1 $ --> +<!-- $Revision: 1.2 $ --> <refentry id="package.php.php-codesniffer.coding-standard-tutorial"> <refnamediv> <refname>Coding Standard Tutorial</refname> @@ -17,14 +17,18 @@ <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> + </para> + <para> + <screen> + <userinput> <![CDATA[ $ cd /path/to/PHP_CodeSniffer/CodeSniffer/Standards $ mkdir MyStandard $ mkdir MyStandard/Sniffs $ mkdir MyStandard/Tests ]]> - </programlisting> + </userinput> + </screen> </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. @@ -34,14 +38,18 @@ <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> + A sniff requires a single PHP file. It's name should clearly describe the standard that we are enforcing, and must end with <literal>Sniff.php</literal>. For our sniff, we will name the PHP file <filename>DisallowHashCommentsSniff.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: + </para> + <para> + <screen> + <userinput> <![CDATA[ $ cd /path/to/PHP_CodeSniffer/CodeSniffer/Standards/MyStandard/Sniffs $ mkdir Commenting -$ touch Commenting/DisallowHashComments.php +$ touch Commenting/DisallowHashCommentsSniff.php ]]> - </programlisting> + </userinput> + </screen> </para> <note> <simpara> @@ -58,7 +66,7 @@ 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>. + 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 distinguishes 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> @@ -69,7 +77,7 @@ </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. + Some tokens have more indexes than those described above. Have a look in the <filename>PHP/CodeSniffer/File.php</filename> class comment for a list of tokens and their axillary indicies. </simpara> </note> </refsect2> @@ -77,15 +85,17 @@ <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. + Once an error is detected, a sniff should indicate that an error has occurred 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> + <title>DisallowHashCommentsSniff.php</title> + <para> + We now have to write the content of our sniff. The content of the <filename>DisallowHashCommentsSniff.php</filename> file should be the following: + </para> <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 @@ -99,7 +109,7 @@ * @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 $ + * @version CVS: $Id: coding-standard-tutorial.xml,v 1.2 2007/01/16 05:03:27 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ @@ -122,7 +132,7 @@ * @version Release: @package_version@ * @link http://pear.php.net/package/PHP_CodeSniffer */ -class MyStandard_Sniffs_Commenting_DisallowHashComments implements PHP_CodeSniffer_Sniff +class MyStandard_Sniffs_Commenting_DisallowHashCommentsSniff implements PHP_CodeSniffer_Sniff { @@ -170,7 +180,36 @@ <title>Results</title> <para> Now that we have defined a coding standard, let's validate a file that contains hash comments. - <programlisting> + </para> + <para> + The test file we are using has the following contents: + </para> + <para> + <programlisting role="php"> +<![CDATA[ +<?php + +# Check for valid contents. +if ($obj->contentsAreValid($array)) { + $value = $obj->getValue(); + + # Value needs to be an array. + if (is_array($value) === false) { + # Error. + $obj->throwError(); + exit(); + } +} + +?> +]]> + </programlisting> + </para> + <para> + When PHP_CodeSniffer is run on the file using our new coding standard, 3 errors will be reported: + </para> + <para> + <screen> <![CDATA[ $ phpcs --standard=MyStandard Test.php @@ -183,7 +222,7 @@ 9 | ERROR | Hash comments are prohibited -------------------------------------------------------------------------------- ]]> - </programlisting> + </screen> </para> </refsect1>
« previous php.pear.doc (#9318) next »