PHP_CodeSniffer
[ class tree: PHP_CodeSniffer ] [ index: PHP_CodeSniffer ] [ all elements ]

Class: PHP_CodeSniffer

Source Location: /PHP_CodeSniffer-2.0.0a1/CodeSniffer.php

Class Overview


PHP_CodeSniffer tokenises PHP code and detects violations of a defined set of coding standards.


Author(s):

Version:

  • Release: @package_version@

Copyright:

  • 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)

Variables

Methods


Inherited Variables

Inherited Methods


Class Details

[line 68]
PHP_CodeSniffer tokenises PHP code and detects violations of a defined set of coding standards.

Standards are specified by classes that implement the PHP_CodeSniffer_Sniff interface. A sniff registers what token types it wishes to listen for, then PHP_CodeSniffer encounters that token, the sniff is invoked and passed information about where the token was found in the stack, and the token stack itself.

Sniff files and their containing class must be prefixed with Sniff, and have an extension of .php.

Multiple PHP_CodeSniffer operations can be performed by re-calling the process function with different parameters.



[ Top ]


Class Variables

$allowedFileExtensions = array(
                                     'php' => 'PHP',
                                     'inc' => 'PHP',
                                     'js'  => 'JS',
                                     'css' => 'CSS',
                                    )

[line 169]

An array of extensions for files we will check.
  • Access: public

Type:   array


[ Top ]

$allowedTypes = array(
                                   'array',
                                   'boolean',
                                   'float',
                                   'integer',
                                   'mixed',
                                   'object',
                                   'string',
                                   'resource',
                                   'callable',
                                  )

[line 181]

An array of variable types for param/var we will check.
  • Access: public

Type:   array(string)


[ Top ]

$cli =  null

[line 117]

The CLI object controlling the run.
  • Access: public



[ Top ]

$file =  ''

[line 90]

The file or directory that is currently being processed.
  • Access: protected

Type:   string


[ Top ]

$ignorePatterns = array()

[line 162]

An array of patterns to use for skipping files.
  • Access: protected

Type:   array


[ Top ]

$listeners = array()

[line 131]

An array of sniff objects that are being used to check files.
  • Access: protected

Type:   array(PHP_CodeSniffer_Sniff)


[ Top ]

$reporting =  null

[line 124]

The Reporting object controlling report generation.
  • Access: public



[ Top ]

$ruleset = array()

[line 155]

An array of rules from the ruleset.xml file.

It may be empty, indicating that the ruleset does not override any of the default sniff settings.

  • Access: protected

Type:   array


[ Top ]

$rulesetDirs = array()

[line 110]

The directories that the processed rulesets are in.

This is declared static because it is also used in the autoloader to look for sniffs outside the PHPCS install. This way, standards designed to be installed inside PHPCS can also be used from outside the PHPCS Standards directory.

  • Access: protected

Type:   string


[ Top ]

$sniffs = array()

[line 138]

An array of sniffs that are being used to check files.
  • Access: protected

Type:   array(string)


[ Top ]



Method Detail

__construct (Constructor)   [line 213]

PHP_CodeSniffer __construct( [int $verbosity = 0], [int $tabWidth = 0], [string $encoding = 'iso-8859-1'], [bool $interactive = false])

Constructs a PHP_CodeSniffer object.

Parameters:

int   $verbosity   —  The verbosity level. 1: Print progress information. 2: Print tokenizer debug information. 3: Print sniff debug information.
int   $tabWidth   —  The number of spaces each tab represents. If greater than zero, tabs will be replaced by spaces before testing each file.
string   $encoding   —  The charset of the sniffed files. This is important for some reports that output with utf-8 encoding as you don't want it double encoding messages.
bool   $interactive   —  If TRUE, will stop after each file with errors and wait for user input.

[ Top ]

autoload   [line 266]

void autoload( string $className)

Autoload static method for loading classes and interfaces.
  • Access: public

Parameters:

string   $className   —  The name of the class or interface.

[ Top ]

generateDocs   [line 1626]

void generateDocs( string $standard, [array $sniffs = array()], [string $generator = 'Text'])

Generates documentation for a coding standard.
  • Access: public

Parameters:

string   $standard   —  The standard to generate docs for
array   $sniffs   —  A list of sniffs to limit the docs to.
string   $generator   —  The name of the generator class to use.

[ Top ]

getAllConfigData   [line 2290]

string getAllConfigData( )

Get all config data in an array.

[ Top ]

getConfigData   [line 2202]

string|null getConfigData( string $key)

Get a single config value.

Config data is stored in the data dir, in a file called CodeSniffer.conf. It is a simple PHP array.


Parameters:

string   $key   —  The name of the config value.

[ Top ]

getFilesToProcess   [line 1277]

array getFilesToProcess( string $paths, [boolean $local = false])

Get a list of files that will be processed.

If passed directories, this method will find all files within them. The method will also perform file extension and ignore pattern filtering.


Parameters:

string   $paths   —  A list of file or directory paths to process.
boolean   $local   —  If true, only process 1 level of files in directories

[ Top ]

getIgnorePatterns   [line 436]

array getIgnorePatterns( [string $listener = null])

Gets the array of ignore patterns.

Optionally takes a listener to get ignore patterns specified for that sniff only.

  • Access: public

Parameters:

string   $listener   —  The listener to get patterns for. If NULL, all patterns are returned.

[ Top ]

getInstalledStandardPath   [line 2170]

string|null getInstalledStandardPath( string $standard)

Return the path of an installed coding standard.

Coding standards are directories located in the CodeSniffer/Standards directory. Valid coding standards include a ruleset.xml file.

  • Access: public

Parameters:

string   $standard   —  The name of the coding standard.

[ Top ]

getInstalledStandards   [line 2078]

array getInstalledStandards( [boolean $includeGeneric = false], [string $standardsDir = ''])

Get a list of all coding standards installed.

Coding standards are directories located in the CodeSniffer/Standards directory. Valid coding standards include a Sniffs subdirectory.


Parameters:

boolean   $includeGeneric   —  If true, the special "Generic" coding standard will be included if installed.
string   $standardsDir   —  A specific directory to look for standards in. If not specified, PHP_CodeSniffer will look in its default locations.

[ Top ]

getSniffs   [line 1645]

array(PHP_CodeSniffer_Sniff) getSniffs( )

Gets the array of PHP_CodeSniffer_Sniff's.
  • Access: public

[ Top ]

getTokenSniffs   [line 1657]

array() getTokenSniffs( )

Gets the array of PHP_CodeSniffer_Sniff's indexed by token type.
  • Access: public

[ Top ]

initStandard   [line 500]

void initStandard( string|array $standards, [array $restrictions = array()])

Initialise the standard that the run will use.
  • Access: public

Parameters:

string|array   $standards   —  The set of code sniffs we are testing against.
array   $restrictions   —  The sniff codes to restrict the

[ Top ]

isCamelCaps   [line 1888]

boolean isCamelCaps( string $string, [boolean $classFormat = false], [boolean $public = true], [boolean $strict = true])

Returns true if the specified string is in the camel caps format.
  • Access: public

Parameters:

string   $string   —  The string the verify.
boolean   $classFormat   —  If true, check to see if the string is in the class format. Class format strings must start with a capital letter and contain no underscores.
boolean   $public   —  If true, the first character in the string must be an a-z character. If false, the character must be an underscore. This argument is only applicable if $classFormat is false.
boolean   $strict   —  If true, the string must not have two capital letters next to each other. If false, a relaxed camel caps policy is used to allow for acronyms.

[ Top ]

isInstalledStandard   [line 2132]

boolean isInstalledStandard( string $standard)

Determine if a standard is installed.

Coding standards are directories located in the CodeSniffer/Standards directory. Valid coding standards include a ruleset.xml file.


Parameters:

string   $standard   —  The name of the coding standard.

[ Top ]

isUnderscoreName   [line 1960]

boolean isUnderscoreName( string $string)

Returns true if the specified string is in the underscore caps format.
  • Access: public

Parameters:

string   $string   —  The string to verify.

[ Top ]

populateTokenListeners   [line 1175]

void populateTokenListeners( )

Populates the array of PHP_CodeSniffer_Sniff's for this file.
  • Throws: PHP_CodeSniffer_Exception If sniff registration fails.
  • Access: public

[ Top ]

process   [line 479]

void process( string|array $files, string|array $standards, [array $restrictions = array()], [boolean $local = false])

Start a PHP_CodeSniffer run.
  • Access: public

Parameters:

string|array   $files   —  The files and directories to process. For directories, each sub directory will also be traversed for source files.
string|array   $standards   —  The set of code sniffs we are testing against.
array   $restrictions   —  The sniff codes to restrict the violations to.
boolean   $local   —  If true, don't recurse into directories.

[ Top ]

processFile   [line 1443]

PHP_CodeSniffer_File processFile( string $file, [string $contents = null])

Run the code sniffs over a single given file.

Processes the file and runs the PHP_CodeSniffer sniffs to verify that it conforms with the standard. Returns the processed file object, or NULL if no file was processed due to error.

  • See: _processFile()
  • Throws: PHP_CodeSniffer_Exception If the file could not be processed.
  • Access: public

Parameters:

string   $file   —  The file to process.
string   $contents   —  The contents to parse. If NULL, the content is taken from the file system.

[ Top ]

processFiles   [line 571]

void processFiles( string|array $files, [boolean $local = false])

Processes the files/directories that PHP_CodeSniffer was constructed with.
  • Throws: PHP_CodeSniffer_Exception If files are invalid.
  • Access: public

Parameters:

string|array   $files   —  The files and directories to process. For directories, each sub directory will also be traversed for source files.
boolean   $local   —  If true, don't recurse into directories.

[ Top ]

processRuleset   [line 664]

array processRuleset( string $rulesetPath, [int $depth = 0])

Processes a single ruleset and returns a list of the sniffs it represents.

Rules founds within the ruleset are processed immediately, but sniff classes are not registered by this method.

  • Throws: PHP_CodeSniffer_Exception If the ruleset path is invalid.
  • Access: public

Parameters:

string   $rulesetPath   —  The path to a ruleset XML file.
int   $depth   —  How many nested processing steps we are in. This is only used for debug output.

[ Top ]

registerSniffs   [line 1118]

void registerSniffs( array $files, array $restrictions)

Loads and stores sniffs objects used for sniffing files.
  • Access: public

Parameters:

array   $files   —  Paths to the sniff files to register.
array   $restrictions   —  The sniff class names to restrict the allowed listeners to.

[ Top ]

resolveSimpleToken   [line 1771]

array resolveSimpleToken( string $token)

Converts simple tokens into a format that conforms to complex tokens produced by token_get_all().

Simple tokens are tokens that are not in array form when produced from token_get_all().

  • Return: The new token in array format.
  • Access: public

Parameters:

string   $token   —  The simple token to convert.

[ Top ]

resolveTstringToken   [line 1728]

array resolveTstringToken( string $token)

Converts T_STRING tokens into more usable token names.

The token should be produced using the token_get_all() function. Currently, not all T_STRING tokens are converted.

  • Return: The new token.
  • Access: public

Parameters:

string   $token   —  The T_STRING token to convert as constructed by token_get_all().

[ Top ]

setAllowedFileExtensions   [line 382]

void setAllowedFileExtensions( array $extensions)

Sets an array of file extensions that we will allow checking of.

If the extension is one of the defaults, a specific tokenizer will be used. Otherwise, the PHP tokenizer will be used for all extensions passed.

  • Access: public

Parameters:

array   $extensions   —  An array of file extensions.

[ Top ]

setCli   [line 458]

void setCli( object $cli)

Sets the internal CLI object.
  • Access: public

Parameters:

object   $cli   —  The CLI object controlling the run.

[ Top ]

setConfigData   [line 2237]

boolean setConfigData( string $key, string|null $value, [boolean $temp = false])

Set a single config value.

Config data is stored in the data dir, in a file called CodeSniffer.conf. It is a simple PHP array.


Parameters:

string   $key   —  The name of the config value.
string|null   $value   —  The value to set. If null, the config entry is deleted, reverting it to the default value.
boolean   $temp   —  Set this config data temporarily for this script run. This will not write the config data to the config file.

[ Top ]

setEncoding   [line 345]

void setEncoding( string $encoding)

Sets the encoding.
  • Access: public

Parameters:

string   $encoding   —  The charset of the sniffed files. This is important for some reports that output with utf-8 encoding as you don't want it double encoding messages.

[ Top ]

setIgnorePatterns   [line 418]

void setIgnorePatterns( array $patterns)

Sets an array of ignore patterns that we use to skip files and folders.

Patterns are not case sensitive.

  • Access: public

Parameters:

array   $patterns   —  An array of ignore patterns. The pattern is the key and the value is either "absolute" or "relative", depending on how the pattern should be applied to a file path.

[ Top ]

setInteractive   [line 362]

void setInteractive( bool $interactive)

Sets the interactive flag.
  • Access: public

Parameters:

bool   $interactive   —  If TRUE, will stop after each file with errors and wait for user input.

[ Top ]

setSniffProperty   [line 1240]

void setSniffProperty( string $listenerClass, string $name, string $value)

Set a single property for a sniff.
  • Access: public

Parameters:

string   $listenerClass   —  The class name of the sniff.
string   $name   —  The name of the property to change.
string   $value   —  The new value of the property.

[ Top ]

setTabWidth   [line 326]

void setTabWidth( int $tabWidth)

Sets the tab width.
  • Access: public

Parameters:

int   $tabWidth   —  The number of spaces each tab represents. If greater than zero, tabs will be replaced by spaces before testing each file.

[ Top ]

setVerbosity   [line 308]

void setVerbosity( int $verbosity)

Sets the verbosity level.
  • Access: public

Parameters:

int   $verbosity   —  The verbosity level. 1: Print progress information. 2: Print tokenizer debug information. 3: Print sniff debug information.

[ Top ]

shouldIgnoreFile   [line 1377]

bool shouldIgnoreFile( string $path, string $basedir)

Checks filtering rules to see if a file should be ignored.
  • Access: public

Parameters:

string   $path   —  The path to the file being checked.
string   $basedir   —  The directory to use for relative path checks.

[ Top ]

shouldProcessFile   [line 1334]

bool shouldProcessFile( string $path, string $basedir)

Checks filtering rules to see if a file should be checked.

Checks both file extension filters and path ignore filters.

  • Access: public

Parameters:

string   $path   —  The path to the file being checked.
string   $basedir   —  The directory to use for relative path checks.

[ Top ]

standardiseToken   [line 1675]

array standardiseToken( string|array $token)

Takes a token produced from
and produces a more uniform token.

Note that this method also resolves T_STRING tokens into more discrete types, therefore there is no need to call resolveTstringToken()

  • Return: The new token.
  • Access: public

Parameters:

string|array   $token   —  The token to convert.

[ Top ]

suggestType   [line 2001]

string suggestType( string $varType)

Returns a valid variable type for param/var tag.

If type is not one of the standard type, it must be a custom type. Returns the correct type name suggestion if type name is invalid.

  • Access: public

Parameters:

string   $varType   —  The variable type to process.

[ Top ]


Documentation generated on Mon, 11 Mar 2019 15:56:26 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.