cvs: peardoc /en/package/php/php-codesniffer coding-standard-tutorial.xml
| From: | Greg Sherwood | Date: | Mon, 07 Jul 2008 06:37:28 +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-10972@lists.php.net to get a copy of this message | ||
squiz Mon Jul 7 06:37:28 2008 UTC
Modified files:
/peardoc/en/package/php/php-codesniffer
coding-standard-tutorial.xml
Log:
The coding standard tutorial now guides people to create their standard outside of PHP_CodeSniffer
and use the absolute path when telling phpcs which standard to use. It also contains information
about support for different tokenizers (php and js)
http://cvs.php.net/viewvc.cgi/peardoc/en/package/php/php-codesniffer/coding-standard-tutorial.xml?r1=1.6&r2=1.7&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.6 peardoc/en/package/php/php-codesniffer/coding-standard-tutorial.xml:1.7 --- peardoc/en/package/php/php-codesniffer/coding-standard-tutorial.xml:1.6 Mon Oct 15 03:28:17 2007 +++ peardoc/en/package/php/php-codesniffer/coding-standard-tutorial.xml Mon Jul 7 06:37:28 2008 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="ISO-8859-1" ?> -<!-- $Revision: 1.6 $ --> +<!-- $Revision: 1.7 $ --> <refentry id="package.php.php-codesniffer.coding-standard-tutorial"> <refnamediv> <refname>Coding Standard Tutorial</refname> @@ -16,12 +16,26 @@ <refsect1> <title>Creating the Coding Standard Directory</title> <para> - All sniffs in PHP_CodeSniffer must belong to a coding standard. A coding standard is a directory inside PHP_CodeSniffer and a single class, 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: + All sniffs in PHP_CodeSniffer must belong to a coding standard. A coding standard is a directory with a specific sub-directory structure and a single class, 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: </para> <para> <screen> <userinput> <![CDATA[ +$ mkdir MyStandard +$ mkdir MyStandard/Sniffs +]]> + </userinput> + </screen> + </para> + <note> + <simpara> + As this coding standard directory sits outside the main PHP_CodeSniffer directory structure, PHP_CodeSniffer will not show it as an installed standard when using the <literal>-i</literal> command line argument. If you want your standard to be shown as installed, create the MyStandard directory inside the PHP_CodeSniffer install: + </simpara> + <para> + <screen> + <userinput> +<![CDATA[ $ cd /path/to/PHP_CodeSniffer/CodeSniffer/Standards $ mkdir MyStandard $ mkdir MyStandard/Sniffs @@ -29,8 +43,9 @@ </userinput> </screen> </para> + </note> <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>MyStandard</filename> directory represents our coding standard. The <filename>Sniffs</filename> sub-directory is used to store all the sniff files for this coding standard. </para> <para> Now that our directory structure is created, we need to add our class file. This file will allow PHP_CodeSniffer to ask our coding standard for information about itself, and also identify this directory as one that contains code sniffs. @@ -39,7 +54,7 @@ <screen> <userinput> <![CDATA[ -$ cd /path/to/PHP_CodeSniffer/CodeSniffer/Standards/MyStandard +$ cd MyStandard $ touch MyStandardCodingStandard.php ]]> </userinput> @@ -61,11 +76,13 @@ * @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.6 2007/10/15 03:28:17 squiz Exp $ + * @version CVS: $Id: coding-standard-tutorial.xml,v 1.7 2008/07/07 06:37:28 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ -require_once 'PHP/CodeSniffer/Standards/CodingStandard.php'; +if (class_exists('PHP_CodeSniffer_Standards_CodingStandard', true) === false) { + throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_CodingStandard not found'); +} /** * MyStandard Coding Standard. @@ -95,13 +112,13 @@ <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, 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: + 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 +$ cd Sniffs $ mkdir Commenting $ touch Commenting/DisallowHashCommentsSniff.php ]]> @@ -114,13 +131,13 @@ </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>. + Each sniff must implement the <literal>PHP_CodeSniffer_Sniff</literal> interface so that 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. + The <function>register</function> method allows a sniff to subscribe to one or more token types that it wants 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 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>. @@ -130,11 +147,11 @@ <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. + 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 (e.g., '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 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. + Some tokens have more indexes than those described above. Have a look in the <filename>PHP/CodeSniffer/File.php</filename> class comment for a full list of token indexes. </simpara> </note> </refsect2> @@ -157,8 +174,7 @@ <![CDATA[ <?php /** - * PHP_CodeSniffer tokenises PHP code and detects violations of a - * defined set of coding standards. + * This sniff prohibits the use of Perl style hash comments. * * PHP version 5 * @@ -166,12 +182,10 @@ * @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.6 2007/10/15 03:28:17 squiz Exp $ + * @version CVS: $Id: coding-standard-tutorial.xml,v 1.7 2008/07/07 06:37:28 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. * @@ -231,8 +245,29 @@ ]]> </programlisting> </para> + <note> + <simpara> + By default, PHP_CodeSniffer assumes all sniffs are designed to check PHP code only. You can specify a list of tokenizers that your sniff supports, allowing it to be used on PHP code, JavaScript code, or both. You do this by setting the <literal>$supportedTokenizers</literal> member variable in your sniff. Adding the following code to your sniff will tell PHP_CodeSniffer that it can be used to check both PHP and JavaScript code: + </simpara> + <para> + <programlisting role="php"> +<![CDATA[ +/** + * A list of tokenizers this sniff supports. + * + * @var array + */ +public $supportedTokenizers = array( + 'PHP', + 'JS', + ); +]]> + </programlisting> + </para> + </note> </refsect1> + <refsect1> <title>Results</title> <para> @@ -268,7 +303,7 @@ <para> <screen> <![CDATA[ -$ phpcs --standard=MyStandard Test.php +$ phpcs --standard=/path/to/MyStandard Test.php FILE: Test.php -------------------------------------------------------------------------------- @@ -281,8 +316,21 @@ ]]> </screen> </para> + <note> + <simpara> + Note that we pass the absolute path to our coding standard directory on the command line because our standard is not installed inside the main PHP_CodeSniffer directory structure. If you have created your standard inside PHP_CodeSniffer, you can simply pass the name of the standard: + </simpara> + <para> + <screen> + <userinput> +<![CDATA[ +$ phpcs --standard=MyStandard Test.php +]]> + </userinput> + </screen> + </para> + </note> </refsect1> - </refentry>
http://cvs.php.net/viewvc.cgi/peardoc/en/package/php/php-codesniffer/coding-standard-tutorial.xml?r1=1.6&r2=1.7&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.6 peardoc/en/package/php/php-codesniffer/coding-standard-tutorial.xml:1.7 --- peardoc/en/package/php/php-codesniffer/coding-standard-tutorial.xml:1.6 Mon Oct 15 03:28:17 2007 +++ peardoc/en/package/php/php-codesniffer/coding-standard-tutorial.xml Mon Jul 7 06:37:28 2008 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="ISO-8859-1" ?> -<!-- $Revision: 1.6 $ --> +<!-- $Revision: 1.7 $ --> <refentry id="package.php.php-codesniffer.coding-standard-tutorial"> <refnamediv> <refname>Coding Standard Tutorial</refname> @@ -16,12 +16,26 @@ <refsect1> <title>Creating the Coding Standard Directory</title> <para> - All sniffs in PHP_CodeSniffer must belong to a coding standard. A coding standard is a directory inside PHP_CodeSniffer and a single class, 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: + All sniffs in PHP_CodeSniffer must belong to a coding standard. A coding standard is a directory with a specific sub-directory structure and a single class, 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: </para> <para> <screen> <userinput> <![CDATA[ +$ mkdir MyStandard +$ mkdir MyStandard/Sniffs +]]> + </userinput> + </screen> + </para> + <note> + <simpara> + As this coding standard directory sits outside the main PHP_CodeSniffer directory structure, PHP_CodeSniffer will not show it as an installed standard when using the <literal>-i</literal> command line argument. If you want your standard to be shown as installed, create the MyStandard directory inside the PHP_CodeSniffer install: + </simpara> + <para> + <screen> + <userinput> +<![CDATA[ $ cd /path/to/PHP_CodeSniffer/CodeSniffer/Standards $ mkdir MyStandard $ mkdir MyStandard/Sniffs @@ -29,8 +43,9 @@ </userinput> </screen> </para> + </note> <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>MyStandard</filename> directory represents our coding standard. The <filename>Sniffs</filename> sub-directory is used to store all the sniff files for this coding standard. </para> <para> Now that our directory structure is created, we need to add our class file. This file will allow PHP_CodeSniffer to ask our coding standard for information about itself, and also identify this directory as one that contains code sniffs. @@ -39,7 +54,7 @@ <screen> <userinput> <![CDATA[ -$ cd /path/to/PHP_CodeSniffer/CodeSniffer/Standards/MyStandard +$ cd MyStandard $ touch MyStandardCodingStandard.php ]]> </userinput> @@ -61,11 +76,13 @@ * @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.6 2007/10/15 03:28:17 squiz Exp $ + * @version CVS: $Id: coding-standard-tutorial.xml,v 1.7 2008/07/07 06:37:28 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ -require_once 'PHP/CodeSniffer/Standards/CodingStandard.php'; +if (class_exists('PHP_CodeSniffer_Standards_CodingStandard', true) === false) { + throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_CodingStandard not found'); +} /** * MyStandard Coding Standard. @@ -95,13 +112,13 @@ <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, 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: + 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 +$ cd Sniffs $ mkdir Commenting $ touch Commenting/DisallowHashCommentsSniff.php ]]> @@ -114,13 +131,13 @@ </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>. + Each sniff must implement the <literal>PHP_CodeSniffer_Sniff</literal> interface so that 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. + The <function>register</function> method allows a sniff to subscribe to one or more token types that it wants 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 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>. @@ -130,11 +147,11 @@ <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. + 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 (e.g., '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 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. + Some tokens have more indexes than those described above. Have a look in the <filename>PHP/CodeSniffer/File.php</filename> class comment for a full list of token indexes. </simpara> </note> </refsect2> @@ -157,8 +174,7 @@ <![CDATA[ <?php /** - * PHP_CodeSniffer tokenises PHP code and detects violations of a - * defined set of coding standards. + * This sniff prohibits the use of Perl style hash comments. * * PHP version 5 * @@ -166,12 +182,10 @@ * @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.6 2007/10/15 03:28:17 squiz Exp $ + * @version CVS: $Id: coding-standard-tutorial.xml,v 1.7 2008/07/07 06:37:28 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. * @@ -231,8 +245,29 @@ ]]> </programlisting> </para> + <note> + <simpara> + By default, PHP_CodeSniffer assumes all sniffs are designed to check PHP code only. You can specify a list of tokenizers that your sniff supports, allowing it to be used on PHP code, JavaScript code, or both. You do this by setting the <literal>$supportedTokenizers</literal> member variable in your sniff. Adding the following code to your sniff will tell PHP_CodeSniffer that it can be used to check both PHP and JavaScript code: + </simpara> + <para> + <programlisting role="php"> +<![CDATA[ +/** + * A list of tokenizers this sniff supports. + * + * @var array + */ +public $supportedTokenizers = array( + 'PHP', + 'JS', + ); +]]> + </programlisting> + </para> + </note> </refsect1> + <refsect1> <title>Results</title> <para> @@ -268,7 +303,7 @@ <para> <screen> <![CDATA[ -$ phpcs --standard=MyStandard Test.php +$ phpcs --standard=/path/to/MyStandard Test.php FILE: Test.php -------------------------------------------------------------------------------- @@ -281,8 +316,21 @@ ]]> </screen> </para> + <note> + <simpara> + Note that we pass the absolute path to our coding standard directory on the command line because our standard is not installed inside the main PHP_CodeSniffer directory structure. If you have created your standard inside PHP_CodeSniffer, you can simply pass the name of the standard: + </simpara> + <para> + <screen> + <userinput> +<![CDATA[ +$ phpcs --standard=MyStandard Test.php +]]> + </userinput> + </screen> + </para> + </note> </refsect1> - </refentry>