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

Class: PHP_CodeSniffer_File

Source Location: /PHP_CodeSniffer-0.7.0/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: 0.7.0

Copyright:

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

Variables

Methods


Inherited Variables

Inherited Methods


Class Details

[line 122]
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 137]

The EOL character this file uses.
  • Access: public

Type:   string


[ Top ]



Method Detail

__construct (Constructor)   [line 326]

PHP_CodeSniffer_File __construct( string $file, array $listeners)

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.

[ Top ]

addError   [line 653]

void addError( string $error, int $stackPtr)

Adds an error to the error stack.
  • Throws: PHP_CodeSniffer_Exception If $stackPtr is null.
  • Access: public

Parameters:

string   $error   —  The error message.
int   $stackPtr   —  The stack position where the error occured.

[ Top ]

addTokenListener   [line 359]

void addTokenListener( PHP_CodeSniffer_Sniff $listener, array $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 680]

void addWarning( string $warning, int $stackPtr)

Adds an warning to the warning stack.
  • Throws: PHP_CodeSniffer_Exception If $stackPtr is null.
  • Access: public

Parameters:

string   $warning   —  The error message.
int   $stackPtr   —  The stack position where the error occured.

[ Top ]

findFirstOnLine   [line 1863]

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 1801]

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 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 ]

findPrevious   [line 1731]

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 ]

getDeclarationName   [line 1357]

string 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.
  • 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 702]

int getErrorCount( )

Returns the number of errors raised.
  • Access: public

[ Top ]

getErrors   [line 726]

array getErrors( )

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

[ Top ]

getFilename   [line 750]

string getFilename( )

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

[ Top ]

getMemberProperties   [line 1579]

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 1393]

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 1492]

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.    );

  • 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 408]

array() getTokens( )

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

[ Top ]

getTokensAsString   [line 1692]

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 ]

getValidScopeOpeners   [line 1340]

array(int) getValidScopeOpeners( )

Returns the token types that are allowed to open scopes.
  • Access: public

[ Top ]

getWarningCount   [line 714]

int getWarningCount( )

Returns the number of warnings raised.
  • Access: public

[ Top ]

getWarnings   [line 738]

array getWarnings( )

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

[ Top ]

hasCondition   [line 1914]

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 ]

isReference   [line 1642]

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 384]

void removeTokenListener( PHP_CodeSniffer_Sniff $listener, array $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 ]

start   [line 420]

void start( )

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

[ Top ]


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