PHP Class PhpCsFixer\Tokenizer\Tokens

Its role is to provide the ability to manage collection and navigate through it. As a token prototype you should understand a single element generated by token_get_all.
Author: Dariusz Rumiński ([email protected])
Inheritance: extends SplFixedArray
Exibir arquivo Open project: friendsofphp/php-cs-fixer Class Usage Examples

Public Methods

Method Description
__clone ( ) Clone tokens collection.
clearCache ( string | null $key = null ) Clear cache - one position or all of them.
clearChanged ( ) Clear internal flag if collection was changed and flag for all collection's items.
clearEmptyTokens ( ) Clear empty tokens.
clearRange ( integer $indexStart, integer $indexEnd ) Clear tokens in the given range.
clearTokenAndMergeSurroundingWhitespace ( integer $index ) Clear token and merge surrounding whitespace tokens.
detectBlockType ( PhpCsFixer\Tokenizer\Token $token ) : null | array Detect type of block.
ensureWhitespaceAtIndex ( integer $index, integer $indexOffset, string $whitespace ) : boolean Ensure that on given index is a whitespace with given kind.
findBlockEnd ( integer $type, integer $searchIndex, boolean $findEnd = true ) : integer Find block end.
findGivenKind ( integer | array $possibleKind, integer $start, integer | null $end = null ) : array Find tokens of given kind.
findSequence ( array $sequence, integer $start, integer $end = null, boolean | array $caseSensitive = true ) : array | null Find a sequence of meaningful tokens and returns the array of their locations.
fromArray ( PhpCsFixer\Tokenizer\Token[] $array, boolean $saveIndexes = null ) : Tokens Create token collection from array.
fromCode ( string $code ) : Tokens Create token collection directly from code.
generateCode ( ) : string Generate code from tokens.
generatePartialCode ( integer $start, integer $end ) : string Generate code from tokens between given indexes.
getBlockEdgeDefinitions ( ) : array Return block edge definitions.
getCodeHash ( ) : string Get hash of code.
getMeaningfulTokenSibling ( integer $index, integer $direction ) : integer | null Get index for closest sibling token that is not a whitespace or comment.
getNextMeaningfulToken ( integer $index ) : integer | null Get index for closest next token that is not a whitespace or comment.
getNextNonWhitespace ( integer $index, null | string $whitespaces = null ) : integer | null Get index for closest next token which is non whitespace.
getNextTokenOfKind ( integer $index, array $tokens = [], boolean $caseSensitive = true ) : integer | null Get index for closest next token of given kind.
getNonEmptySibling ( integer $index, integer $direction ) : integer | null Get index for closest sibling token which is not empty.
getNonWhitespaceSibling ( integer $index, integer $direction, null | string $whitespaces = null ) : integer | null Get index for closest sibling token which is non whitespace.
getPrevMeaningfulToken ( integer $index ) : integer | null Get index for closest previous token that is not a whitespace or comment.
getPrevNonWhitespace ( integer $index, null | string $whitespaces = null ) : integer | null Get index for closest previous token which is non whitespace.
getPrevTokenOfKind ( integer $index, array $tokens = [], boolean $caseSensitive = true ) : integer | null Get index for closest previous token of given kind.
getTokenNotOfKindSibling ( integer $index, integer $direction, array $tokens = [] ) : integer | null Get index for closest sibling token not of given kind.
getTokenOfKindSibling ( integer $index, integer $direction, array $tokens = [], boolean $caseSensitive = true ) : integer | null Get index for closest sibling token of given kind.
insertAt ( integer $index, Tokens | PhpCsFixer\Tokenizer\Token[] | PhpCsFixer\Tokenizer\Token $items ) Insert instances of Token inside collection.
isAllTokenKindsFound ( array $tokenKinds ) : boolean Check if all token kinds given as argument are found.
isAnyTokenKindsFound ( array $tokenKinds ) : boolean Check if any token kind given as argument is found.
isChanged ( ) : boolean Check if collection was change: collection itself (like insert new tokens) or any of collection's elements.
isMonolithicPhp ( ) : boolean Checks for monolithic PHP code.
isPartialCodeMultiline ( integer $start, integer $end ) : boolean Check if partial code is multiline.
isTokenKindFound ( integer | string $tokenKind ) : boolean Check if token kind given as argument is found.
offsetSet ( integer $index, PhpCsFixer\Tokenizer\Token $newval ) Set collection item.
offsetUnset ( integer $index ) Unset collection item.
overrideAt ( integer $index, PhpCsFixer\Tokenizer\Token | array | string $token ) Override token at given index and register it.
overrideRange ( integer $indexStart, integer $indexEnd, Tokens | PhpCsFixer\Tokenizer\Token[] $items ) Override tokens at given range.
removeLeadingWhitespace ( integer $index, null | string $whitespaces = null ) Removes all the leading whitespace.
removeTrailingWhitespace ( integer $index, null | string $whitespaces = null ) Removes all the trailing whitespace.
setCode ( string $code ) Set code. Clear all current content and replace it by new Token items generated from code directly.
setSize ( integer $size ) Set new size of collection.
toJson ( )

Private Methods

Method Description
calculateCodeHash ( string $code ) : integer Calculate hash for code.
changeCodeHash ( string $codeHash ) Change code hash.
getCache ( string $key ) : Tokens Get cache value for given key.
hasCache ( string $key ) : boolean Check if given key exists in cache.
registerFoundToken ( PhpCsFixer\Tokenizer\Token | array | string $token ) Register token as found.
setCache ( string $key, Tokens $value ) Set cache item.

Method Details

__clone() public method

Clone tokens collection.
public __clone ( )

clearCache() public static method

Clear cache - one position or all of them.
public static clearCache ( string | null $key = null )
$key string | null position to clear, when null clear all

clearChanged() public method

Clear internal flag if collection was changed and flag for all collection's items.
public clearChanged ( )

clearEmptyTokens() public method

Empty tokens can occur e.g. after calling clear on item of collection.
public clearEmptyTokens ( )

clearRange() public method

Clear tokens in the given range.
public clearRange ( integer $indexStart, integer $indexEnd )
$indexStart integer
$indexEnd integer

clearTokenAndMergeSurroundingWhitespace() public method

Clear token and merge surrounding whitespace tokens.

detectBlockType() public static method

Detect type of block.
public static detectBlockType ( PhpCsFixer\Tokenizer\Token $token ) : null | array
$token PhpCsFixer\Tokenizer\Token token
return null | array array with 'type' and 'isStart' keys or null if not found

ensureWhitespaceAtIndex() public method

If there is a whitespace then it's content will be modified. If not - the new Token will be added.
public ensureWhitespaceAtIndex ( integer $index, integer $indexOffset, string $whitespace ) : boolean
$index integer index
$indexOffset integer index offset for Token insertion
$whitespace string whitespace to set
return boolean if new Token was added

findBlockEnd() public method

Find block end.
public findBlockEnd ( integer $type, integer $searchIndex, boolean $findEnd = true ) : integer
$type integer type of block, one of BLOCK_TYPE_*
$searchIndex integer index of opening brace
$findEnd boolean if method should find block's end, default true, otherwise method find block's start
return integer index of closing brace

findGivenKind() public method

Find tokens of given kind.
public findGivenKind ( integer | array $possibleKind, integer $start, integer | null $end = null ) : array
$possibleKind integer | array kind or array of kind
$start integer optional offset
$end integer | null optional limit
return array array of tokens of given kinds or assoc array of arrays

findSequence() public method

Find a sequence of meaningful tokens and returns the array of their locations.
public findSequence ( array $sequence, integer $start, integer $end = null, boolean | array $caseSensitive = true ) : array | null
$sequence array an array of tokens (same format used by getNextTokenOfKind)
$start integer start index, defaulting to the start of the file
$end integer end index, defaulting to the end of the file
$caseSensitive boolean | array global case sensitiveness or an array of booleans, whose keys should match the ones used in $others. If any is missing, the default case-sensitive comparison is used
return array | null an array containing the tokens matching the sequence elements, indexed by their position

fromArray() public static method

Create token collection from array.
public static fromArray ( PhpCsFixer\Tokenizer\Token[] $array, boolean $saveIndexes = null ) : Tokens
$array PhpCsFixer\Tokenizer\Token[] the array to import
$saveIndexes boolean save the numeric indexes used in the original array, default is yes
return Tokens

fromCode() public static method

Create token collection directly from code.
public static fromCode ( string $code ) : Tokens
$code string PHP code
return Tokens

generateCode() public method

Generate code from tokens.
public generateCode ( ) : string
return string

generatePartialCode() public method

Generate code from tokens between given indexes.
public generatePartialCode ( integer $start, integer $end ) : string
$start integer start index
$end integer end index
return string

getBlockEdgeDefinitions() public static method

Return block edge definitions.
public static getBlockEdgeDefinitions ( ) : array
return array

getCodeHash() public method

Get hash of code.
public getCodeHash ( ) : string
return string

getMeaningfulTokenSibling() public method

Get index for closest sibling token that is not a whitespace or comment.
public getMeaningfulTokenSibling ( integer $index, integer $direction ) : integer | null
$index integer token index
$direction integer direction for looking, +1 or -1
return integer | null

getNextMeaningfulToken() public method

Get index for closest next token that is not a whitespace or comment.
public getNextMeaningfulToken ( integer $index ) : integer | null
$index integer token index
return integer | null

getNextNonWhitespace() public method

This method is shorthand for getNonWhitespaceSibling method.
public getNextNonWhitespace ( integer $index, null | string $whitespaces = null ) : integer | null
$index integer token index
$whitespaces null | string whitespaces characters for Token::isWhitespace
return integer | null

getNextTokenOfKind() public method

This method is shorthand for getTokenOfKindSibling method.
public getNextTokenOfKind ( integer $index, array $tokens = [], boolean $caseSensitive = true ) : integer | null
$index integer token index
$tokens array possible tokens
$caseSensitive boolean perform a case sensitive comparison
return integer | null

getNonEmptySibling() public method

Get index for closest sibling token which is not empty.
public getNonEmptySibling ( integer $index, integer $direction ) : integer | null
$index integer token index
$direction integer direction for looking, +1 or -1
return integer | null

getNonWhitespaceSibling() public method

Get index for closest sibling token which is non whitespace.
public getNonWhitespaceSibling ( integer $index, integer $direction, null | string $whitespaces = null ) : integer | null
$index integer token index
$direction integer direction for looking, +1 or -1
$whitespaces null | string whitespaces characters for Token::isWhitespace
return integer | null

getPrevMeaningfulToken() public method

Get index for closest previous token that is not a whitespace or comment.
public getPrevMeaningfulToken ( integer $index ) : integer | null
$index integer token index
return integer | null

getPrevNonWhitespace() public method

This method is shorthand for getNonWhitespaceSibling method.
public getPrevNonWhitespace ( integer $index, null | string $whitespaces = null ) : integer | null
$index integer token index
$whitespaces null | string whitespaces characters for Token::isWhitespace
return integer | null

getPrevTokenOfKind() public method

This method is shorthand for getTokenOfKindSibling method.
public getPrevTokenOfKind ( integer $index, array $tokens = [], boolean $caseSensitive = true ) : integer | null
$index integer token index
$tokens array possible tokens
$caseSensitive boolean perform a case sensitive comparison
return integer | null

getTokenNotOfKindSibling() public method

Get index for closest sibling token not of given kind.
public getTokenNotOfKindSibling ( integer $index, integer $direction, array $tokens = [] ) : integer | null
$index integer token index
$direction integer direction for looking, +1 or -1
$tokens array possible tokens
return integer | null

getTokenOfKindSibling() public method

Get index for closest sibling token of given kind.
public getTokenOfKindSibling ( integer $index, integer $direction, array $tokens = [], boolean $caseSensitive = true ) : integer | null
$index integer token index
$direction integer direction for looking, +1 or -1
$tokens array possible tokens
$caseSensitive boolean perform a case sensitive comparison
return integer | null

insertAt() public method

Insert instances of Token inside collection.
public insertAt ( integer $index, Tokens | PhpCsFixer\Tokenizer\Token[] | PhpCsFixer\Tokenizer\Token $items )
$index integer start inserting index
$items Tokens | PhpCsFixer\Tokenizer\Token[] | PhpCsFixer\Tokenizer\Token instances of Token to insert

isAllTokenKindsFound() public method

Check if all token kinds given as argument are found.
public isAllTokenKindsFound ( array $tokenKinds ) : boolean
$tokenKinds array
return boolean

isAnyTokenKindsFound() public method

Check if any token kind given as argument is found.
public isAnyTokenKindsFound ( array $tokenKinds ) : boolean
$tokenKinds array
return boolean

isChanged() public method

Check if collection was change: collection itself (like insert new tokens) or any of collection's elements.
public isChanged ( ) : boolean
return boolean

isMonolithicPhp() public method

Checks that the code is pure PHP code, in a single code block, starting with an open tag.
public isMonolithicPhp ( ) : boolean
return boolean

isPartialCodeMultiline() public method

Check if partial code is multiline.
public isPartialCodeMultiline ( integer $start, integer $end ) : boolean
$start integer start index
$end integer end index
return boolean

isTokenKindFound() public method

Check if token kind given as argument is found.
public isTokenKindFound ( integer | string $tokenKind ) : boolean
$tokenKind integer | string
return boolean

offsetSet() public method

Warning! $newval must not be typehinted to be compatible with ArrayAccess::offsetSet method.
public offsetSet ( integer $index, PhpCsFixer\Tokenizer\Token $newval )
$index integer
$newval PhpCsFixer\Tokenizer\Token

offsetUnset() public method

Unset collection item.
public offsetUnset ( integer $index )
$index integer

overrideAt() public method

Override token at given index and register it.
public overrideAt ( integer $index, PhpCsFixer\Tokenizer\Token | array | string $token )
$index integer
$token PhpCsFixer\Tokenizer\Token | array | string token prototype

overrideRange() public method

Override tokens at given range.
public overrideRange ( integer $indexStart, integer $indexEnd, Tokens | PhpCsFixer\Tokenizer\Token[] $items )
$indexStart integer start overriding index
$indexEnd integer end overriding index
$items Tokens | PhpCsFixer\Tokenizer\Token[] tokens to insert

removeLeadingWhitespace() public method

Removes all the leading whitespace.
public removeLeadingWhitespace ( integer $index, null | string $whitespaces = null )
$index integer
$whitespaces null | string optional whitespaces characters for Token::isWhitespace

removeTrailingWhitespace() public method

Removes all the trailing whitespace.
public removeTrailingWhitespace ( integer $index, null | string $whitespaces = null )
$index integer
$whitespaces null | string optional whitespaces characters for Token::isWhitespace

setCode() public method

Set code. Clear all current content and replace it by new Token items generated from code directly.
public setCode ( string $code )
$code string PHP code

setSize() public method

Set new size of collection.
public setSize ( integer $size )
$size integer

toJson() public method

public toJson ( )