Documentation

File
in package

Table of Contents

$config  : Config
The config data for the run.
$eolChar  : string
The EOL character this file uses.
$fixer  : Fixer
The Fixer object to control fixing errors.
$fromCache  : bool
Was the file loaded from cache?
$ignored  : bool
If TRUE, the entire file is being ignored.
$numTokens  : int
The number of tokens in this file.
$path  : string
The absolute path to the file associated with this object.
$ruleset  : Ruleset
The ruleset used for the run.
$tokenizer  : Tokenizer
The tokenizer being used for this file.
$tokenizerType  : string
The name of the tokenizer being used for this file.
$activeListener  : string
The class name of the sniff currently processing the file.
$configCache  : array<string|int, mixed>
A cache of often used config settings to improve performance.
$content  : string
The content of the file.
$errorCount  : int
The total number of errors raised.
$errors  : array<string|int, mixed>
The errors raised from sniffs.
$fixableCount  : int
The total number of errors and warnings that can be fixed.
$fixedCount  : int
The total number of errors and warnings that were fixed.
$ignoredCodes  : array<string|int, mixed>
An array of message codes that are being ignored.
$ignoredListeners  : array<string|int, mixed>
An array of sniffs that are being ignored.
$listeners  : array<string|int, Sniff>
An array of sniffs listening to this file's processing.
$listenerTimes  : array<string|int, mixed>
An array of sniffs being processed and how long they took.
$metrics  : array<string|int, mixed>
The metrics recorded by sniffs.
$replayingErrors  : bool
TRUE if errors are being replayed from the cache.
$tokens  : array<string|int, mixed>
The tokens stack map.
$warningCount  : int
The total number of warnings raised.
$warnings  : array<string|int, mixed>
The warnings raised from sniffs.
$metricTokens  : array<string|int, mixed>
The metrics recorded for each token.
__construct()  : void
Constructs a file.
addError()  : bool
Records an error against a specific token in the file.
addErrorOnLine()  : bool
Records an error against a specific line in the file.
addFixableError()  : bool
Records a fixable error against a specific token in the file.
addFixableWarning()  : bool
Records a fixable warning against a specific token in the file.
addWarning()  : bool
Records a warning against a specific token in the file.
addWarningOnLine()  : bool
Records a warning against a specific token in the file.
cleanUp()  : void
Remove vars stored in this file that are no longer required.
disableCaching()  : void
Disables caching of this file.
findEndOfStatement()  : int
Returns the position of the last non-whitespace token in a statement.
findExtendedClassName()  : string|false
Returns the name of the class that the specified class extends.
findFirstOnLine()  : int|false
Returns the position of the first token on a line, matching given type.
findImplementedInterfaceNames()  : array<string|int, mixed>|false
Returns the names of the interfaces that the specified class implements.
findNext()  : int|false
Returns the position of the next specified token(s).
findPrevious()  : int|false
Returns the position of the previous specified token(s).
findStartOfStatement()  : int
Returns the position of the first non-whitespace token in a statement.
getClassProperties()  : array<string|int, mixed>
Returns the visibility and implementation properties of a class.
getCondition()  : int|false
Return the position of the condition for the passed token.
getDeclarationName()  : string|null
Returns the declaration names for classes, interfaces, traits, and functions.
getErrorCount()  : int
Returns the number of errors raised.
getErrors()  : array<string|int, mixed>
Returns the errors raised from processing this file.
getFilename()  : string
Returns the absolute filename of this file.
getFixableCount()  : int
Returns the number of fixable errors/warnings raised.
getFixedCount()  : int
Returns the number of fixed errors/warnings.
getIgnoredLines()  : array<string|int, mixed>
Returns the list of ignored lines.
getMemberProperties()  : array<string|int, mixed>
Returns the visibility and implementation properties of a class member var.
getMethodParameters()  : array<string|int, mixed>
Returns the method parameters for the specified function token.
getMethodProperties()  : array<string|int, mixed>
Returns the visibility and implementation properties of a method.
getMetrics()  : array<string|int, mixed>
Returns the metrics found while processing this file.
getTokens()  : array<string|int, mixed>
Returns the token stack for this file.
getTokensAsString()  : string
Returns the content of the tokens from the specified start position in the token stack for the specified length.
getWarningCount()  : int
Returns the number of warnings raised.
getWarnings()  : array<string|int, mixed>
Returns the warnings raised from processing this file.
hasCondition()  : bool
Determine if the passed token has a condition of one of the passed types.
isReference()  : bool
Determine if the passed token is a reference operator.
parse()  : void
Tokenizes the file and prepares it for the test run.
process()  : void
Starts the stack traversal and tells listeners when tokens are found.
recordMetric()  : bool
Record a metric about the file being examined.
reloadContent()  : void
Reloads the content of the file.
setContent()  : void
Set the content of the file.
addMessage()  : bool
Adds an error to the error stack.

Properties

$config

The config data for the run.

public Config $config = null

$eolChar

The EOL character this file uses.

public string $eolChar = ''

$fixer

The Fixer object to control fixing errors.

public Fixer $fixer = null

$fromCache

Was the file loaded from cache?

public bool $fromCache = false

If TRUE, the file was loaded from a local cache. If FALSE, the file was tokenized and processed fully.

$ignored

If TRUE, the entire file is being ignored.

public bool $ignored = false

$numTokens

The number of tokens in this file.

public int $numTokens = 0

Stored here to save calling count() everywhere.

$path

The absolute path to the file associated with this object.

public string $path = ''

$ruleset

The ruleset used for the run.

public Ruleset $ruleset = null

$tokenizer

The tokenizer being used for this file.

public Tokenizer $tokenizer = null

$tokenizerType

The name of the tokenizer being used for this file.

public string $tokenizerType = 'PHP'

$activeListener

The class name of the sniff currently processing the file.

protected string $activeListener = ''

$configCache

A cache of often used config settings to improve performance.

protected array<string|int, mixed> $configCache = []

Storing them here saves 10k+ calls to __get() in the Config class.

$content

The content of the file.

protected string $content = ''

$errorCount

The total number of errors raised.

protected int $errorCount = 0

$errors

The errors raised from sniffs.

protected array<string|int, mixed> $errors = []
Tags
see
getErrors()

$fixableCount

The total number of errors and warnings that can be fixed.

protected int $fixableCount = 0

$fixedCount

The total number of errors and warnings that were fixed.

protected int $fixedCount = 0

$ignoredCodes

An array of message codes that are being ignored.

protected array<string|int, mixed> $ignoredCodes = []

$ignoredListeners

An array of sniffs that are being ignored.

protected array<string|int, mixed> $ignoredListeners = []

$listeners

An array of sniffs listening to this file's processing.

protected array<string|int, Sniff> $listeners = []

$listenerTimes

An array of sniffs being processed and how long they took.

protected array<string|int, mixed> $listenerTimes = []

$metrics

The metrics recorded by sniffs.

protected array<string|int, mixed> $metrics = []
Tags
see
getMetrics()

$replayingErrors

TRUE if errors are being replayed from the cache.

protected bool $replayingErrors = false

$tokens

The tokens stack map.

protected array<string|int, mixed> $tokens = []

$warningCount

The total number of warnings raised.

protected int $warningCount = 0

$warnings

The warnings raised from sniffs.

protected array<string|int, mixed> $warnings = []
Tags
see
getWarnings()

$metricTokens

The metrics recorded for each token.

private array<string|int, mixed> $metricTokens = []

Stops the same metric being recorded for the same token twice.

Tags
see
getMetrics()

Methods

__construct()

Constructs a file.

public __construct(string $path, Ruleset $ruleset, Config $config) : void
Parameters
$path : string

The absolute path to the file to process.

$ruleset : Ruleset

The ruleset used for the run.

$config : Config

The config data for the run.

Return values
void

addError()

Records an error against a specific token in the file.

public addError(string $error, int $stackPtr, string $code[, array<string|int, mixed> $data = [] ], int $severity[, bool $fixable = false ]) : bool
Parameters
$error : string

The error message.

$stackPtr : int

The stack position where the error occurred.

$code : string

A violation code unique to the sniff message.

$data : array<string|int, mixed> = []

Replacements for the error message.

$severity : int

The severity level for this error. A value of 0 will be converted into the default severity level.

$fixable : bool = false

Can the error be fixed by the sniff?

Return values
bool

addErrorOnLine()

Records an error against a specific line in the file.

public addErrorOnLine(string $error, int $line, string $code[, array<string|int, mixed> $data = [] ], int $severity) : bool
Parameters
$error : string

The error message.

$line : int

The line on which the error occurred.

$code : string

A violation code unique to the sniff message.

$data : array<string|int, mixed> = []

Replacements for the error message.

$severity : int

The severity level for this error. A value of 0 will be converted into the default severity level.

Return values
bool

addFixableError()

Records a fixable error against a specific token in the file.

public addFixableError(string $error, int $stackPtr, string $code[, array<string|int, mixed> $data = [] ], int $severity) : bool

Returns true if the error was recorded and should be fixed.

Parameters
$error : string

The error message.

$stackPtr : int

The stack position where the error occurred.

$code : string

A violation code unique to the sniff message.

$data : array<string|int, mixed> = []

Replacements for the error message.

$severity : int

The severity level for this error. A value of 0 will be converted into the default severity level.

Return values
bool

addFixableWarning()

Records a fixable warning against a specific token in the file.

public addFixableWarning(string $warning, int $stackPtr, string $code[, array<string|int, mixed> $data = [] ], int $severity) : bool

Returns true if the warning was recorded and should be fixed.

Parameters
$warning : string

The error message.

$stackPtr : int

The stack position where the error occurred.

$code : string

A violation code unique to the sniff message.

$data : array<string|int, mixed> = []

Replacements for the warning message.

$severity : int

The severity level for this warning. A value of 0 will be converted into the default severity level.

Return values
bool

addWarning()

Records a warning against a specific token in the file.

public addWarning(string $warning, int $stackPtr, string $code[, array<string|int, mixed> $data = [] ], int $severity[, bool $fixable = false ]) : bool
Parameters
$warning : string

The error message.

$stackPtr : int

The stack position where the error occurred.

$code : string

A violation code unique to the sniff message.

$data : array<string|int, mixed> = []

Replacements for the warning message.

$severity : int

The severity level for this warning. A value of 0 will be converted into the default severity level.

$fixable : bool = false

Can the warning be fixed by the sniff?

Return values
bool

addWarningOnLine()

Records a warning against a specific token in the file.

public addWarningOnLine(string $warning, int $line, string $code[, array<string|int, mixed> $data = [] ], int $severity) : bool
Parameters
$warning : string

The error message.

$line : int

The line on which the warning occurred.

$code : string

A violation code unique to the sniff message.

$data : array<string|int, mixed> = []

Replacements for the warning message.

$severity : int

The severity level for this warning. A value of 0 will will be converted into the default severity level.

Return values
bool

cleanUp()

Remove vars stored in this file that are no longer required.

public cleanUp() : void
Return values
void

disableCaching()

Disables caching of this file.

public disableCaching() : void
Return values
void

findEndOfStatement()

Returns the position of the last non-whitespace token in a statement.

public findEndOfStatement(int $start[, int|string|array<string|int, mixed> $ignore = null ]) : int
Parameters
$start : int

The position to start searching from in the token stack.

$ignore : int|string|array<string|int, mixed> = null

Token types that should not be considered stop points.

Return values
int

findExtendedClassName()

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

public findExtendedClassName(int $stackPtr) : string|false

(works for classes, anonymous classes and interfaces)

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

Parameters
$stackPtr : int

The stack position of the class.

Return values
string|false

findFirstOnLine()

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

public findFirstOnLine(int|string|array<string|int, mixed> $types, int $start[, bool $exclude = false ][, string $value = null ]) : int|false

Returns false if no token can be found.

Parameters
$types : int|string|array<string|int, mixed>

The type(s) of tokens to search for.

$start : int

The position to start searching from in the token stack. The first token matching on this line before this token will be returned.

$exclude : bool = false

If true, find the token that is NOT of the types specified in $types.

$value : string = null

The value that the token must be equal to. If value is omitted, tokens with any value will be returned.

Return values
int|false

findImplementedInterfaceNames()

Returns the names of the interfaces that the specified class implements.

public findImplementedInterfaceNames(int $stackPtr) : array<string|int, mixed>|false

Returns FALSE on error or if there are no implemented interface names.

Parameters
$stackPtr : int

The stack position of the class.

Return values
array<string|int, mixed>|false

findNext()

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

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

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
$types : int|string|array<string|int, mixed>

The type(s) of tokens to search for.

$start : int

The position to start searching from in the token stack.

$end : int|null = null

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.

$exclude : bool = false

If true, find the next token that is NOT of a type specified in $types.

$value : string|null = null

The value that the token(s) must be equal to. If value is omitted, tokens with any value will be returned.

$local : bool = false

If true, tokens outside the current statement will not be checked. i.e., checking will stop at the next semi-colon found.

Tags
see
findPrevious()
Return values
int|false

findPrevious()

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

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

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

Returns false if no token can be found.

Parameters
$types : int|string|array<string|int, mixed>

The type(s) of tokens to search for.

$start : int

The position to start searching from in the token stack.

$end : int|null = null

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.

$exclude : bool = false

If true, find the previous token that is NOT of the types specified in $types.

$value : string|null = null

The value that the token(s) must be equal to. If value is omitted, tokens with any value will be returned.

$local : bool = false

If true, tokens outside the current statement will not be checked. IE. checking will stop at the previous semi-colon found.

Tags
see
findNext()
Return values
int|false

findStartOfStatement()

Returns the position of the first non-whitespace token in a statement.

public findStartOfStatement(int $start[, int|string|array<string|int, mixed> $ignore = null ]) : int
Parameters
$start : int

The position to start searching from in the token stack.

$ignore : int|string|array<string|int, mixed> = null

Token types that should not be considered stop points.

Return values
int

getClassProperties()

Returns the visibility and implementation properties of a class.

public getClassProperties(int $stackPtr) : array<string|int, mixed>

The format of the return value is: array( 'is_abstract' => false, // true if the abstract keyword was found. 'is_final' => false, // true if the final keyword was found. );

Parameters
$stackPtr : int

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

Tags
throws
RuntimeException

If the specified position is not a T_CLASS token.

Return values
array<string|int, mixed>

getCondition()

Return the position of the condition for the passed token.

public getCondition(int $stackPtr, int|string $type[, bool $first = true ]) : int|false

Returns FALSE if the token does not have the condition.

Parameters
$stackPtr : int

The position of the token we are checking.

$type : int|string

The type of token to search for.

$first : bool = true

If TRUE, will return the matched condition furthest away from the passed token. If FALSE, will return the matched condition closest to the passed token.

Return values
int|false

getDeclarationName()

Returns the declaration names for classes, interfaces, traits, and functions.

public getDeclarationName(int $stackPtr) : string|null
Parameters
$stackPtr : int

The position of the declaration token which declared the class, interface, trait, or function.

Tags
throws
RuntimeException

If the specified token is not of type T_FUNCTION, T_CLASS, T_ANON_CLASS, T_CLOSURE, T_TRAIT, or T_INTERFACE.

Return values
string|null

The name of the class, interface, trait, or function; or NULL if the function or class is anonymous.

getErrorCount()

Returns the number of errors raised.

public getErrorCount() : int
Return values
int

getErrors()

Returns the errors raised from processing this file.

public getErrors() : array<string|int, mixed>
Return values
array<string|int, mixed>

getFilename()

Returns the absolute filename of this file.

public getFilename() : string
Return values
string

getFixableCount()

Returns the number of fixable errors/warnings raised.

public getFixableCount() : int
Return values
int

getFixedCount()

Returns the number of fixed errors/warnings.

public getFixedCount() : int
Return values
int

getIgnoredLines()

Returns the list of ignored lines.

public getIgnoredLines() : array<string|int, mixed>
Return values
array<string|int, mixed>

getMemberProperties()

Returns the visibility and implementation properties of a class member var.

public getMemberProperties(int $stackPtr) : array<string|int, mixed>

The format of the return value is:

array( 'scope' => string, // Public, private, or protected. 'scope_specified' => boolean, // TRUE if the scope was explicitly specified. 'is_static' => boolean, // TRUE if the static keyword was found. 'type' => string, // The type of the var (empty if no type specified). 'type_token' => integer, // The stack pointer to the start of the type // or FALSE if there is no type. 'type_end_token' => integer, // The stack pointer to the end of the type // or FALSE if there is no type. 'nullable_type' => boolean, // TRUE if the type is preceded by the nullability // operator. );
Parameters
$stackPtr : int

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

Tags
throws
RuntimeException

If the specified position is not a T_VARIABLE token, or if the position is not a class member variable.

Return values
array<string|int, mixed>

getMethodParameters()

Returns the method parameters for the specified function token.

public getMethodParameters(int $stackPtr) : array<string|int, mixed>

Also supports passing in a USE token for a closure use group.

Each parameter is in the following format:

0 => array( 'name' => '$var', // The variable name. 'token' => integer, // The stack pointer to the variable name. 'content' => string, // The full content of the variable definition. 'has_attributes' => boolean, // Does the parameter have one or more attributes attached ? 'pass_by_reference' => boolean, // Is the variable passed by reference? 'reference_token' => integer, // The stack pointer to the reference operator // or FALSE if the param is not passed by reference. 'variable_length' => boolean, // Is the param of variable length through use of `...` ? 'variadic_token' => integer, // The stack pointer to the ... operator // or FALSE if the param is not variable length. 'type_hint' => string, // The type hint for the variable. 'type_hint_token' => integer, // The stack pointer to the start of the type hint // or FALSE if there is no type hint. 'type_hint_end_token' => integer, // The stack pointer to the end of the type hint // or FALSE if there is no type hint. 'nullable_type' => boolean, // TRUE if the type is preceded by the nullability // operator. 'comma_token' => integer, // The stack pointer to the comma after the param // or FALSE if this is the last param. )

Parameters with default values have additional array indexes of: 'default' => string, // The full content of the default value. 'default_token' => integer, // The stack pointer to the start of the default value. 'default_equal_token' => integer, // The stack pointer to the equals sign.

Parameters declared using PHP 8 constructor property promotion, have these additional array indexes: 'property_visibility' => string, // The property visibility as declared. 'visibility_token' => integer, // The stack pointer to the visibility modifier token.

Parameters
$stackPtr : int

The position in the stack of the function token to acquire the parameters for.

Tags
throws
RuntimeException

If the specified $stackPtr is not of type T_FUNCTION, T_CLOSURE, T_USE, or T_FN.

Return values
array<string|int, mixed>

getMethodProperties()

Returns the visibility and implementation properties of a method.

public getMethodProperties(int $stackPtr) : array<string|int, mixed>

The format of the return value is: array( 'scope' => 'public', // Public, private, or protected 'scope_specified' => true, // TRUE if the scope keyword was found. 'return_type' => '', // The return type of the method. 'return_type_token' => integer, // The stack pointer to the start of the return type // or FALSE if there is no return type. 'return_type_end_token' => integer, // The stack pointer to the end of the return type // or FALSE if there is no return type. 'nullable_return_type' => false, // TRUE if the return type is preceded by the // nullability operator. 'is_abstract' => false, // TRUE if the abstract keyword was found. 'is_final' => false, // TRUE if the final keyword was found. 'is_static' => false, // TRUE if the static keyword was found. 'has_body' => false, // TRUE if the method has a body );

Parameters
$stackPtr : int

The position in the stack of the function token to acquire the properties for.

Tags
throws
RuntimeException

If the specified position is not a T_FUNCTION, T_CLOSURE, or T_FN token.

Return values
array<string|int, mixed>

getMetrics()

Returns the metrics found while processing this file.

public getMetrics() : array<string|int, mixed>
Return values
array<string|int, mixed>

getTokens()

Returns the token stack for this file.

public getTokens() : array<string|int, mixed>
Return values
array<string|int, mixed>

getTokensAsString()

Returns the content of the tokens from the specified start position in the token stack for the specified length.

public getTokensAsString(int $start, int $length[, bool $origContent = false ]) : string
Parameters
$start : int

The position to start from in the token stack.

$length : int

The length of tokens to traverse from the start pos.

$origContent : bool = false

Whether the original content or the tab replaced content should be used.

Tags
throws
RuntimeException

If the specified position does not exist.

Return values
string

The token contents.

getWarningCount()

Returns the number of warnings raised.

public getWarningCount() : int
Return values
int

getWarnings()

Returns the warnings raised from processing this file.

public getWarnings() : array<string|int, mixed>
Return values
array<string|int, mixed>

hasCondition()

Determine if the passed token has a condition of one of the passed types.

public hasCondition(int $stackPtr, int|string|array<string|int, mixed> $types) : bool
Parameters
$stackPtr : int

The position of the token we are checking.

$types : int|string|array<string|int, mixed>

The type(s) of tokens to search for.

Return values
bool

isReference()

Determine if the passed token is a reference operator.

public isReference(int $stackPtr) : bool

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

Parameters
$stackPtr : int

The position of the T_BITWISE_AND token.

Return values
bool

parse()

Tokenizes the file and prepares it for the test run.

public parse() : void
Return values
void

process()

Starts the stack traversal and tells listeners when tokens are found.

public process() : void
Return values
void

recordMetric()

Record a metric about the file being examined.

public recordMetric(int $stackPtr, string $metric, string $value) : bool
Parameters
$stackPtr : int

The stack position where the metric was recorded.

$metric : string

The name of the metric being recorded.

$value : string

The value of the metric being recorded.

Return values
bool

reloadContent()

Reloads the content of the file.

public reloadContent() : void

By default, we have no idea where our content comes from, so we can't do anything.

Return values
void

setContent()

Set the content of the file.

public setContent(string $content) : void

Setting the content also calculates the EOL char being used.

Parameters
$content : string

The file content.

Return values
void

addMessage()

Adds an error to the error stack.

protected addMessage(bool $error, string $message, int $line, int $column, string $code, array<string|int, mixed> $data, int $severity, bool $fixable) : bool
Parameters
$error : bool

Is this an error message?

$message : string

The text of the message.

$line : int

The line on which the message occurred.

$column : int

The column at which the message occurred.

$code : string

A violation code unique to the sniff message.

$data : array<string|int, mixed>

Replacements for the message.

$severity : int

The severity level for this message. A value of 0 will be converted into the default severity level.

$fixable : bool

Can the problem be fixed by the sniff?

Return values
bool

Search results