Documentation

GetTokensAsString

Utility functions to retrieve the content of a set of tokens as a string.

In contrast to the PHPCS native \PHP_CodeSniffer\Files\File::getTokensAsString() method, which has $length as the third parameter, all methods in this class expect a stack pointer to an $end token (inclusive) as the third parameter.

Table of Contents

normal() Retrieve the tab-replaced content of the tokens from the specified start position in the token stack to the specified end position (inclusive). string
tabReplaced() Retrieve the tab-replaced content of the tokens from the specified start position in the token stack to the specified end position (inclusive). string
origContent() Retrieve the original content of the tokens from the specified start position in the token stack to the specified end position (inclusive). string
noComments() Retrieve the content of the tokens from the specified start position in the token stack to the specified end position (inclusive) without comments. string
noEmpties() Retrieve the code-tokens only content of the tokens from the specified start position in the token stack to the specified end position (inclusive) without whitespace or comments. string
compact() Retrieve the content of the tokens from the specified start position in the token stack to the specified end position (inclusive) with all whitespace tokens - tabs, new lines, multiple spaces - replaced by a single space and optionally without comments. string
getString() Retrieve the content of the tokens from the specified start position in the token stack to the specified end position (inclusive). string

Methods

normal()

Retrieve the tab-replaced content of the tokens from the specified start position in the token stack to the specified end position (inclusive).

public static normal( $phpcsFile : File , $start : int , $end : int ) : string

This is the default behaviour for PHPCS.

If the tab width is set, either via a (custom) ruleset, the config file or by passing it on the command-line, PHPCS will automatically replace tabs with spaces. The 'content' index key in the $tokens array will contain the tab-replaced content. The 'orig_content' index key in the $tokens array will contain the original content.

Parameters
$phpcsFile : File

The file being scanned.

$start : int

The position to start from in the token stack.

$end : int

The position to end at in the token stack (inclusive).

Tags
see

Similar length-based function.

see

Cross-version compatible version of the original.

since
throws

If the specified start position does not exist.

Return values
string

The token contents.

tabReplaced()

Retrieve the tab-replaced content of the tokens from the specified start position in the token stack to the specified end position (inclusive).

public static tabReplaced( $phpcsFile : File , $start : int , $end : int ) : string

Alias for the \PHPCSUtils\Utils\GetTokensAsString::normal() method.

Parameters
$phpcsFile : File

The file being scanned.

$start : int

The position to start from in the token stack.

$end : int

The position to end at in the token stack (inclusive).

Tags
since
throws

If the specified start position does not exist.

Return values
string

The token contents.

origContent()

Retrieve the original content of the tokens from the specified start position in the token stack to the specified end position (inclusive).

public static origContent( $phpcsFile : File , $start : int , $end : int ) : string

If the original content contained tabs, the return value of this function will also contain tabs.

Parameters
$phpcsFile : File

The file being scanned.

$start : int

The position to start from in the token stack.

$end : int

The position to end at in the token stack (inclusive).

Tags
see

Similar length-based function.

see

Cross-version compatible version of the original.

since
throws

If the specified start position does not exist.

Return values
string

The token contents.

noComments()

Retrieve the content of the tokens from the specified start position in the token stack to the specified end position (inclusive) without comments.

public static noComments( $phpcsFile : File , $start : int , $end : int ) : string
Parameters
$phpcsFile : File

The file being scanned.

$start : int

The position to start from in the token stack.

$end : int

The position to end at in the token stack (inclusive).

Tags
see

Loosely related function.

since
throws

If the specified start position does not exist.

Return values
string

The token contents.

noEmpties()

Retrieve the code-tokens only content of the tokens from the specified start position in the token stack to the specified end position (inclusive) without whitespace or comments.

public static noEmpties( $phpcsFile : File , $start : int , $end : int ) : string

This is, for instance, useful to retrieve a namespace name without stray whitespace or comments. Use this function selectively and with care!

Parameters
$phpcsFile : File

The file being scanned.

$start : int

The position to start from in the token stack.

$end : int

The position to end at in the token stack (inclusive).

Tags
see

Loosely related function.

since
throws

If the specified start position does not exist.

Return values
string

The token contents.

compact()

Retrieve the content of the tokens from the specified start position in the token stack to the specified end position (inclusive) with all whitespace tokens - tabs, new lines, multiple spaces - replaced by a single space and optionally without comments.

public static compact( $phpcsFile : File , $start : int , $end : int [, $stripComments : bool = false ] ) : string
Parameters
$phpcsFile : File

The file being scanned.

$start : int

The position to start from in the token stack.

$end : int

The position to end at in the token stack (inclusive).

$stripComments : bool = false

Whether comments should be stripped from the contents. Defaults to false.

Tags
see

Loosely related function.

since
throws

If the specified start position does not exist.

Return values
string

The token contents.

getString()

Retrieve the content of the tokens from the specified start position in the token stack to the specified end position (inclusive).

protected static getString( $phpcsFile : File , $start : int , $end : int [, $origContent : bool = false ] [, $stripComments : bool = false ] [, $stripWhitespace : bool = false ] [, $compact : bool = false ] ) : string
Parameters
$phpcsFile : File

The file being scanned.

$start : int

The position to start from in the token stack.

$end : int

The position to end at in the token stack (inclusive).

$origContent : bool = false

Whether the original content or the tab replaced content should be used. Defaults to false (= tabs replaced with spaces).

$stripComments : bool = false

Whether comments should be stripped from the contents. Defaults to false.

$stripWhitespace : bool = false

Whether whitespace should be stripped from the contents. Defaults to false.

$compact : bool = false

Whether all whitespace tokens should be replaced with a single space. Defaults to false.

Tags
since
throws

If the specified start position does not exist.

Return values
string

The token contents.

Search results