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
Afficher le fichier Open project: friendsofphp/php-cs-fixer Class Usage Examples

Méthodes publiques

Méthode 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

Méthode 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 méthode

Clone tokens collection.
public __clone ( )

clearCache() public static méthode

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 méthode

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

clearEmptyTokens() public méthode

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

clearRange() public méthode

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

clearTokenAndMergeSurroundingWhitespace() public méthode

Clear token and merge surrounding whitespace tokens.

detectBlockType() public static méthode

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

ensureWhitespaceAtIndex() public méthode

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
Résultat boolean if new Token was added

findBlockEnd() public méthode

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
Résultat integer index of closing brace

findGivenKind() public méthode

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
Résultat array array of tokens of given kinds or assoc array of arrays

findSequence() public méthode

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
Résultat array | null an array containing the tokens matching the sequence elements, indexed by their position

fromArray() public static méthode

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
Résultat Tokens

fromCode() public static méthode

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

generateCode() public méthode

Generate code from tokens.
public generateCode ( ) : string
Résultat string

generatePartialCode() public méthode

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

getBlockEdgeDefinitions() public static méthode

Return block edge definitions.
public static getBlockEdgeDefinitions ( ) : array
Résultat array

getCodeHash() public méthode

Get hash of code.
public getCodeHash ( ) : string
Résultat string

getMeaningfulTokenSibling() public méthode

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
Résultat integer | null

getNextMeaningfulToken() public méthode

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

getNextNonWhitespace() public méthode

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
Résultat integer | null

getNextTokenOfKind() public méthode

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
Résultat integer | null

getNonEmptySibling() public méthode

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
Résultat integer | null

getNonWhitespaceSibling() public méthode

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
Résultat integer | null

getPrevMeaningfulToken() public méthode

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

getPrevNonWhitespace() public méthode

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
Résultat integer | null

getPrevTokenOfKind() public méthode

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
Résultat integer | null

getTokenNotOfKindSibling() public méthode

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
Résultat integer | null

getTokenOfKindSibling() public méthode

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
Résultat integer | null

insertAt() public méthode

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 méthode

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

isAnyTokenKindsFound() public méthode

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

isChanged() public méthode

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

isMonolithicPhp() public méthode

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

isPartialCodeMultiline() public méthode

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

isTokenKindFound() public méthode

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

offsetSet() public méthode

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 méthode

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

overrideAt() public méthode

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 méthode

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 méthode

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 méthode

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 méthode

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 méthode

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

toJson() public méthode

public toJson ( )