class Optimizer

This class optimizes the code produced by the PHP renderer. It is not meant to be used on general purpose code

Properties

BranchOutputOptimizer $branchOutputOptimizer
protected int $cnt
protected int $i
int $maxLoops
protected array $tokens

Methods

__construct()

Constructor

string
optimize(string $php)

Optimize the code generated by the PHP renderer generator

bool
isBetweenHtmlspecialcharCalls()

Test whether current token is between two htmlspecialchars() calls

bool
isHtmlspecialcharSafeVar()

Test whether current token is at the beginning of an htmlspecialchars()-safe var

bool
isOutputAssignment()

Test whether the cursor is at the beginning of an output assignment

bool
isPrecededByOutputVar()

Test whether the cursor is immediately after the output variable

bool
mergeConcatenatedHtmlSpecialChars()

Merge concatenated htmlspecialchars() calls together

bool
mergeConcatenatedStrings()

Merge concatenated strings together

void
optimizeOutConcatEqual()

Optimize T_CONCAT_EQUAL assignments in an array of PHP tokens

void
optimizeConcatenations()

Optimize concatenations in an array of PHP tokens

void
optimizeHtmlspecialchars()

Optimize htmlspecialchars() calls

bool
removeHtmlspecialcharsSafeVar()

Remove htmlspecialchars() calls on variables that are known to be safe

bool
replaceHtmlspecialcharsLiteral()

Precompute the result of a htmlspecialchars() call on a string literal

bool
skipPast(array|string $token)

Move the cursor past given token

bool
skipTo(array|string $token)

Move the cursor until it reaches given token

Details

at line 44
__construct()

Constructor

at line 55
string optimize(string $php)

Optimize the code generated by the PHP renderer generator

Parameters

string $php

Original code

Return Value

string

Optimized code

at line 115
protected bool isBetweenHtmlspecialcharCalls()

Test whether current token is between two htmlspecialchars() calls

Return Value

bool

at line 131
protected bool isHtmlspecialcharSafeVar()

Test whether current token is at the beginning of an htmlspecialchars()-safe var

Tests whether current var is either $node->localName or $node->nodeName

Return Value

bool

at line 147
protected bool isOutputAssignment()

Test whether the cursor is at the beginning of an output assignment

Return Value

bool

at line 160
protected bool isPrecededByOutputVar()

Test whether the cursor is immediately after the output variable

Return Value

bool

at line 174
protected bool mergeConcatenatedHtmlSpecialChars()

Merge concatenated htmlspecialchars() calls together

Must be called when the cursor is at the concatenation operator

Return Value

bool

Whether calls were merged

at line 236
protected bool mergeConcatenatedStrings()

Merge concatenated strings together

Must be called when the cursor is at the concatenation operator

Return Value

bool

Whether strings were merged

at line 266
protected void optimizeOutConcatEqual()

Optimize T_CONCAT_EQUAL assignments in an array of PHP tokens

Will only optimize $this->out.= assignments

Return Value

void

at line 308
protected void optimizeConcatenations()

Optimize concatenations in an array of PHP tokens

  • Will precompute the result of the concatenation of constant strings
  • Will replace the concatenation of two compatible htmlspecialchars() calls with one call to htmlspecialchars() on the concatenation of their first arguments

Return Value

void

at line 326
protected void optimizeHtmlspecialchars()

Optimize htmlspecialchars() calls

  • The result of htmlspecialchars() on literals is precomputed
  • By default, the generator escapes all values, including variables that cannot contain special characters such as $node->localName. This pass removes those calls

Return Value

void

at line 347
protected bool removeHtmlspecialcharsSafeVar()

Remove htmlspecialchars() calls on variables that are known to be safe

Must be called when the cursor is at the first argument of the call

Return Value

bool

Whether the call was removed

at line 374
protected bool replaceHtmlspecialcharsLiteral()

Precompute the result of a htmlspecialchars() call on a string literal

Must be called when the cursor is at the first argument of the call

Return Value

bool

Whether the call was replaced

at line 410
protected bool skipPast(array|string $token)

Move the cursor past given token

Parameters

array|string $token

Target token

Return Value

bool

Whether a matching token was found and the cursor is within bounds

at line 421
protected bool skipTo(array|string $token)

Move the cursor until it reaches given token

Parameters

array|string $token

Target token

Return Value

bool

Whether a matching token was found