PHPCSUtils 1.0.11

GetTokensAsString
in package

FinalYes

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

In contrast to the PHPCS native 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.

Tags
since
1.0.0

The principle of this class is loosely based on and inspired by the File::getTokensAsString() method in the PHPCS native File class. Also see BCFile::getTokensAsString().

Table of Contents

Methods

compact()  : string
Retrieve the content of the tokens from the specified start position in the token stack to the specified end position (inclusive) with all consecutive whitespace tokens - tabs, new lines, multiple spaces - replaced by a single space and optionally without comments.
noComments()  : string
Retrieve the content of the tokens from the specified start position in the token stack to the specified end position (inclusive) without comments.
noEmpties()  : string
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.
normal()  : string
Retrieve the tab-replaced content of the tokens from the specified start position in the token stack to the specified end position (inclusive).
origContent()  : string
Retrieve the original content of the tokens from the specified start position in the token stack to the specified end position (inclusive).
tabReplaced()  : string
Retrieve the tab-replaced content of the tokens from the specified start position in the token stack to the specified end position (inclusive).
getString()  : string
Retrieve the content of the tokens from the specified start position in the token stack to the specified end position (inclusive).

Methods

compact()

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

public static compact(File $phpcsFile, int $start, int $end[, bool $stripComments = 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
File::getTokensAsString()

Loosely related function.

since
1.0.0
throws
RuntimeException

If the specified start position does not exist.

Return values
string

The token contents with compacted whitespace and optionally stripped off comments.

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(File $phpcsFile, int $start, int $end) : 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
File::getTokensAsString()

Loosely related function.

since
1.0.0
throws
RuntimeException

If the specified start position does not exist.

Return values
string

The token contents stripped off comments.

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(File $phpcsFile, int $start, int $end) : string

This is useful, for instance, 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
File::getTokensAsString()

Loosely related function.

since
1.0.0
throws
RuntimeException

If the specified start position does not exist.

Return values
string

The token contents stripped off comments and whitespace.

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(File $phpcsFile, int $start, int $end) : string

Alias for the GetTokensAsString::tabReplaced() 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
1.0.0
throws
RuntimeException

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(File $phpcsFile, int $start, int $end) : string

In contrast to the GetTokensAsString::tabReplaced() method, this method will return the original token content for the specified tokens. That means that if the original content contained tabs, the return value of this function will also contain tabs.

The same can be achieved in PHPCS since version 3.3.0, by calling the File::getTokensAsString() method and passing true as the value for the $origContent parameter.

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
File::getTokensAsString()

Similar length-based function.

see
BCFile::getTokensAsString()

Cross-version compatible version of the original.

since
1.0.0
throws
RuntimeException

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(File $phpcsFile, int $start, int $end) : string

This is the default behaviour for PHPCS.

If the tabWidth 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
File::getTokensAsString()

Similar length-based function.

see
BCFile::getTokensAsString()

Cross-version compatible version of the original.

since
1.0.0
throws
RuntimeException

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(File $phpcsFile, int $start, int $end[, bool $origContent = false ][, bool $stripComments = false ][, bool $stripWhitespace = false ][, bool $compact = 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 consecutive whitespace tokens should be replaced with a single space. Defaults to false.

Tags
since
1.0.0
throws
RuntimeException

If the specified start position does not exist.

Return values
string

The token contents.


        
On this page

Search results