class BranchOutputOptimizer

Properties

protected int $cnt
protected int $i
protected array $tokens

Methods

string
optimize(array $tokens)

Optimize the code used to output content

string[]
captureOutput()

Capture the expressions used in any number of consecutive output statements

string
captureOutputExpression()

Capture an expression used in output at current index

string
captureStructure()

Capture the source of a control structure from its keyword to its opening brace

bool
isBranchToken()

Test whether the token at current index is an if/elseif/else token

array
mergeIfBranches(array $branches)

Merge the branches of an if/elseif/else block

array
mergeOutput(array $left, array $right)

Merge two consecutive series of consecutive output expressions together

string[]
optimizeBranchesHead(array $branches)

Optimize the "head" part of a series of branches in-place

string[]
optimizeBranchesOutput(array $branches, string $which)

Optimize the output of given branches

string[]
optimizeBranchesTail(array $branches)

Optimize the "tail" part of a series of branches in-place

array
parseBranch()

Parse the if, elseif or else branch starting at current index

array
parseIfBlock()

Parse the if block (including elseif/else branches) starting at current index

string
serializeBranch(array $branch)

Serialize a recorded branch back to PHP

string
serializeIfBlock(array $block)

Serialize a series of recorded branch back to PHP

string
serializeOutput(array $expressions)

Serialize a series of output expressions

string
serializeToken(array|string $token)

Serialize a token back to PHP

bool
skipOutputAssignment()

Attempt to move past output assignment at current index

Details

at line 36
string optimize(array $tokens)

Optimize the code used to output content

This method will go through the array of tokens, identify if/elseif/else blocks that contain identical code at the beginning or the end and move the common code outside of the block

Parameters

array $tokens

Array of tokens from token_get_all()

Return Value

string

Optimized code

at line 69
protected string[] captureOutput()

Capture the expressions used in any number of consecutive output statements

Starts looking at current index. Ends at the first token that's not part of an output statement

Return Value

string[]

at line 91
protected string captureOutputExpression()

Capture an expression used in output at current index

Ends on "." or ";"

Return Value

string

at line 128
protected string captureStructure()

Capture the source of a control structure from its keyword to its opening brace

Ends after the brace, but the brace itself is not returned

Return Value

string

at line 148
protected bool isBranchToken()

Test whether the token at current index is an if/elseif/else token

Return Value

bool

at line 165
protected array mergeIfBranches(array $branches)

Merge the branches of an if/elseif/else block

Returns an array that contains the following:

  • before: array of PHP expressions to be output before the block
  • source: PHP code for the if block
  • after: array of PHP expressions to be output after the block

Parameters

array $branches

Return Value

array

at line 200
protected array mergeOutput(array $left, array $right)

Merge two consecutive series of consecutive output expressions together

Parameters

array $left

First series

array $right

Second series

Return Value

array

Merged series

at line 231
protected string[] optimizeBranchesHead(array $branches)

Optimize the "head" part of a series of branches in-place

Parameters

array $branches

Array of branches, modified in-place

Return Value

string[]

PHP expressions removed from the "head" part of the branches

at line 259
protected string[] optimizeBranchesOutput(array $branches, string $which)

Optimize the output of given branches

Parameters

array $branches

Array of branches

string $which

Which end to optimize ("head" or "tail")

Return Value

string[]

PHP expressions removed from the given part of the branches

at line 290
protected string[] optimizeBranchesTail(array $branches)

Optimize the "tail" part of a series of branches in-place

Parameters

array $branches

Array of branches, modified in-place

Return Value

string[]

PHP expressions removed from the "tail" part of the branches

at line 302
protected array parseBranch()

Parse the if, elseif or else branch starting at current index

Ends at the last }

Return Value

array

Branch's data ("structure", "head", "body", "tail")

at line 372
protected array parseIfBlock()

Parse the if block (including elseif/else branches) starting at current index

Return Value

array

at line 393
protected string serializeBranch(array $branch)

Serialize a recorded branch back to PHP

Parameters

array $branch

Return Value

string

at line 413
protected string serializeIfBlock(array $block)

Serialize a series of recorded branch back to PHP

Parameters

array $block

Return Value

string

at line 424
protected string serializeOutput(array $expressions)

Serialize a series of output expressions

Parameters

array $expressions

Array of PHP expressions

Return Value

string

PHP code used to append given expressions to the output

at line 440
protected string serializeToken(array|string $token)

Serialize a token back to PHP

Parameters

array|string $token

Token from token_get_all()

Return Value

string

PHP code

at line 450
protected bool skipOutputAssignment()

Attempt to move past output assignment at current index

Return Value

bool

Whether if an output assignment was skipped