\Horde\RefactorTokens

Custom ArrayIterator implementation representing the tokens.

$iterator = new Horde\Refactor\Iterator( token_get_all('<?php echo "foo"; ?>') ); $iterator->find(T_ECHO); $iterator->find(T_CONSTANT_ENCAPSED_STRING); $iterator->find( T_CONSTANT_ENCAPSED_STRING, '"foo"', array('backwards' => true, 'allowed' => T_WHITESPACE) );

Summary

Methods
Properties
Constants
previous()
find()
findConstruct()
findFunctionTokens()
skipWhitespace()
findMatchingBracket()
matchesWhitespace()
matches()
matchesAll()
slice()
splice()
insert()
__toString()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Methods

previous()

previous() 

Moves the iterator to previous entry.

find()

find(string|integer|\Horde\Refactor\Regexp  $token, string  $term = null, array  $opts = array()) : boolean

Seeks to a certain string or token.

Parameters

string|integer|\Horde\Refactor\Regexp $token

Token to search for. One of the T_* token constants or a plain string. See token_get_all().

string $term

If $token is a token that can have individual content, the term to search for in the content.

array $opts

Additional options:

  • backward (boolean): Search backwards?
  • allowed (array): Tokens allowed to skip over.

Returns

boolean —

Whether the token was found.

findConstruct()

findConstruct(integer  $token, string  $name) : boolean

Finds a certain construct, e.g. a function of a certain name.

Parameters

integer $token

Token to search for. One of the T_* token constants.

string $name

Name to search for.

Throws

\Horde\Refactor\Exception\UnexpectedToken

Returns

boolean —

Whether the token was found.

findFunctionTokens()

findFunctionTokens() : array

Returns the token positions of the current function.

This is the complete function definition, including any leading doc comments and whitespace, and up to the closing curly brace and trailing newline.

Returns

array —

Tuple with the start and end position of the function tokens.

skipWhitespace()

skipWhitespace(boolean  $backward = false) 

Traverses the tokens to the next non-whitespace token.

If the current token is not a whitespace token, it's skipped too.

Parameters

boolean $backward

Traverse backwards through the tokens.

findMatchingBracket()

findMatchingBracket(string  $bracket = null, boolean  $backward = false) 

Finds matching brackets.

Supported are parentheses, curly braces, angle brackets, and square brackets, both finding the closing to the opening and vice versa.

Parameters

string $bracket

The bracket to match against. Defaults to the current token.

boolean $backward

Traverse backwards through the tokens.

Throws

\Horde\Refactor\Exception\UnexpectedToken

if the current token is not a bracket and $bracket wasn't specified.

\Horde\Refactor\Exception\NotFound

if the matching backet wasn't found.

matchesWhitespace()

matchesWhitespace() : boolean

Returns whether the current string token only contains whitespace.

Returns

boolean —

True if the current token is whitespace only.

matches()

matches(string|integer|\Horde\Refactor\Regexp  $token, string|\Horde\Refactor\Regexp  $term = null) : boolean

Returns whether the current token matches a certain string or token.

Parameters

string|integer|\Horde\Refactor\Regexp $token

Token to search for. One of the T_* token constants or a plain string. See token_get_all().

string|\Horde\Refactor\Regexp $term

If $token is a token that can have individual content, the term to match the content against.

Returns

boolean —

Whether the token matched.

matchesAll()

matchesAll(array  $tokens) : boolean

Returns whether the tokens starting from the current position match a certain sequence of tokens.

Parameters

array $tokens

A sequence of function arguments to matches().

Returns

boolean —

True if all tokens from the sequence match.

slice()

slice(integer  $offset, integer  $length = null) : self

Returns a slice of the tokens.

Parameters

integer $offset

Offset where to start the slice. See array_slice() for details on this parameter.

integer $length

The number of tokens to return. See array_slice() for details on this parameter.

Returns

self —

The requested slice.

splice()

splice(integer  $offset, integer  $length = null, array  $replacement = array()) : self

Removes a portion of the tokens and replaces it with something else.

Parameters

integer $offset

Offset where to start removing tokens. See array_splice() for details on this parameter.

integer $length

The number of tokens to remove. If null, removes everything to the end. Use 0 if you don't want to remove anything. See array_splice() for details on this parameter.

array $replacement

Replace the removed tokens with these tokens.

Returns

self —

Contrary to array_splice(), this method doesn't return the extracted tokens, but a new iterator with the tokens replaced.

insert()

insert(array  $tokens) : self

Inserts tokens at the current position.

Parameters

array $tokens

The tokens to insert.

Returns

self —

A new iterator with the tokens inserted.

__toString()

__toString() : string

Returns the file code in its current state.

Returns

string —

The file code.