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

Class: PHP_CodeSniffer_File

Source Location: /PHP_CodeSniffer-1.3.2/CodeSniffer/File.php

Class Overview


A PHP_CodeSniffer_File object represents a PHP source file and the tokens associated with it.


Author(s):

Version:

  • Release: 1.3.2

Copyright:

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

Variables

Methods


Inherited Variables

Inherited Methods


Class Details

[line 117]
A PHP_CodeSniffer_File object represents a PHP source file and the tokens associated with it.

It provides a means for traversing the token stack, along with other token related operations. If a PHP_CodeSniffer_Sniff finds and error or warning within a PHP_CodeSniffer_File, you can raise an error using the addError() or addWarning() methods.

Token Information

Each token within the stack contains information about itself:

  1.    array(
  2.     'code'       => 301,       // the token type code (see token_get_all())
  3.     'content'    => 'if',      // the token content
  4.     'type'       => 'T_IF',    // the token name
  5.     'line'       => 56,        // the line number when the token is located
  6.     'column'     => 12,        // the column in the line where this token
  7.                                // starts (starts from 1)
  8.     'level'      => 2          // the depth a token is within the scopes open
  9.     'conditions' => array(     // a list of scope condition token
  10.                                // positions => codes that
  11.                      2 => 50,  // openened the scopes that this token exists
  12.                      9 => 353// in (see conditional tokens section below)
  13.                     ),
  14.    );

Conditional Tokens

In addition to the standard token fields, conditions contain information to determine where their scope begins and ends:

  1.    array(
  2.     'scope_condition' => 38// the token position of the condition
  3.     'scope_opener'    => 41// the token position that started the scope
  4.     'scope_closer'    => 70// the token position that ended the scope
  5.    );

The condition, the scope opener and the scope closer each contain this information.

Parenthesis Tokens

Each parenthesis token (T_OPEN_PARENTHESIS and T_CLOSE_PARENTHESIS) has a reference to their opening and closing parenthesis, one being itself, the other being its oposite.

  1.    array(
  2.     'parenthesis_opener' => 34,
  3.     'parenthesis_closer' => 40,
  4.    );

Some tokens can "own" a set of parethesis. For example a T_FUNCTION token has parenthesis around its argument list. These tokens also have the parenthesis_opener and and parenthesis_closer indicies. Not all parethesis have owners, for example parenthesis used for arithmetic operations and function calls. The parenthesis tokens that have an owner have the following auxilery array indicies.

  1.    array(
  2.     'parenthesis_opener' => 34,
  3.     'parenthesis_closer' => 40,
  4.     'parenthesis_owner'  => 33,
  5.    );

Each token within a set of parenthesis also has an array indicy 'nested_parenthesis' which is an array of the left parenthesis => right parenthesis token positions.

  1.    'nested_parentheisis' => array(
  2.                              12 => 15
  3.                              11 => 14
  4.                             );

Extended Tokens

PHP_CodeSniffer extends and augments some of the tokens created by token_get_all(). A full list of these tokens can be seen in the Tokens.php file.



[ Top ]


Class Variables

$eolChar =  ''

[line 132]

The EOL character this file uses.
  • Access: public

Type:   string


[ Top ]

$numTokens =  0

[line 162]

The number of tokens in this file.

Stored here to save calling count() everywhere.

  • Access: public

Type:   int


[ Top ]

$phpcs =  null

[line 139]

The PHP_CodeSniffer object controlling this run.
  • Access: public

Type:   PHP_CodeSniffer


[ Top ]

$ruleset = array()

[line 252]

An array of rules from the ruleset.xml file.

This value gets set by PHP_CodeSniffer when the object is created. It may be empty, indicating that the ruleset does not override any of the default sniff settings.

  • Access: protected

Type:   array


[ Top ]

$tokenizer =  null

[line 146]

The tokenizer being used for this file.
  • Access: public

Type:   object


[ Top ]

$tokenizers = array()

[line 241]

An array of extensions mapping to the tokenizer to use.

This value gets set by PHP_CodeSniffer when the object is created.

  • Access: protected

Type:   array


[ Top ]

$tokenizerType =  'PHP'

[line 153]

The tokenizer being used for this file.
  • Access: public

Type:   string


[ Top ]



Method Detail

__construct (Constructor)   [line 271]

PHP_CodeSniffer_File __construct( string $file, $listeners, $tokenizers, $ruleset, PHP_CodeSniffer $phpcs)

Constructs a PHP_CodeSniffer_File.
  • Throws: PHP_CodeSniffer_Exception If the register() method does not return an array.
  • Access: public

Parameters:

string   $file     The absolute path to the file to process.
array(string)   $listeners     The initial listeners listening to processing of this file.
array   $tokenizers     An array of extensions mapping to the tokenizer to use.
array   $ruleset     An array of rules from the ruleset.xml file.
PHP_CodeSniffer   $phpcs     The PHP_CodeSniffer object controlling this run.

[ Top ]

addError   [line 672]

void addError( string $error, int $stackPtr, [string $code = ''], [array $data = array()], [int $severity = 0])

Adds an error to the error stack.
  • Access: public

Parameters:

string   $error     The error message.
int   $stackPtr     The stack position where the error occured.
string   $code     A violation code unique to the sniff message.
array   $data     Replacements for the error message.
int   $severity     The severity level for this error. A value of will be converted into the default severity level.

[ Top ]

addTokenListener   [line 314]

void addTokenListener( PHP_CodeSniffer_Sniff $listener, $tokens)

Adds a listener to the token stack that listens to the specific tokens.

When PHP_CodeSniffer encounters on the the tokens specified in $tokens, it invokes the process method of the sniff.

  • Access: public

Parameters:

PHP_CodeSniffer_Sniff   $listener     The listener to add to the listener stack.
array(int)   $tokens     The token types the listener wishes to listen to.

[ Top ]

addWarning   [line 774]

void addWarning( string $warning, int $stackPtr, [string $code = ''], [array $data = array()], [int $severity = 0])

Adds an warning to the warning stack.
  • Access: public

Parameters:

string   $warning     The error message.
int   $stackPtr     The stack position where the error occured.
string   $code     A violation code unique to the sniff message.
array   $data     Replacements for the warning message.
int   $severity     The severity level for this warning. A value of will be converted into the default severity level.

[ Top ]

cleanUp   [line 543]

void cleanUp( )

Remove vars stored in this sniff that are no longer required.
  • Access: public

[ Top ]

detectLineEndings   [line 615]

string detectLineEndings( string $file, [string $contents = null])

Opens a file and detects the EOL character being used.
  • Throws: PHP_CodeSniffer_Exception If $file could not be opened.
  • Access: public

Parameters:

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

[ Top ]

findExtendedClassName   [line 2718]

string findExtendedClassName( int $stackPtr)

Returns the name of the class that the specified class extends.

Returns FALSE on error or if there is no extended class name.

  • Access: public

Parameters:

int   $stackPtr     The stack position of the class.

[ Top ]

findFirstOnLine   [line 2598]

int findFirstOnLine( int|array $types, int $start, [bool $exclude = false], [string $value = null])

Returns the position of the first token on a line, matching given type.

Returns false if no token can be found.

  • Return: | bool
  • Access: public

Parameters:

int|array   $types     The type(s) of tokens to search for.
int   $start     The position to start searching from in the token stack. The first token matching on this line before this token will be returned.
bool   $exclude     If true, find the token that is NOT of the types specified in $types.
string   $value     The value that the token must be equal to. If value is ommited, tokens with any value will be returned.

[ Top ]

findNext   [line 2540]

int findNext( int|array $types, int $start, [int $end = null], [bool $exclude = false], [string $value = null], [bool $local = false])

Returns the position of the next specified token(s).

If a value is specified, the next token of the specified type(s) containing the specified value will be returned.

Returns false if no token can be found.


Parameters:

int|array   $types     The type(s) of tokens to search for.
int   $start     The position to start searching from in the token stack.
int   $end     The end position to fail if no token is found. if not specified or null, end will default to the end of the token stack.
bool   $exclude     If true, find the next token that is NOT of a type specified in $types.
string   $value     The value that the token(s) must be equal to. If value is ommited, tokens with any value will be returned.
bool   $local     If true, tokens outside the current statement will not be checked. i.e., checking will stop at the next semi-colon found.

[ Top ]

findPrevious   [line 2473]

int findPrevious( int|array $types, int $start, [int $end = null], [bool $exclude = false], [string $value = null], [bool $local = false])

Returns the position of the next specified token(s).

If a value is specified, the next token of the specified type(s) containing the specified value will be returned.

Returns false if no token can be found.


Parameters:

int|array   $types     The type(s) of tokens to search for.
int   $start     The position to start searching from in the token stack.
int   $end     The end position to fail if no token is found. if not specified or null, end will default to the start of the token stack.
bool   $exclude     If true, find the next token that are NOT of the types specified in $types.
string   $value     The value that the token(s) must be equal to. If value is ommited, tokens with any value will be returned.
bool   $local     If true, tokens outside the current statement will not be cheked. IE. checking will stop at the next semi-colon found.

[ Top ]

getClassProperties   [line 2309]

array getClassProperties( int $stackPtr)

Returns the visibility and implementation properies of a class.

The format of the array is:

  1.    array(
  2.     'is_abstract' => false// true if the abstract keyword was found.
  3.     'is_final'    => false// true if the final keyword was found.
  4.    );

  • Throws: PHP_CodeSniffer_Exception If the specified position is not a T_CLASS token.
  • Access: public

Parameters:

int   $stackPtr     The position in the stack of the T_CLASS token to acquire the properties for.

[ Top ]

getCondition   [line 2685]

int getCondition( int $stackPtr, int $type)

Return the position of the condition for the passed token.

Returns FALSE if the token does not have the condition.

  • Access: public

Parameters:

int   $stackPtr     The position of the token we are checking.
int   $type     The type of token to search for.

[ Top ]

getDeclarationName   [line 1920]

string|null getDeclarationName( int $stackPtr)

Returns the declaration names for T_CLASS, T_INTERFACE and T_FUNCTION tokens.
  • Return: The name of the class, interface or function. or NULL if the function is a closure.
  • Throws: PHP_CodeSniffer_Exception If the specified token is not of type T_FUNCTION, T_CLASS or T_INTERFACE.
  • Access: public

Parameters:

int   $stackPtr     The position of the declaration token which declared the class, interface or function.

[ Top ]

getErrorCount   [line 869]

int getErrorCount( )

Returns the number of errors raised.
  • Access: public

[ Top ]

getErrors   [line 905]

array getErrors( )

Returns the errors raised from processing this file.
  • Access: public

[ Top ]

getFilename   [line 929]

string getFilename( )

Returns the absolute filename of this file.
  • Access: public

[ Top ]

getIgnoredLines   [line 893]

array getIgnoredLines( )

Returns the list of ignored lines.
  • Access: public

[ Top ]

getMemberProperties   [line 2214]

array getMemberProperties( int $stackPtr)

Returns the visibility and implementation properies of the class member variable found at the specified position in the stack.

The format of the array is:

  1.    array(
  2.     'scope'       => 'public'// public private or protected
  3.     'is_static'   => false,    // true if the static keyword was found.
  4.    );

  • Throws: PHP_CodeSniffer_Exception If the specified position is not a T_VARIABLE token, or if the position is not a class member variable.
  • Access: public

Parameters:

int   $stackPtr     The position in the stack of the T_VARIABLE token to acquire the properties for.

[ Top ]

getMethodParameters   [line 2003]

array() getMethodParameters( int $stackPtr)

Returns the method parameters for the specified T_FUNCTION token.

Each parameter is in the following format:

  1.    0 => array(
  2.          'name'              => '$var',  // The variable name.
  3.          'pass_by_reference' => false,   // Passed by reference.
  4.          'type_hint'         => string,  // Type hint for array or custom type
  5.         )

Parameters with default values have and additional array indice of 'default' with the value of the default as a string.

  • Throws: PHP_CodeSniffer_Exception If the specified $stackPtr is not of type T_FUNCTION.
  • Access: public

Parameters:

int   $stackPtr     The position in the stack of the T_FUNCTION token to acquire the parameters for.

[ Top ]

getMethodProperties   [line 2126]

array getMethodProperties( int $stackPtr)

Returns the visibility and implementation properies of a method.

The format of the array is:

  1.    array(
  2.     'scope'           => 'public'// public private or protected
  3.     'scope_specified' => true,     // true is scope keyword was found.
  4.     'is_abstract'     => false,    // true if the abstract keyword was found.
  5.     'is_final'        => false,    // true if the final keyword was found.
  6.     'is_static'       => false,    // true if the static keyword was found.
  7.     'is_closure'      => false,    // true if no name is found.
  8.    );

  • Throws: PHP_CodeSniffer_Exception If the specified position is not a T_FUNCTION token.
  • Access: public

Parameters:

int   $stackPtr     The position in the stack of the T_FUNCTION token to acquire the properties for.

[ Top ]

getTokens   [line 365]

array() getTokens( )

Returns the token stack for this file.
  • Access: public

[ Top ]

getTokensAsString   [line 2434]

string getTokensAsString( int $start, int $length)

Returns the content of the tokens from the specified start position in the token stack for the specified legnth.
  • Return: The token contents.
  • Access: public

Parameters:

int   $start     The position to start from in the token stack.
int   $length     The length of tokens to traverse from the start pos.

[ Top ]

getWarningCount   [line 881]

int getWarningCount( )

Returns the number of warnings raised.
  • Access: public

[ Top ]

getWarnings   [line 917]

array getWarnings( )

Returns the warnings raised from processing this file.
  • Access: public

[ Top ]

hasCondition   [line 2648]

boolean hasCondition( int $stackPtr, int|array $types)

Determine if the passed token has a condition of one of the passed types.
  • Access: public

Parameters:

int   $stackPtr     The position of the token we are checking.
int|array   $types     The type(s) of tokens to search for.

[ Top ]

isAnonymousFunction   [line 1952]

boolean isAnonymousFunction( int $stackPtr)

Check if the token at the specified position is a anonymus function.
  • Throws: PHP_CodeSniffer_Exception If the specified token is not of type T_FUNCTION
  • Access: public

Parameters:

int   $stackPtr     The position of the declaration token which declared the class, interface or function.

[ Top ]

isReference   [line 2360]

boolean isReference( int $stackPtr)

Determine if the passed token is a reference operator.

Returns true if the specified token position represents a reference. Returns false if the token represents a bitwise operator.

  • Access: public

Parameters:

int   $stackPtr     The position of the T_BITWISE_AND token.

[ Top ]

removeTokenListener   [line 339]

void removeTokenListener( PHP_CodeSniffer_Sniff $listener, $tokens)

Removes a listener from listening from the specified tokens.
  • Access: public

Parameters:

PHP_CodeSniffer_Sniff   $listener     The listener to remove from the listener stack.
array(int)   $tokens     The token types the listener wishes to stop listen to.

[ Top ]

setActiveListener   [line 294]

void setActiveListener( string $activeListener)

Sets the name of the currently active sniff.
  • Access: public

Parameters:

string   $activeListener     The class name of the current sniff.

[ Top ]

start   [line 380]

void start( [string $contents = null])

Starts the stack traversal and tells listeners when tokens are found.
  • Access: public

Parameters:

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

[ Top ]

tokenizeString   [line 948]

array tokenizeString( string $string, object $tokenizer, [string $eolChar = '\n'])

Creates an array of tokens when given some PHP code.

Starts by using token_get_all() but does a lot of extra processing to insert information about the context of the token.

  • Access: public

Parameters:

string   $string     The string to tokenize.
object   $tokenizer     A tokenizer class to use to tokenize the string.
string   $eolChar     The EOL character to use for splitting strings.

[ Top ]


Documentation generated on Thu, 01 Dec 2011 05:30:41 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.