class ControlStructuresOptimizer extends AbstractOptimizer

Optimize the control structures of a script

Removes brackets in control structures wherever possible. Prevents the generation of EXT_STMT opcodes where they're not strictly required.

Properties

protected int $cnt from  AbstractOptimizer
protected int $i from  AbstractOptimizer
protected bool $changed from  AbstractOptimizer
protected array $tokens from  AbstractOptimizer
protected int $braces
protected array $context

Methods

string
optimize(string $php)

Optimize the control structures of a script

void
optimizeTokens()

Optimize control structures in stored tokens

void
reset(string $php)

Reset the internal state of this optimizer

string
serialize()

Serialize the tokens back to source

void
skipToString(string $str)

Move the internal cursor until it reaches given string

void
skipWhitespace()

Skip all whitespace

void
unindentBlock(int $start, int $end)

Remove one tab of indentation off a range of PHP tokens

bool
blockEndsWithIf()

Test whether current block ends with an if or elseif control structure

bool
isControlStructure()

Test whether the token at current index is a control structure

bool
isFollowedByElse()

Test whether current block is followed by an elseif/else structure

bool
mustPreserveBraces()

Test whether braces must be preserved in current context

void
processControlStructure()

Process the control structure starting at current index

void
processEndOfBlock()

Process the block ending at current index

void
removeBracesInCurrentContext()

Remove the braces surrounding current context

void
skipCondition()

Skip the condition of a control structure

Details

in AbstractOptimizer at line 41
string optimize(string $php)

Optimize the control structures of a script

Removes brackets in control structures wherever possible. Prevents the generation of EXT_STMT opcodes where they're not strictly required.

Parameters

string $php

Original code

Return Value

string

Optimized code

at line 94
protected void optimizeTokens()

Optimize control structures in stored tokens

Return Value

void

at line 269
protected void reset(string $php)

Reset the internal state of this optimizer

Parameters

string $php

PHP source

Return Value

void

in AbstractOptimizer at line 84
protected string serialize()

Serialize the tokens back to source

Return Value

string

in AbstractOptimizer at line 104
protected void skipToString(string $str)

Move the internal cursor until it reaches given string

Parameters

string $str

String to reach

Return Value

void

in AbstractOptimizer at line 114
protected void skipWhitespace()

Skip all whitespace

Return Value

void

in AbstractOptimizer at line 126
protected void unindentBlock(int $start, int $end)

Remove one tab of indentation off a range of PHP tokens

Parameters

int $start

Index of the first token to unindent

int $end

Index of the last token to unindent

Return Value

void

at line 33
protected bool blockEndsWithIf()

Test whether current block ends with an if or elseif control structure

Return Value

bool

at line 43
protected bool isControlStructure()

Test whether the token at current index is a control structure

Return Value

bool

at line 57
protected bool isFollowedByElse()

Test whether current block is followed by an elseif/else structure

Return Value

bool

at line 81
protected bool mustPreserveBraces()

Test whether braces must be preserved in current context

Return Value

bool

at line 127
protected void processControlStructure()

Process the control structure starting at current index

Return Value

void

at line 191
protected void processEndOfBlock()

Process the block ending at current index

Return Value

void

at line 209
protected void removeBracesInCurrentContext()

Remove the braces surrounding current context

Return Value

void

at line 289
protected void skipCondition()

Skip the condition of a control structure

Return Value

void