$requestTime
$requestTime : ?string
Rewrites the curly string offset syntax PHP 8.0 removed ($str{0}) to square brackets.
Each replacement stays on its own line, so the output keeps the input's line count and every line number in a stack trace.
__construct(string|null $requestTime = null, array$untypedParameters = []) : mixed
Constructs a rewriter.
| string|null | $requestTime | An expression that answers the current request time. When set, every use of the REQUEST_TIME constant becomes that expression, because a constant freezes at the first request in a process that serves many. Null leaves the constant alone. |
| array |
$untypedParameters | Parameter names, without the dollar sign, whose |
rewrite(string $code, array$refused = []) : string
Rewrites every curly offset, and the request time constant when configured, in a source.
| string | $code | PHP source. |
| array |
$refused | Receives each use of the constant that cannot become an expression, because PHP evaluates its position at compile time. Those uses stay as written. |
The rewritten source, or the same string when there is nothing to rewrite.
startsOffset(array$tokens, int $open, array $replace) : bool
Checks whether the brace at an index follows something that can take an offset.
Whitespace between the two is allowed, as PHP 7 allowed $s {0}.
| array |
$tokens | The token list. |
| int | $open | Index of the brace. |
| array |
$replace | Replacements decided so far, which make a closing brace count as a bracket. |
True when the brace is an offset opener.
isExpression(array$tokens, int $start, int $end) : bool
Checks whether the tokens between two braces form an offset expression.
A block brace inside the range (a property hook body, for one) fails the check: a semicolon at the top level, or a brace after anything other than a variable, a bracket, a closing brace or a property name.
| array |
$tokens | The token list. |
| int | $start | Index of the first token after the opening brace. |
| int | $end | Index of the closing brace. |
True when the range holds at least one non-space token and reads as an expression.
requestTimeSites(array$tokens, array $refused) : array<int,string>
Finds the uses of the request time constant and the expression each becomes.
A use in a parameter default, a constant declaration, or a class body outside a method is refused: PHP evaluates those at compile time and a call there is a fatal error.
| array |
$tokens | The token list. |
| array |
$refused | Receives the refused uses. |
The expression to write at each token index.
untypedSites(array$tokens) : array<int,string>
Finds the type tokens to drop from the parameters named in the constructor.
Only a type that holds array goes (array, ?array, array|null); any other type on the
parameter stays. Whitespace is kept, so each line keeps its number.
| array |
$tokens | The token list. |
An empty string at each type token index.
typeBefore(array$tokens, int $variable) : array<int,int>
Finds the type tokens of the parameter whose variable sits at an index.
| array |
$tokens | The token list. |
| int | $variable | Index of the variable. |
Indexes of the type tokens between the previous comma or parenthesis and the variable, leaving out whitespace, a reference marker and a spread. Empty when something that is not a type, such as a visibility keyword, sits there.
opensParameters(array$tokens, int $open) : bool
Checks whether the parenthesis at an index opens a parameter list.
| array |
$tokens | The token list. |
| int | $open | Index of the parenthesis. |
True after function, fn, or function name, with an optional reference marker.
isRequestTime(array$tokens, int $index) : bool
Checks whether the token at an index is a use of the request time constant.
A property, a class constant, a function call, a declaration and a named argument share the name and are not uses.
| array |
$tokens | The token list. |
| int | $index | Index of the token. |
True for the bare or fully qualified name used as a value.