## GetTokensAsString

GetTokensAsString.php : 29

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.

##### Tags

since
  1.0.0

  The principle of this class is loosely based on and inspired by the {@see \PHP_CodeSniffer\Files\File::getTokensAsString()} method in the PHPCS native `File` class. Also see {@see \PHPCSUtils\BackCompat\BCFile::getTokensAsString()}.

### Table of Contents

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()

GetTokensAsString.php : 177

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()

GetTokensAsString.php : 129

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()

GetTokensAsString.php : 153

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, 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
  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()

GetTokensAsString.php : 56

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`

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.

#### origContent()

GetTokensAsString.php : 108

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 \PHPCSUtils\Utils\GetTokensAsString::normal() 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 \PHP_CodeSniffer\Files\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()

GetTokensAsString.php : 77

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`

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
  1.0.0
throws
  RuntimeException

  If the specified start position does not exist.

##### Return values

string —

The token contents.

#### getString()

GetTokensAsString.php : 205

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.

## Search results