Editor PHP 2.4.0

Tokens extends SplFixedArray
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.

Tags
author

Dariusz Rumiński dariusz.ruminski@gmail.com

extends
final

Table of Contents

Constants

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_DISJUNCTIVE_NORMAL_FORM_TYPE_PARENTHESIS  = 12
BLOCK_TYPE_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE  = 13
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

Methods

__clone()  : mixed
Clone tokens collection.
clearAt()  : void
clearCache()  : void
Clear cache - one position or all of them.
clearChanged()  : void
Clear internal flag if collection was changed and flag for all collection's items.
clearEmptyTokens()  : void
Clear empty tokens.
clearRange()  : void
Clear tokens in the given range.
clearTokenAndMergeSurroundingWhitespace()  : void
countTokenKind()  : int
detectBlockType()  : null|array{type: self::BLOCK_TYPE_*, isStart: bool}
Detect type of block.
ensureWhitespaceAtIndex()  : bool
Ensure that on given index is a whitespace with given kind.
findBlockEnd()  : int
findBlockStart()  : int
findGivenKind()  : array<int, array<int, Token>>|array<int, Token>
findSequence()  : null|array<int, Token>
Find a sequence of meaningful tokens and returns the array of their locations.
fromArray()  : self
Create token collection from array.
fromCode()  : self
Create token collection directly from code.
generateCode()  : string
generatePartialCode()  : string
Generate code from tokens between given indices.
getBlockEdgeDefinitions()  : array<string|int, mixed>
getCodeHash()  : string
Get hash of code.
getMeaningfulTokenSibling()  : int|null
Get index for closest sibling token that is not a whitespace, comment or attribute.
getNextMeaningfulToken()  : int|null
Get index for closest next token that is not a whitespace or comment.
getNextNonWhitespace()  : int|null
Get index for closest next token which is non whitespace.
getNextTokenOfKind()  : int|null
Get index for closest next token of given kind.
getNonEmptySibling()  : int|null
Get index for closest sibling token which is not empty.
getNonWhitespaceSibling()  : int|null
Get index for closest sibling token which is non whitespace.
getPrevMeaningfulToken()  : int|null
Get index for closest previous token that is not a whitespace or comment.
getPrevNonWhitespace()  : int|null
Get index for closest previous token which is non whitespace.
getPrevTokenOfKind()  : int|null
Get index for closest previous token of given kind.
getTokenNotOfKindSibling()  : int|null
Get index for closest sibling token not of given kind.
getTokenNotOfKindsSibling()  : int|null
Get index for closest sibling token not of given kind.
getTokenOfKindSibling()  : int|null
Get index for closest sibling token of given kind.
hasAlternativeSyntax()  : bool
insertAt()  : void
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.
offsetGet()  : Token
offsetSet()  : void
Set collection item.
offsetUnset()  : void
Unset collection item.
overrideRange()  : void
Override tokens at given range.
removeLeadingWhitespace()  : void
removeTrailingWhitespace()  : void
setCode()  : void
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()  : string

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_DISJUNCTIVE_NORMAL_FORM_TYPE_PARENTHESIS

public mixed BLOCK_TYPE_DISJUNCTIVE_NORMAL_FORM_TYPE_PARENTHESIS = 12

BLOCK_TYPE_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE

public mixed BLOCK_TYPE_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE = 13

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

Methods

__clone()

Clone tokens collection.

public __clone() : mixed

clearAt()

public clearAt(int $index) : void
Parameters
$index : int

clearCache()

Clear cache - one position or all of them.

public static clearCache([null|string $key = null ]) : void
Parameters
$key : null|string = null

position to clear, when null clear all

clearChanged()

Clear internal flag if collection was changed and flag for all collection's items.

public clearChanged() : void

clearEmptyTokens()

Clear empty tokens.

public clearEmptyTokens() : void

Empty tokens can occur e.g. after calling clear on item of collection.

clearRange()

Clear tokens in the given range.

public clearRange(int $indexStart, int $indexEnd) : void
Parameters
$indexStart : int
$indexEnd : int

clearTokenAndMergeSurroundingWhitespace()

public clearTokenAndMergeSurroundingWhitespace(int $index) : void
Parameters
$index : int

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{type: self::BLOCK_TYPE_*, isStart: bool}
Parameters
$token : Token

token

Return values
null|array{type: self::BLOCK_TYPE_*, isStart: bool}

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(self::BLOCK_TYPE_* $type, int $searchIndex) : int
Parameters
$type : self::BLOCK_TYPE_*

type of block

$searchIndex : int

index of opening brace

Return values
int

index of closing brace

findBlockStart()

public findBlockStart(self::BLOCK_TYPE_* $type, int $searchIndex) : int
Parameters
$type : self::BLOCK_TYPE_*

type of block

$searchIndex : int

index of closing brace

Return values
int

index of opening brace

findGivenKind()

public findGivenKind(int|array<int, int> $possibleKind[, int $start = 0 ][, null|int $end = null ]) : array<int, array<int, Token>>|array<int, Token>
Parameters
$possibleKind : int|array<int, int>

kind or array of kinds

$start : int = 0

optional offset

$end : null|int = null

optional limit

Return values
array<int, array<int, Token>>|array<int, Token>

findSequence()

Find a sequence of meaningful tokens and returns the array of their locations.

public findSequence(array<int, array{0: int, 1?: string}|string|Token$sequence[, int $start = 0 ][, null|int $end = null ][, array<string|int, bool>|bool $caseSensitive = true ]) : null|array<int, Token>
Parameters
$sequence : array<int, array{0: int, 1?: string}|string|Token>

an array of token (kinds)

$start : int = 0

start index, defaulting to the start of the file

$end : null|int = null

end index, defaulting to the end of the file

$caseSensitive : array<string|int, bool>|bool = true

global case sensitiveness or a list of booleans, whose keys should match the ones used in $sequence. 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|null $saveIndices = null ]) : self
Parameters
$array : array<string|int, Token>

the array to import

$saveIndices : bool|null = null

save the numeric indices used in the original array, default is yes

Return values
self

fromCode()

Create token collection directly from code.

public static fromCode(string $code) : self
Parameters
$code : string

PHP code

Return values
self

generateCode()

public generateCode() : string
Return values
string

generatePartialCode()

Generate code from tokens between given indices.

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, -1|1 $direction) : int|null
Parameters
$index : int

token index

$direction : -1|1
Return values
int|null

getNextMeaningfulToken()

Get index for closest next token that is not a whitespace or comment.

public getNextMeaningfulToken(int $index) : int|null
Parameters
$index : int

token index

Return values
int|null

getNextNonWhitespace()

Get index for closest next token which is non whitespace.

public getNextNonWhitespace(int $index[, null|string $whitespaces = null ]) : int|null

This method is shorthand for getNonWhitespaceSibling method.

Parameters
$index : int

token index

$whitespaces : null|string = null

whitespaces characters for Token::isWhitespace

Return values
int|null

getNextTokenOfKind()

Get index for closest next token of given kind.

public getNextTokenOfKind(int $index[, array<int, array{: int}|string|Token$tokens = [] ][, bool $caseSensitive = true ]) : int|null

This method is shorthand for getTokenOfKindSibling method.

Parameters
$index : int

token index

$tokens : array<int, array{: int}|string|Token> = []

possible tokens

$caseSensitive : bool = true

perform a case sensitive comparison

Return values
int|null

getNonEmptySibling()

Get index for closest sibling token which is not empty.

public getNonEmptySibling(int $index, -1|1 $direction) : int|null
Parameters
$index : int

token index

$direction : -1|1
Return values
int|null

getNonWhitespaceSibling()

Get index for closest sibling token which is non whitespace.

public getNonWhitespaceSibling(int $index, -1|1 $direction[, null|string $whitespaces = null ]) : int|null
Parameters
$index : int

token index

$direction : -1|1
$whitespaces : null|string = null

whitespaces characters for Token::isWhitespace

Return values
int|null

getPrevMeaningfulToken()

Get index for closest previous token that is not a whitespace or comment.

public getPrevMeaningfulToken(int $index) : int|null
Parameters
$index : int

token index

Return values
int|null

getPrevNonWhitespace()

Get index for closest previous token which is non whitespace.

public getPrevNonWhitespace(int $index[, null|string $whitespaces = null ]) : int|null

This method is shorthand for getNonWhitespaceSibling method.

Parameters
$index : int

token index

$whitespaces : null|string = null

whitespaces characters for Token::isWhitespace

Return values
int|null

getPrevTokenOfKind()

Get index for closest previous token of given kind.

public getPrevTokenOfKind(int $index[, array<int, array{: int}|string|Token$tokens = [] ][, bool $caseSensitive = true ]) : int|null

This method is shorthand for getTokenOfKindSibling method.

Parameters
$index : int

token index

$tokens : array<int, array{: int}|string|Token> = []

possible tokens

$caseSensitive : bool = true

perform a case sensitive comparison

Return values
int|null

getTokenNotOfKindSibling()

Get index for closest sibling token not of given kind.

public getTokenNotOfKindSibling(int $index, -1|1 $direction[, array<int, array{: int}|string|Token$tokens = [] ]) : int|null
Parameters
$index : int

token index

$direction : -1|1
$tokens : array<int, array{: int}|string|Token> = []

possible tokens

Return values
int|null

getTokenNotOfKindsSibling()

Get index for closest sibling token not of given kind.

public getTokenNotOfKindsSibling(int $index, -1|1 $direction[, array<int, int> $kinds = [] ]) : int|null
Parameters
$index : int

token index

$direction : -1|1
$kinds : array<int, int> = []

possible tokens kinds

Return values
int|null

getTokenOfKindSibling()

Get index for closest sibling token of given kind.

public getTokenOfKindSibling(int $index, -1|1 $direction[, array<int, array{: int}|string|Token$tokens = [] ][, bool $caseSensitive = true ]) : int|null
Parameters
$index : int

token index

$direction : -1|1
$tokens : array<int, array{: int}|string|Token> = []

possible tokens

$caseSensitive : bool = true

perform a case sensitive comparison

Return values
int|null

hasAlternativeSyntax()

public hasAlternativeSyntax() : bool
Return values
bool

insertAt()

Insert instances of Token inside collection.

public insertAt(int $index, array<int, Token>|Token|Tokens $items) : void
Parameters
$index : int

start inserting index

$items : array<int, Token>|Token|Tokens

instances of Token to insert

isAllTokenKindsFound()

Check if all token kinds given as argument are found.

public isAllTokenKindsFound(array<int, int|string> $tokenKinds) : bool
Parameters
$tokenKinds : array<int, int|string>
Return values
bool

isAnyTokenKindsFound()

Check if any token kind given as argument is found.

public isAnyTokenKindsFound(array<int, int|string> $tokenKinds) : bool
Parameters
$tokenKinds : array<int, int|string>
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

offsetGet()

public offsetGet(mixed $offset) : Token
Parameters
$offset : mixed
Return values
Token

offsetSet()

Set collection item.

public offsetSet(int $index, Token $newval) : void

Warning! $newval must not be typehinted to be compatible with ArrayAccess::offsetSet method.

Parameters
$index : int
$newval : Token

offsetUnset()

Unset collection item.

public offsetUnset(int $index) : void
Parameters
$index : int

overrideRange()

Override tokens at given range.

public overrideRange(int $indexStart, int $indexEnd, array<int, Token>|Tokens $items) : void
Parameters
$indexStart : int

start overriding index

$indexEnd : int

end overriding index

$items : array<int, Token>|Tokens

tokens to insert

removeLeadingWhitespace()

public removeLeadingWhitespace(int $index[, null|string $whitespaces = null ]) : void
Parameters
$index : int
$whitespaces : null|string = null

optional whitespaces characters for Token::isWhitespace

removeTrailingWhitespace()

public removeTrailingWhitespace(int $index[, null|string $whitespaces = null ]) : void
Parameters
$index : int
$whitespaces : null|string = null

optional whitespaces characters for Token::isWhitespace

setCode()

Set code. Clear all current content and replace it by new Token items generated from code directly.

public setCode(string $code) : void
Parameters
$code : string

PHP code

setSize()

Set new size of collection.

public setSize(int $size) : bool
Parameters
$size : int
Return values
bool

toJson()

public toJson() : string
Return values
string

        
On this page

Search results