TokensWithObservedTransformers
extends Tokens
in package
Collection of code 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.
Table of Contents
- BLOCK_TYPE_ARRAY_INDEX_CURLY_BRACE = 7
- BLOCK_TYPE_ARRAY_SQUARE_BRACE = 4
- BLOCK_TYPE_ATTRIBUTE = 11
- BLOCK_TYPE_BRACE_CLASS_INSTANTIATION = 10
- BLOCK_TYPE_CURLY_BRACE = 2
- BLOCK_TYPE_DESTRUCTURING_SQUARE_BRACE = 9
- BLOCK_TYPE_DYNAMIC_PROP_BRACE = 5
- BLOCK_TYPE_DYNAMIC_VAR_BRACE = 6
- BLOCK_TYPE_GROUP_IMPORT_BRACE = 8
- BLOCK_TYPE_INDEX_SQUARE_BRACE = 3
- BLOCK_TYPE_PARENTHESIS_BRACE = 1
- $currentTransformer : null|string
- $observedModificationsPerTransformer : mixed
- $blockEndCache : array<int, int>
- Cache of block ends. Any change in collection will invalidate it.
- $blockStartCache : array<int, int>
- Cache of block starts. Any change in collection will invalidate it.
- $cache : array<string|int, mixed>
- Static class cache.
- $changed : bool
- Flag is collection was changed.
- $codeHash : string
- crc32 hash of code string.
- $foundTokenKinds : array<int|string, int>
- Set of found token kinds.
- $isLegacyMode : bool
- __clone() : mixed
- Clone tokens collection.
- clearAt() : mixed
- clearCache() : mixed
- Clear cache - one position or all of them.
- clearChanged() : mixed
- Clear internal flag if collection was changed and flag for all collection's items.
- clearEmptyTokens() : mixed
- Clear empty tokens.
- clearRange() : mixed
- Clear tokens in the given range.
- clearTokenAndMergeSurroundingWhitespace() : mixed
- countTokenKind() : int
- detectBlockType() : null|array<string|int, mixed>
- Detect type of block.
- ensureWhitespaceAtIndex() : bool
- Ensure that on given index is a whitespace with given kind.
- findBlockEnd() : int
- findBlockStart() : int
- findGivenKind() : array<string|int, mixed>
- findSequence() : null|array<int, Token>
- Find a sequence of meaningful tokens and returns the array of their locations.
- fromArray() : Tokens
- Create token collection from array.
- fromCode() : Tokens
- Create token collection directly from code.
- generateCode() : string
- generatePartialCode() : string
- Generate code from tokens between given indexes.
- getBlockEdgeDefinitions() : array<string|int, mixed>
- getCodeHash() : string
- Get hash of code.
- getMeaningfulTokenSibling() : null|int
- Get index for closest sibling token that is not a whitespace, comment or attribute.
- getNextMeaningfulToken() : null|int
- Get index for closest next token that is not a whitespace or comment.
- getNextNonWhitespace() : null|int
- Get index for closest next token which is non whitespace.
- getNextTokenOfKind() : null|int
- Get index for closest next token of given kind.
- getNonEmptySibling() : null|int
- Get index for closest sibling token which is not empty.
- getNonWhitespaceSibling() : null|int
- Get index for closest sibling token which is non whitespace.
- getPrevMeaningfulToken() : null|int
- Get index for closest previous token that is not a whitespace or comment.
- getPrevNonWhitespace() : null|int
- Get index for closest previous token which is non whitespace.
- getPrevTokenOfKind() : null|int
- Get index for closest previous token of given kind.
- getTokenNotOfKindSibling() : null|int
- Get index for closest sibling token not of given kind.
- getTokenNotOfKindsSibling() : null|int
- Get index for closest sibling token not of given kind.
- getTokenOfKindSibling() : null|int
- Get index for closest sibling token of given kind.
- hasAlternativeSyntax() : bool
- insertAt() : mixed
- Insert instances of Token inside collection.
- isAllTokenKindsFound() : bool
- Check if all token kinds given as argument are found.
- isAnyTokenKindsFound() : bool
- Check if any token kind given as argument is found.
- isChanged() : bool
- Check if collection was change: collection itself (like insert new tokens) or any of collection's elements.
- isEmptyAt() : bool
- isMonolithicPhp() : bool
- Checks for monolithic PHP code.
- isPartialCodeMultiline() : bool
- isTokenKindFound() : bool
- Check if token kind given as argument is found.
- offsetSet() : mixed
- Set collection item.
- offsetUnset() : mixed
- Unset collection item.
- overrideAt() : mixed
- Override token at given index and register it.
- overrideRange() : mixed
- Override tokens at given range.
- removeLeadingWhitespace() : mixed
- removeTrailingWhitespace() : mixed
- setCode() : mixed
- Set code. Clear all current content and replace it by new Token items generated from code directly.
- setSize() : bool
- Set new size of collection.
- toJson() : mixed
- calculateCodeHash() : string
- Calculate hash for code.
- changeCodeHash() : mixed
- Change code hash.
- extractTokenKind() : int|string
- findOppositeBlockEdge() : int
- getCache() : Tokens
- Get cache value for given key.
- getTokenNotOfKind() : null|int
- hasCache() : bool
- Check if given key exists in cache.
- registerFoundToken() : mixed
- Register token as found.
- removeWhitespaceSafely() : mixed
- setCache() : mixed
- unregisterFoundToken() : mixed
- Register token as found.
- warnPhp8SplFixerArrayChange() : mixed
Constants
BLOCK_TYPE_ARRAY_INDEX_CURLY_BRACE
public
mixed
BLOCK_TYPE_ARRAY_INDEX_CURLY_BRACE
= 7
BLOCK_TYPE_ARRAY_SQUARE_BRACE
public
mixed
BLOCK_TYPE_ARRAY_SQUARE_BRACE
= 4
BLOCK_TYPE_ATTRIBUTE
public
mixed
BLOCK_TYPE_ATTRIBUTE
= 11
BLOCK_TYPE_BRACE_CLASS_INSTANTIATION
public
mixed
BLOCK_TYPE_BRACE_CLASS_INSTANTIATION
= 10
BLOCK_TYPE_CURLY_BRACE
public
mixed
BLOCK_TYPE_CURLY_BRACE
= 2
BLOCK_TYPE_DESTRUCTURING_SQUARE_BRACE
public
mixed
BLOCK_TYPE_DESTRUCTURING_SQUARE_BRACE
= 9
BLOCK_TYPE_DYNAMIC_PROP_BRACE
public
mixed
BLOCK_TYPE_DYNAMIC_PROP_BRACE
= 5
BLOCK_TYPE_DYNAMIC_VAR_BRACE
public
mixed
BLOCK_TYPE_DYNAMIC_VAR_BRACE
= 6
BLOCK_TYPE_GROUP_IMPORT_BRACE
public
mixed
BLOCK_TYPE_GROUP_IMPORT_BRACE
= 8
BLOCK_TYPE_INDEX_SQUARE_BRACE
public
mixed
BLOCK_TYPE_INDEX_SQUARE_BRACE
= 3
BLOCK_TYPE_PARENTHESIS_BRACE
public
mixed
BLOCK_TYPE_PARENTHESIS_BRACE
= 1
Properties
$currentTransformer
public
null|string
$currentTransformer
$observedModificationsPerTransformer
public
mixed
$observedModificationsPerTransformer
= []
$blockEndCache
Cache of block ends. Any change in collection will invalidate it.
private
array<int, int>
$blockEndCache
= []
$blockStartCache
Cache of block starts. Any change in collection will invalidate it.
private
array<int, int>
$blockStartCache
= []
$cache
Static class cache.
private
static array<string|int, mixed>
$cache
= []
$changed
Flag is collection was changed.
private
bool
$changed
= false
It doesn't know about change of collection's items. To check it run isChanged
method.
$codeHash
crc32 hash of code string.
private
string
$codeHash
$foundTokenKinds
Set of found token kinds.
private
array<int|string, int>
$foundTokenKinds
= []
When the token kind is present in this set it means that given token kind was ever seen inside the collection (but may not be part of it any longer). The key is token kind and the value is always true.
$isLegacyMode
private
static bool
$isLegacyMode
= false
Tags
Methods
__clone()
Clone tokens collection.
public
__clone() : mixed
Return values
mixed —clearAt()
public
clearAt(mixed $index) : mixed
Parameters
- $index : mixed
Return values
mixed —clearCache()
Clear cache - one position or all of them.
public
static clearCache([null|string $key = null ]) : mixed
Parameters
- $key : null|string = null
-
position to clear, when null clear all
Return values
mixed —clearChanged()
Clear internal flag if collection was changed and flag for all collection's items.
public
clearChanged() : mixed
Return values
mixed —clearEmptyTokens()
Clear empty tokens.
public
clearEmptyTokens() : mixed
Empty tokens can occur e.g. after calling clear on item of collection.
Return values
mixed —clearRange()
Clear tokens in the given range.
public
clearRange(int $indexStart, int $indexEnd) : mixed
Parameters
- $indexStart : int
- $indexEnd : int
Return values
mixed —clearTokenAndMergeSurroundingWhitespace()
public
clearTokenAndMergeSurroundingWhitespace(int $index) : mixed
Parameters
- $index : int
Return values
mixed —countTokenKind()
public
countTokenKind(int|string $tokenKind) : int
Parameters
- $tokenKind : int|string
Return values
int —detectBlockType()
Detect type of block.
public
static detectBlockType(Token $token) : null|array<string|int, mixed>
Parameters
- $token : Token
-
token
Return values
null|array<string|int, mixed> —array with 'type' and 'isStart' keys or null if not found
ensureWhitespaceAtIndex()
Ensure that on given index is a whitespace with given kind.
public
ensureWhitespaceAtIndex(int $index, int $indexOffset, string $whitespace) : bool
If there is a whitespace then it's content will be modified. If not - the new Token will be added.
Parameters
- $index : int
-
index
- $indexOffset : int
-
index offset for Token insertion
- $whitespace : string
-
whitespace to set
Return values
bool —if new Token was added
findBlockEnd()
public
findBlockEnd(int $type, int $searchIndex[, bool $findEnd = true ]) : int
Parameters
- $type : int
-
type of block, one of BLOCK_TYPE_*
- $searchIndex : int
-
index of opening brace
- $findEnd : bool = true
-
if method should find block's end, default true, otherwise method find block's start
Return values
int —index of closing brace
findBlockStart()
public
findBlockStart(int $type, int $searchIndex) : int
Parameters
- $type : int
-
type of block, one of BLOCK_TYPE_*
- $searchIndex : int
-
index of closing brace
Return values
int —index of opening brace
findGivenKind()
public
findGivenKind(array<string|int, mixed>|int $possibleKind, int $start[, null|int $end = null ]) : array<string|int, mixed>
Parameters
- $possibleKind : array<string|int, mixed>|int
-
kind or array of kind
- $start : int
-
optional offset
- $end : null|int = null
-
optional limit
Return values
array<string|int, mixed> —array of tokens of given kinds or assoc array of arrays
findSequence()
Find a sequence of meaningful tokens and returns the array of their locations.
public
findSequence(array<string|int, mixed> $sequence, int $start[, int $end = null ][, array<int, bool>|bool $caseSensitive = true ]) : null|array<int, Token>
Parameters
- $sequence : array<string|int, mixed>
-
an array of tokens (kinds) (same format used by getNextTokenOfKind)
- $start : int
-
start index, defaulting to the start of the file
- $end : int = null
-
end index, defaulting to the end of the file
- $caseSensitive : array<int, bool>|bool = true
-
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 values
null|array<int, Token> —an array containing the tokens matching the sequence elements, indexed by their position
fromArray()
Create token collection from array.
public
static fromArray(array<string|int, Token> $array[, bool $saveIndexes = null ]) : Tokens
Parameters
- $array : array<string|int, Token>
-
the array to import
- $saveIndexes : bool = null
-
save the numeric indexes used in the original array, default is yes
Return values
Tokens —fromCode()
Create token collection directly from code.
public
static fromCode(string $code) : Tokens
Parameters
- $code : string
-
PHP code
Return values
Tokens —generateCode()
public
generateCode() : string
Return values
string —generatePartialCode()
Generate code from tokens between given indexes.
public
generatePartialCode(int $start, int $end) : string
Parameters
- $start : int
-
start index
- $end : int
-
end index
Return values
string —getBlockEdgeDefinitions()
public
static getBlockEdgeDefinitions() : array<string|int, mixed>
Return values
array<string|int, mixed> —getCodeHash()
Get hash of code.
public
getCodeHash() : string
Return values
string —getMeaningfulTokenSibling()
Get index for closest sibling token that is not a whitespace, comment or attribute.
public
getMeaningfulTokenSibling(int $index, int $direction) : null|int
Parameters
- $index : int
-
token index
- $direction : int
-
direction for looking, +1 or -1
Return values
null|int —getNextMeaningfulToken()
Get index for closest next token that is not a whitespace or comment.
public
getNextMeaningfulToken(int $index) : null|int
Parameters
- $index : int
-
token index
Return values
null|int —getNextNonWhitespace()
Get index for closest next token which is non whitespace.
public
getNextNonWhitespace(int $index[, null|string $whitespaces = null ]) : null|int
This method is shorthand for getNonWhitespaceSibling method.
Parameters
- $index : int
-
token index
- $whitespaces : null|string = null
-
whitespaces characters for Token::isWhitespace
Return values
null|int —getNextTokenOfKind()
Get index for closest next token of given kind.
public
getNextTokenOfKind(int $index[, array<string|int, mixed> $tokens = [] ][, bool $caseSensitive = true ]) : null|int
This method is shorthand for getTokenOfKindSibling method.
Parameters
- $index : int
-
token index
- $tokens : array<string|int, mixed> = []
-
possible tokens
- $caseSensitive : bool = true
-
perform a case sensitive comparison
Return values
null|int —getNonEmptySibling()
Get index for closest sibling token which is not empty.
public
getNonEmptySibling(int $index, int $direction) : null|int
Parameters
- $index : int
-
token index
- $direction : int
-
direction for looking, +1 or -1
Return values
null|int —getNonWhitespaceSibling()
Get index for closest sibling token which is non whitespace.
public
getNonWhitespaceSibling(int $index, int $direction[, null|string $whitespaces = null ]) : null|int
Parameters
- $index : int
-
token index
- $direction : int
-
direction for looking, +1 or -1
- $whitespaces : null|string = null
-
whitespaces characters for Token::isWhitespace
Return values
null|int —getPrevMeaningfulToken()
Get index for closest previous token that is not a whitespace or comment.
public
getPrevMeaningfulToken(int $index) : null|int
Parameters
- $index : int
-
token index
Return values
null|int —getPrevNonWhitespace()
Get index for closest previous token which is non whitespace.
public
getPrevNonWhitespace(int $index[, null|string $whitespaces = null ]) : null|int
This method is shorthand for getNonWhitespaceSibling method.
Parameters
- $index : int
-
token index
- $whitespaces : null|string = null
-
whitespaces characters for Token::isWhitespace
Return values
null|int —getPrevTokenOfKind()
Get index for closest previous token of given kind.
public
getPrevTokenOfKind(int $index[, array<string|int, mixed> $tokens = [] ][, bool $caseSensitive = true ]) : null|int
This method is shorthand for getTokenOfKindSibling method.
Parameters
- $index : int
-
token index
- $tokens : array<string|int, mixed> = []
-
possible tokens
- $caseSensitive : bool = true
-
perform a case sensitive comparison
Return values
null|int —getTokenNotOfKindSibling()
Get index for closest sibling token not of given kind.
public
getTokenNotOfKindSibling(int $index, int $direction[, array<string|int, mixed> $tokens = [] ]) : null|int
Parameters
- $index : int
-
token index
- $direction : int
-
direction for looking, +1 or -1
- $tokens : array<string|int, mixed> = []
-
possible tokens
Return values
null|int —getTokenNotOfKindsSibling()
Get index for closest sibling token not of given kind.
public
getTokenNotOfKindsSibling(int $index, int $direction[, array<string|int, mixed> $kinds = [] ]) : null|int
Parameters
- $index : int
-
token index
- $direction : int
-
direction for looking, +1 or -1
- $kinds : array<string|int, mixed> = []
-
possible tokens kinds
Return values
null|int —getTokenOfKindSibling()
Get index for closest sibling token of given kind.
public
getTokenOfKindSibling(int $index, int $direction[, array<string|int, mixed> $tokens = [] ][, bool $caseSensitive = true ]) : null|int
Parameters
- $index : int
-
token index
- $direction : int
-
direction for looking, +1 or -1
- $tokens : array<string|int, mixed> = []
-
possible tokens
- $caseSensitive : bool = true
-
perform a case sensitive comparison
Return values
null|int —hasAlternativeSyntax()
public
hasAlternativeSyntax() : bool
Return values
bool —insertAt()
Insert instances of Token inside collection.
public
insertAt(int $index, array<string|int, Token>|Token|Tokens $items) : mixed
Parameters
- $index : int
-
start inserting index
- $items : array<string|int, Token>|Token|Tokens
-
instances of Token to insert
Return values
mixed —isAllTokenKindsFound()
Check if all token kinds given as argument are found.
public
isAllTokenKindsFound(array<string|int, mixed> $tokenKinds) : bool
Parameters
- $tokenKinds : array<string|int, mixed>
Return values
bool —isAnyTokenKindsFound()
Check if any token kind given as argument is found.
public
isAnyTokenKindsFound(array<string|int, mixed> $tokenKinds) : bool
Parameters
- $tokenKinds : array<string|int, mixed>
Return values
bool —isChanged()
Check if collection was change: collection itself (like insert new tokens) or any of collection's elements.
public
isChanged() : bool
Return values
bool —isEmptyAt()
public
isEmptyAt(int $index) : bool
Parameters
- $index : int
Return values
bool —isMonolithicPhp()
Checks for monolithic PHP code.
public
isMonolithicPhp() : bool
Checks that the code is pure PHP code, in a single code block, starting with an open tag.
Return values
bool —isPartialCodeMultiline()
public
isPartialCodeMultiline(int $start, int $end) : bool
Parameters
- $start : int
-
start index
- $end : int
-
end index
Return values
bool —isTokenKindFound()
Check if token kind given as argument is found.
public
isTokenKindFound(int|string $tokenKind) : bool
Parameters
- $tokenKind : int|string
Return values
bool —offsetSet()
Set collection item.
public
offsetSet(mixed $index, mixed $newval) : mixed
Warning! $newval
must not be typehinted to be compatible with ArrayAccess::offsetSet
method.
Parameters
- $index : mixed
- $newval : mixed
Return values
mixed —offsetUnset()
Unset collection item.
public
offsetUnset(int $index) : mixed
Parameters
- $index : int
Return values
mixed —overrideAt()
Override token at given index and register it.
public
overrideAt(int $index, array<string|int, mixed>|string|Token $token) : mixed
Parameters
- $index : int
- $token : array<string|int, mixed>|string|Token
-
token prototype
Tags
Return values
mixed —overrideRange()
Override tokens at given range.
public
overrideRange(int $indexStart, int $indexEnd, array<string|int, Token>|Tokens $items) : mixed
Parameters
- $indexStart : int
-
start overriding index
- $indexEnd : int
-
end overriding index
- $items : array<string|int, Token>|Tokens
-
tokens to insert
Return values
mixed —removeLeadingWhitespace()
public
removeLeadingWhitespace(int $index[, null|string $whitespaces = null ]) : mixed
Parameters
- $index : int
- $whitespaces : null|string = null
-
optional whitespaces characters for Token::isWhitespace
Return values
mixed —removeTrailingWhitespace()
public
removeTrailingWhitespace(int $index[, null|string $whitespaces = null ]) : mixed
Parameters
- $index : int
- $whitespaces : null|string = null
-
optional whitespaces characters for Token::isWhitespace
Return values
mixed —setCode()
Set code. Clear all current content and replace it by new Token items generated from code directly.
public
setCode(string $code) : mixed
Parameters
- $code : string
-
PHP code
Return values
mixed —setSize()
Set new size of collection.
public
setSize(int $size) : bool
Parameters
- $size : int
Return values
bool —toJson()
public
toJson() : mixed
Return values
mixed —calculateCodeHash()
Calculate hash for code.
private
static calculateCodeHash(string $code) : string
Parameters
- $code : string
Return values
string —changeCodeHash()
Change code hash.
private
changeCodeHash(string $codeHash) : mixed
Remove old cache and set new one.
Parameters
- $codeHash : string
-
new code hash
Return values
mixed —extractTokenKind()
private
extractTokenKind(array<string|int, mixed>|string|Token $token) : int|string
Parameters
- $token : array<string|int, mixed>|string|Token
-
token prototype
Return values
int|string —findOppositeBlockEdge()
private
findOppositeBlockEdge(int $type, int $searchIndex, bool $findEnd) : int
Parameters
- $type : int
-
type of block, one of BLOCK_TYPE_*
- $searchIndex : int
-
index of starting brace
- $findEnd : bool
-
if method should find block's end or start
Return values
int —index of opposite brace
getCache()
Get cache value for given key.
private
static getCache(string $key) : Tokens
Parameters
- $key : string
-
item key
Return values
Tokens —getTokenNotOfKind()
private
getTokenNotOfKind(int $index, int $direction, callable $filter) : null|int
Parameters
- $index : int
-
token index
- $direction : int
-
direction for looking, +1 or -1
- $filter : callable
Return values
null|int —hasCache()
Check if given key exists in cache.
private
static hasCache(string $key) : bool
Parameters
- $key : string
-
item key
Return values
bool —registerFoundToken()
Register token as found.
private
registerFoundToken(array<string|int, mixed>|string|Token $token) : mixed
Parameters
- $token : array<string|int, mixed>|string|Token
-
token prototype
Return values
mixed —removeWhitespaceSafely()
private
removeWhitespaceSafely(mixed $index, mixed $direction[, mixed $whitespaces = null ]) : mixed
Parameters
- $index : mixed
- $direction : mixed
- $whitespaces : mixed = null
Return values
mixed —setCache()
private
static setCache(string $key, Tokens $value) : mixed
Parameters
- $key : string
-
item key
- $value : Tokens
-
item value
Return values
mixed —unregisterFoundToken()
Register token as found.
private
unregisterFoundToken(array<string|int, mixed>|string|Token $token) : mixed
Parameters
- $token : array<string|int, mixed>|string|Token
-
token prototype
Return values
mixed —warnPhp8SplFixerArrayChange()
private
warnPhp8SplFixerArrayChange(mixed $method) : mixed
Parameters
- $method : mixed