Coding Standard Class Methods

Coding Standard Class Methods – Information about overriding coding standard class methods

Introduction

Every coding standard contains a class file. This file allows PHP_CodeSniffer to ask a coding standard for information about itself, and also identify a directory as one that contains code sniffs. This guide describes each of the coding standard class methods that can be overridden to provide additional information about your coding standard to PHP_CodeSniffer.

The getIncludedSniffs() Method

This method allows you to tell PHP_CodeSniffer that, besides the code sniffs you have defined in your Sniffs directory, you want to include code sniffs from other coding standards. You can include single code sniffs, whole directories of code sniffs, or an entire coding standard.

The example below includes the MultipleStatementAlignment sniff from the Generic coding standard, all code sniffs in the Functions category of the Generic coding standard, and all code sniffs defined in the PEAR coding standard.

<?php
/**
 * Return a list of external sniffs to include with this standard.
 *
 * The MyStandard coding standard uses some generic sniffs, and
 * the entire PEAR coding standard.
 *
 * @return array
 */
public function getIncludedSniffs()
{
    return array(
            
'PEAR',
            
'Generic/Sniffs/Formatting/MultipleStatementAlignmentSniff.php',
            
'Generic/Sniffs/Functions',
           );

}
//end getIncludedSniffs()
?>

When including an entire coding standard, you will also include any code sniffs that the standard includes from other coding standards. For example, the PEAR coding standard includes some code sniffs from the Generic coding standard, so including the entire PEAR coding standard will also include those code sniffs. This is a quick and easy way to create your own coding standard based on an existing one.

The getExcludedSniffs() Method

This method allows you to tell PHP_CodeSniffer to exclude code sniffs from your own standard or any other standard you are including using the getIncludedSniffs() method. You can exclude single code sniffs, whole directories of code sniffs, or an entire coding standard.

The example below includes the whole PEAR coding standard, except for the ControlSignature sniff.

<?php
/**
 * Return a list of external sniffs to include with this standard.
 *
 * The MyStandard coding standard uses all PEAR sniffs except one.
 *
 * @return array
 */
public function getIncludedSniffs()
{
    return array(
            
'PEAR',
           );

}
//end getIncludedSniffs()


/**
 * Return a list of external sniffs to exclude from this standard.
 *
 * The MyStandard coding standard uses all PEAR sniffs except one.
 *
 * @return array
 */
public function getExcludedSniffs()
{
    return array(
            
'PEAR/Sniffs/ControlStructures/ControlSignatureSniff.php',
           );

}
//end getExcludedSniffs()
?>

A guide to writing your own code sniffs (Previous) How to configure the Subversion pre-commit hook (Next)
Last updated: Sun, 29 Aug 2010 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report or add a note.
View this page in:

User Notes:

There are no user contributed notes for this page.