\Drupflare\Anachron\Php SyntaxRewriter

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.

Summary

Methods
Properties
Constants
__construct
rewrite
No public properties found
No public constants found
No protected methods found
No protected properties found
No protected constants found
startsOffset
isOffsetHead
followsArrow
closingBrace
opens
isExpression
requestTimeSites
untypedSites
typeBefore
opensParameters
isReferenceOrSpread
isRequestTime
previousNonSpace
requestTime
untypedParameters
No private constants found

Properties

$requestTime

$requestTime : ?string

Type

string|null

$untypedParameters

$untypedParameters : array

Type

array<string|int, mixed>

Methods

__construct()

__construct(string|null  $requestTime = null, array  $untypedParameters = []) : mixed

Constructs a rewriter.

Parameters

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 array type is dropped. Code written for an array that a host now passes as an object, one that reads like an array, fails on the type alone.

Returns

mixed —

rewrite()

rewrite(string  $code, array  $refused = []) : string

Rewrites every curly offset, and the request time constant when configured, in a source.

Parameters

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.

Returns

string —

The rewritten source, or the same string when there is nothing to rewrite.

startsOffset()

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}.

Parameters

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.

Returns

bool —

True when the brace is an offset opener.

isOffsetHead()

isOffsetHead(array  $tokens, int  $index) : bool

Checks whether a token can take a curly offset.

Parameters

array $tokens

The token list.

int $index

Index of the token.

Returns

bool —

True for a variable, a bracket, or a property name after an arrow.

followsArrow()

followsArrow(array  $tokens, int  $index) : bool

Checks whether the token at an index follows an object or nullsafe arrow.

Parameters

array $tokens

The token list.

int $index

Index of the token.

Returns

bool —

True when the nearest non-space token before it is an arrow.

closingBrace()

closingBrace(array  $tokens, int  $open) : int|null

Finds the brace that closes the one at an index.

Parameters

array $tokens

The token list.

int $open

Index of the opening brace.

Returns

int|null —

Index of the matching closing brace, or null when the braces never balance.

opens()

opens(string|array{int, string, int}  $token) : bool

Checks whether a token opens a brace level, including string interpolation.

Parameters

string|array{int, string, int} $token

The token.

Returns

bool —

True for a plain brace, a string brace, or a dollar brace.

isExpression()

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.

Parameters

array $tokens

The token list.

int $start

Index of the first token after the opening brace.

int $end

Index of the closing brace.

Returns

bool —

True when the range holds at least one non-space token and reads as an expression.

requestTimeSites()

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.

Parameters

array $tokens

The token list.

array $refused

Receives the refused uses.

Returns

array —

The expression to write at each token index.

untypedSites()

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.

Parameters

array $tokens

The token list.

Returns

array —

An empty string at each type token index.

typeBefore()

typeBefore(array  $tokens, int  $variable) : array<int,int>

Finds the type tokens of the parameter whose variable sits at an index.

Parameters

array $tokens

The token list.

int $variable

Index of the variable.

Returns

array —

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()

opensParameters(array  $tokens, int  $open) : bool

Checks whether the parenthesis at an index opens a parameter list.

Parameters

array $tokens

The token list.

int $open

Index of the parenthesis.

Returns

bool —

True after function, fn, or function name, with an optional reference marker.

isReferenceOrSpread()

isReferenceOrSpread(string|array{int, string, int}  $token) : bool

Checks whether a token is a reference marker or a spread.

Parameters

string|array{int, string, int} $token

The token.

Returns

bool —

True for an ampersand in either form PHP 8.1 lexes, or an ellipsis.

isRequestTime()

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.

Parameters

array $tokens

The token list.

int $index

Index of the token.

Returns

bool —

True for the bare or fully qualified name used as a value.

previousNonSpace()

previousNonSpace(array  $tokens, int  $index) : int

Finds the nearest token before an index that is not whitespace.

Parameters

array $tokens

The token list.

int $index

Index to search before.

Returns

int —

The index found, or -1 when only whitespace precedes it.