Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
d4fac42
refactor(compat-scanner): drop the walk budgets and size caps
mrrobot47 Nov 28, 2026
32aed79
feat(compat-scanner): scan all of app/ and judge every WordPress by i…
mrrobot47 Nov 28, 2026
a25bebe
feat(compat-scanner): lint for PHP 8 parse errors and count every fil…
mrrobot47 Nov 28, 2026
6465a9d
ci: assert that every php image loads redis
mrrobot47 Nov 28, 2026
7b563ed
fix(compat-scanner): never leave out the release that holds the scan …
mrrobot47 Nov 28, 2026
14046cc
test(compat-scanner): name the v3.11.1-style runs after what they do
mrrobot47 Nov 28, 2026
dbe6a78
fix(compat-scanner): read a double-quoted $wp_version too
mrrobot47 Nov 28, 2026
e3f9118
fix(compat-scanner): leave the run-again hint of a timeout to EE
mrrobot47 Nov 28, 2026
a5fd8e6
fix(compat-scanner): read a WordPress version over 64 characters as u…
mrrobot47 Nov 28, 2026
ce42b7c
refactor(compat-scanner): read the contract env in its own function
mrrobot47 Nov 28, 2026
7c1ba08
test(compat-scanner): pin the report key order
mrrobot47 Nov 28, 2026
3cecfd8
fix(compat-scanner): read .user.ini and version.php lines with a leng…
mrrobot47 Nov 28, 2026
b34b311
fix(compat-scanner): read a .user.ini the way PHP does
mrrobot47 Nov 28, 2026
fb44320
fix(compat-scanner): read .user.ini files in core and old releases
mrrobot47 Nov 28, 2026
2084908
fix(compat-scanner): check a wp-config.php linked into a mount
mrrobot47 Nov 28, 2026
49127be
fix(compat-scanner): keep checking a php site whose htdocs is missing…
mrrobot47 Nov 28, 2026
0245d7e
fix(compat-scanner): lint only files that start as PHP
mrrobot47 Nov 28, 2026
e24f620
fix(compat-scanner): judge redeclared PHP functions by the target ver…
mrrobot47 Nov 28, 2026
8258722
fix(compat-scanner): leave out only release siblings that look like a…
mrrobot47 Nov 28, 2026
083658f
fix(compat-scanner): raise the entrypoint memory limit to 2G
mrrobot47 Nov 28, 2026
242107a
fix(compat-scanner): lint for fatal errors only
mrrobot47 Nov 28, 2026
864e0fc
fix(compat-scanner): leave .user.ini files in old releases unread
mrrobot47 Nov 29, 2026
e87f7e6
fix(compat-scanner): walk the code beside a wp-content linked out of …
mrrobot47 Nov 29, 2026
1949a88
fix(compat-scanner): keep linting past a function PHP adds after the …
mrrobot47 Nov 29, 2026
c30c8bf
fix(compat-scanner): count only a function_exists() check on the name…
mrrobot47 Nov 29, 2026
6250542
fix(compat-scanner): leave out an old release only when it holds the …
mrrobot47 Nov 29, 2026
5f2e9bb
fix(compat-scanner): read every .user.ini, and report a core one that…
mrrobot47 Nov 29, 2026
1c8e19d
fix(compat-scanner): bound the reads of data-only candidates, the pha…
mrrobot47 Nov 29, 2026
8ae627d
test(compat-scanner): keep the harness exact on odd input
mrrobot47 Nov 29, 2026
39606fd
fix(compat-scanner): set the entrypoint memory limit to 1536M
mrrobot47 Nov 29, 2026
8fee2fd
fix(compat-scanner): lint templates with text before their PHP tag
mrrobot47 Nov 29, 2026
97fe322
feat(compat-scanner): report a class PHP has natively in the target d…
mrrobot47 Nov 29, 2026
c7b250b
fix(compat-scanner): count a PHP_VERSION_ID bound as a guard for nati…
mrrobot47 Nov 29, 2026
bb62736
fix(compat-scanner): keep the scanner and a child below the 2g contai…
mrrobot47 Nov 29, 2026
a663f03
fix(compat-scanner): time PHPCS and lint runs in the scanner itself
mrrobot47 Nov 29, 2026
7df0f2d
fix(compat-scanner): check code that the walk dropped or left out by …
mrrobot47 Nov 29, 2026
37de1da
fix(compat-scanner): give "can't confirm" for lint failures that may …
mrrobot47 Nov 29, 2026
43736ba
feat(compat-scanner): follow the site's short_open_tag setting
mrrobot47 Nov 29, 2026
08f6c14
fix(compat-scanner): accept only guards that keep a native's declarat…
mrrobot47 Nov 29, 2026
2cb69ca
feat(compat-scanner): know the names PHP 8.0 and its date extension a…
mrrobot47 Nov 29, 2026
c4bf1a6
fix(compat-scanner): no errors for polyfill files PHP never runs on t…
mrrobot47 Nov 29, 2026
29f771b
style(compat-scanner): put each comment on one line
mrrobot47 Nov 29, 2026
d2d3bd9
test(compat-scanner): pull the PHP 8.5.0 image before the guard table…
mrrobot47 Nov 29, 2026
4364daa
fix(compat-scanner): an XML file isn't PHP code where short tags are on
mrrobot47 Nov 29, 2026
bacd6fb
perf(compat-scanner): read a directory once for the bootstrap check
mrrobot47 Nov 29, 2026
6bf18cb
fix(compat-scanner): find every string naming a file in the bootstrap…
mrrobot47 Nov 29, 2026
28af03c
fix(compat-scanner): lint with each target's PHP and honour guards ar…
mrrobot47 Nov 29, 2026
69a1e04
fix(compat-scanner): a sniffed file with no PHP code isn't a php_tag …
mrrobot47 Nov 29, 2026
29e1aa4
fix(compat-scanner): count a lint run per file in the estimate below …
mrrobot47 Nov 29, 2026
a458768
test(compat-scanner): compare the estimate's lint time as a number
mrrobot47 Nov 29, 2026
6caebde
fix(compat-scanner): match namespaced new classes the site declares i…
mrrobot47 Nov 29, 2026
abab5df
test(compat-scanner): cover a version_compare() guard with named argu…
mrrobot47 Nov 29, 2026
57f272f
refactor(compat-scanner): share the guard operand test
mrrobot47 Nov 29, 2026
a817b57
fix(compat-scanner): count a guard only inside the code's own functio…
mrrobot47 Nov 29, 2026
577a96e
fix(compat-scanner): keep deprecation warnings under an exists-check
mrrobot47 Nov 29, 2026
0af602a
fix(compat-scanner): match a new class's guard on its full name
mrrobot47 Nov 29, 2026
ce9f838
fix(compat-scanner): a site's trait or interface doesn't stand in for…
mrrobot47 Nov 29, 2026
3a546a9
fix(compat-scanner): check short tags on where any .user.ini down to …
mrrobot47 Nov 29, 2026
b75cb47
style(compat-scanner): put the phar-name docblock back on its function
mrrobot47 Nov 29, 2026
3dc4ad0
test(compat-scanner): read the use-guard table once
mrrobot47 Nov 29, 2026
24d4105
test(compat-scanner): pin the shapes that aren't guards
mrrobot47 Nov 29, 2026
ae61aba
fix(compat-scanner): let defined() guard the constant it names
mrrobot47 Nov 29, 2026
33bfa7c
fix(compat-scanner): skip a constant default of a function declared o…
mrrobot47 Nov 29, 2026
acb699a
fix(compat-scanner): leave out package files their package loads only…
mrrobot47 Nov 29, 2026
b4b5e92
fix(compat-scanner): let the doc root's .user.ini Off hold over a sho…
mrrobot47 Nov 29, 2026
8a800ce
fix(compat-scanner): keep the kind of a new interface finding
mrrobot47 Nov 29, 2026
3f5c97d
fix(compat-scanner): check every file with short tags on when any .us…
mrrobot47 Nov 29, 2026
38733e6
fix(compat-scanner): read .user.ini directive names case-sensitively
mrrobot47 Nov 29, 2026
b323539
fix(compat-scanner): match .user.ini directories to the doc root by r…
mrrobot47 Nov 29, 2026
3e9976f
fix(compat-scanner): ignore the scan path's .user.ini Off when the do…
mrrobot47 Nov 29, 2026
caabc54
fix(compat-scanner): check package files Composer's autoloader loads
mrrobot47 Nov 29, 2026
17e5cc1
fix(compat-scanner): apply a linked .user.ini to every directory that…
mrrobot47 Nov 29, 2026
40219f3
fix(compat-scanner): flag a .user.ini include_path into a left-out ol…
mrrobot47 Nov 29, 2026
aafa13a
test(compat-scanner): pin the short-tag hiding case as a known limit
mrrobot47 Nov 29, 2026
a1d95e8
fix(compat-scanner): never let a .user.ini turn short tags off
mrrobot47 Nov 29, 2026
c43805f
fix(compat-scanner): count a .user.ini On reached through a link to a…
mrrobot47 Nov 29, 2026
4d9cda5
fix(compat-scanner): ignore the include_path of a .user.ini above the…
mrrobot47 Nov 29, 2026
0ef9e66
fix(compat-scanner): flag a link to releases/ or an ancestor of it
mrrobot47 Nov 29, 2026
901d26f
perf(compat-scanner): skip the doc root's link names when no .user.in…
mrrobot47 Nov 29, 2026
b093f76
test(compat-scanner): run each truth row from its own checked fixture…
mrrobot47 Nov 29, 2026
1bd0312
fix(compat-scanner): list a .user.ini linked into a left-out old rele…
mrrobot47 Nov 29, 2026
3a2d678
fix(compat-scanner): take no version_compare() literal with a part ov…
mrrobot47 Nov 29, 2026
ad8773c
fix(compat-scanner): split guard conditions only on their own && and ||
mrrobot47 Nov 29, 2026
1269628
fix(compat-scanner): drop a declared-class list too long for the repo…
mrrobot47 Nov 29, 2026
fe95702
fix(compat-scanner): judge native declaration version guards against …
mrrobot47 Nov 29, 2026
e32335f
test(compat-scanner): check each source the mcrypt use-guard row reports
mrrobot47 Nov 29, 2026
0ee9bec
test(compat-scanner): match templates through every vendor dir in the…
mrrobot47 Nov 29, 2026
9cf8384
test(compat-scanner): name the component the oversized declared-class…
mrrobot47 Nov 29, 2026
3157534
test(compat-scanner): pull each guard table's PHP image before its tr…
mrrobot47 Nov 29, 2026
47b8873
test(compat-scanner): match each fatal guard row against the error it…
mrrobot47 Nov 29, 2026
b4caa86
fix(compat-scanner): find a Composer vendor dir right inside a package
mrrobot47 Nov 29, 2026
abac3b3
fix(compat-scanner): keep a package's lint errors when its autoload l…
mrrobot47 Nov 29, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/test_and_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ jobs:
- name: Check PHP - ${{ matrix.php }} modules and startup
run: .github/scripts/check-php-image.sh easyengine/php${{ steps.img-tag.outputs.tag }}:test php/${{ matrix.php }}/expected-modules.txt

# site-command ignores nginx-helper's bundled Predis errors because every EE php image loads redis.
- name: Check PHP - ${{ matrix.php }} loads redis
run: docker run --rm --pull=never --entrypoint php easyengine/php${{ steps.img-tag.outputs.tag }}:test -m | grep -x redis

- name: Build and push PHP - ${{ matrix.php }} image
uses: docker/build-push-action@v7
if: github.event_name != 'pull_request'
Expand Down
30 changes: 29 additions & 1 deletion compat-scanner/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,43 @@ RUN set -eux; \
php /tmp/select.php /tmp/wordpress/wp-includes /opt/compat-scanner/calibration; \
rm -rf /tmp/wp.tar.gz /tmp/wordpress /tmp/select.php

# Each target's own PHP for `php -l`: only it parses, reserves names and declares functions as the target does.
# Older Alpine bases bring their own libraries; the alpine3.24 ones use the final image's.
FROM php:8.0.30-cli-alpine3.16 AS php80
FROM php:8.1.34-cli-alpine3.22 AS php81
FROM php:8.2.36-cli-alpine3.24 AS php82
FROM php:8.3.37-cli-alpine3.24 AS php83
FROM php:8.5.13-cli-alpine3.24 AS php85
FROM php:8.6.0-cli-alpine3.24 AS php86
# Their libraries beside them, found through the binary's RPATH.
FROM php80 AS libs80
RUN apk add --no-cache patchelf && mkdir -p /out/lib && cp /usr/local/bin/php /out/ \
&& ldd /usr/local/bin/php | awk '$3 ~ /^\// && $1 !~ /musl/ { print $3 }' | xargs cp -L -t /out/lib \
&& patchelf --set-rpath /opt/php/8.0/lib /out/php
FROM php81 AS libs81
RUN apk add --no-cache patchelf && mkdir -p /out/lib && cp /usr/local/bin/php /out/ \
&& ldd /usr/local/bin/php | awk '$3 ~ /^\// && $1 !~ /musl/ { print $3 }' | xargs cp -L -t /out/lib \
&& patchelf --set-rpath /opt/php/8.1/lib /out/php

FROM php:8.4.28-cli-alpine3.24

LABEL org.label-schema.schema-version="1.0.0"
LABEL org.label-schema.vendor="EasyEngine"
LABEL org.label-schema.name="compat-scanner"
# What site-command checks before a scan: bump it when the env, arguments or report change incompatibly.
LABEL io.easyengine.compat-scanner.contract="2"

COPY --from=composer /opt/compat-scanner /opt/compat-scanner
COPY --from=libs80 /out/ /opt/php/8.0/
COPY --from=libs81 /out/ /opt/php/8.1/
COPY --from=php82 /usr/local/bin/php /opt/php/8.2/php
COPY --from=php83 /usr/local/bin/php /opt/php/8.3/php
COPY --from=php85 /usr/local/bin/php /opt/php/8.5/php
COPY --from=php86 /usr/local/bin/php /opt/php/8.6/php
RUN mkdir /opt/php/8.4 && ln -s /usr/local/bin/php /opt/php/8.4/php
COPY bin/ /opt/compat-scanner/bin/
COPY src/ /opt/compat-scanner/src/

USER 33:33
ENTRYPOINT ["php", "-d", "memory_limit=256M", "-d", "display_errors=stderr", "/opt/compat-scanner/bin/compat-scanner.php"]
# 1G plus a PHPCS or lint child's 896M stays below EE's 2g container limit: PHP's own error (a tool_error), not a silent OOM kill, ends a scan that outgrows it.
ENTRYPOINT ["php", "-d", "memory_limit=1G", "-d", "display_errors=stderr", "/opt/compat-scanner/bin/compat-scanner.php"]
1,567 changes: 1,208 additions & 359 deletions compat-scanner/bin/compat-scanner.php

Large diffs are not rendered by default.

274 changes: 274 additions & 0 deletions compat-scanner/src/CompatScanner/Guard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
<?php
/**
* The strict guard grammar the scanner's sniffs and its report share: which if/elseif/else blocks run only where a name exists or is missing, or only on some PHP versions. Anything the grammar doesn't name is no guard.
*/

namespace CompatScanner;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Utils\Namespaces;
use PHPCSUtils\Utils\PassedParameters;

final class Guard {

/** The PHP_VERSION_ID above every version. */
const TOP = PHP_INT_MAX;

/**
* The PHP_VERSION_ID range [from, to) where a version test holds, from the text of its tokens (no whitespace or comments): `PHP_VERSION_ID <op> N` either way round with N a plain decimal, or `version_compare( PHP_VERSION, 'X.Y[.Z]', '<' | 'lt' | '>=' | 'ge' )`.
*
* @return int[]|null
*/
public static function range( array $words ): ?array {
$w = array_map( 'ltrim', $words, array_fill( 0, count( $words ), '\\' ) );
$flip = [ '<' => '>', '<=' => '>=', '>' => '<', '>=' => '<=' ];
if ( 3 === count( $w ) && 'PHP_VERSION_ID' === $w[2] && isset( $flip[ $w[1] ] ) ) {
$w = [ $w[2], $flip[ $w[1] ], $w[0] ];
}
if ( 3 === count( $w ) && 'PHP_VERSION_ID' === $w[0] && isset( $flip[ $w[1] ] ) && preg_match( '/^[1-9][0-9]*$/', $w[2] ) ) {
$n = (int) $w[2];
return [ '<' => [ 0, $n ], '<=' => [ 0, $n + 1 ], '>' => [ $n + 1, self::TOP ], '>=' => [ $n, self::TOP ] ][ $w[1] ];
}
// Positional arguments only: a named one can reorder them.
if ( 8 !== count( $w ) || 'version_compare' !== strtolower( $w[0] ) || [ '(', 'PHP_VERSION', ',', ',', ')' ] !== [ $w[1], $w[2], $w[3], $w[5], $w[7] ] ) {
return null;
}
if ( ! preg_match( '/^([\'"])(\d+)(?:\.(\d+))?(?:\.(\d+))?\1$/', $w[4], $v ) || ! preg_match( '/^([\'"])(<|lt|>=|ge)\1$/', $w[6], $c ) ) {
return null;
}
// PHP_VERSION_ID has two digits each for the minor and the patch version; '8.3.200' would read as 8.5.
if ( (int) ( $v[3] ?? 0 ) > 99 || (int) ( $v[4] ?? 0 ) > 99 ) {
return null;
}
$id = $v[2] * 10000 + (int) ( $v[3] ?? 0 ) * 100 + (int) ( $v[4] ?? 0 );
return in_array( $c[2], [ '<', 'lt' ], true ) ? [ 0, $id ] : [ $id, self::TOP ];
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

/** The PHP_VERSION_ID range of a target `X.Y`: every X.Y.Z. */
public static function target( string $version ): array {
list( $major, $minor ) = explode( '.', $version );
$id = $major * 10000 + $minor * 100;
return [ $id, $id + 100 ];
}

/** Whether a block that runs where the range holds never runs on the target. */
public static function misses( array $range, array $target ): bool {
return $range[1] <= $target[0] || $range[0] >= $target[1];
}

/** Whether the range holds on every version of the target. */
public static function covers( array $range, array $target ): bool {
return $range[0] <= $target[0] && $range[1] >= $target[1];
}

/**
* Whether a block enclosing $ptr, within its innermost function or class and with no goto label in it, runs only where $inIf accepts an `&&` operand of its if or elseif, or is the else of an if with a `||` operand $inElse accepts.
*/
public static function encloses( File $file, int $ptr, callable $inIf, callable $inElse ): bool {
$tokens = $file->getTokens();
if ( ! $tokens[ $ptr ]['conditions'] ) {
return false;
}
$scope = self::scope( $file, $ptr );
foreach ( array_keys( $tokens[ $ptr ]['conditions'] ) as $block ) {
// A guard around a function or class decides only whether it is declared; code outside the guard can call it.
if ( $block < $scope || self::jumpedInto( $file, $block ) ) {
continue;
}
if ( T_ELSE === $tokens[ $block ]['code'] ) {
// The closing brace before `else` belongs to the if or elseif it is the else of.
$prev = $file->findPrevious( Tokens::EMPTY_TOKENS, $block - 1, null, true );
$if = T_CLOSE_CURLY_BRACKET === $tokens[ $prev ]['code'] ? ( $tokens[ $prev ]['scope_condition'] ?? null ) : null;
if ( null !== $if && self::any( $file, $if, true, $inElse ) ) {
return true;
}
} elseif ( self::any( $file, $block, false, $inIf ) ) {
return true;
}
}
return false;
}

/** The innermost function, closure, arrow function or class around $ptr, or -1. */
private static function scope( File $file, int $ptr ): int {
$tokens = $file->getTokens();
$scope = -1;
foreach ( $tokens[ $ptr ]['conditions'] as $block => $code ) {
if ( T_FUNCTION === $code || T_CLOSURE === $code || isset( Tokens::OO_SCOPE_TOKENS[ $code ] ) ) {
$scope = $block;
}
}
// A parameter list belongs to its function, though it isn't one of the function's conditions.
foreach ( $tokens[ $ptr ]['nested_parenthesis'] ?? [] as $open => $close ) {
$owner = $tokens[ $open ]['parenthesis_owner'] ?? -1;
if ( $owner > $scope && in_array( $tokens[ $owner ]['code'] ?? null, [ T_FUNCTION, T_CLOSURE, T_FN ], true ) ) {
$scope = $owner;
}
}
// Arrow functions aren't conditions; only one inside the outermost block matters.
$from = max( $scope, array_key_first( $tokens[ $ptr ]['conditions'] ) );
for ( $fn = $ptr; false !== ( $fn = $file->findPrevious( T_FN, $fn - 1, $from + 1 ) ); ) {
if ( ( $tokens[ $fn ]['scope_closer'] ?? -1 ) >= $ptr ) {
return $fn;
}
}
return $scope;
}

/** Whether a goto can enter the block: a label inside it. */
private static function jumpedInto( File $file, int $block ): bool {
$tokens = $file->getTokens();
return isset( $tokens[ $block ]['scope_closer'] ) && false !== $file->findNext( T_GOTO_LABEL, $block, $tokens[ $block ]['scope_closer'] );
}

/** Whether the operand is `check( 'name' )` with that polarity (false: negated), or a version test whose range $fits accepts. */
public static function tests( File $file, array $op, array $checks, string $name, bool $polarity, callable $fits ): bool {
$range = self::versions( $file, $op );
return $polarity === self::checks( $file, $op, $checks, $name ) || ( null !== $range && $fits( $range ) );
}

/** Whether $test accepts an operand of the condition of the if or elseif at $ptr, split at its top-level `||` ($or) or `&&`. */
public static function any( File $file, int $ptr, bool $or, callable $test ): bool {
foreach ( (array) self::operands( $file, $ptr, $or ) as $op ) {
if ( $test( $op ) ) {
return true;
}
}
return false;
}

/**
* The top-level operands of the condition of the if or elseif at $ptr, each without parentheses around it; null when it isn't one or another top-level logical operator joins them.
*
* @return array<int,int[]>|null
*/
public static function operands( File $file, int $ptr, bool $or ): ?array {
$tokens = $file->getTokens();
if ( ! in_array( $tokens[ $ptr ]['code'], [ T_IF, T_ELSEIF ], true ) || ! isset( $tokens[ $ptr ]['parenthesis_closer'] ) ) {
return null;
}
$open = $tokens[ $ptr ]['parenthesis_opener'];
$depth = count( $tokens[ $open ]['nested_parenthesis'] ?? [] ) + 1;
$join = $or ? [ T_BOOLEAN_OR, T_LOGICAL_OR ] : [ T_BOOLEAN_AND, T_LOGICAL_AND ];
$other = $or ? [ T_BOOLEAN_AND, T_LOGICAL_AND ] : [ T_BOOLEAN_OR, T_LOGICAL_OR ];
// These bind looser than `&&` and `||` and take in the operators after them: `! $x = a && b` is `! ( $x = ( a && b ) )`.
$looser = [ T_FN, T_PRINT, T_YIELD, T_YIELD_FROM, T_THROW, T_INCLUDE, T_INCLUDE_ONCE, T_REQUIRE, T_REQUIRE_ONCE ];
$other = array_merge( $other, [ T_LOGICAL_XOR, T_INLINE_THEN, T_COALESCE ], $looser, array_keys( Tokens::ASSIGNMENT_TOKENS ) );

$operands = [ [] ];
$inside = -1;
for ( $i = $open + 1; $i < $tokens[ $ptr ]['parenthesis_closer']; $i++ ) {
$code = $tokens[ $i ]['code'];
if ( isset( Tokens::EMPTY_TOKENS[ $code ] ) ) {
continue;
}
// Operators in brackets or braces belong to what they hold: `[ a && b ]`, `$m[ a && b ]`, a closure's body.
$top = $i > $inside && count( $tokens[ $i ]['nested_parenthesis'] ?? [] ) === $depth;
if ( $top && ( $tokens[ $i ]['bracket_opener'] ?? null ) === $i ) {
$inside = $tokens[ $i ]['bracket_closer'];
}
if ( $top && in_array( $code, $other, true ) ) {
return null;
}
if ( $top && in_array( $code, $join, true ) ) {
$operands[] = [];
continue;
}
$operands[ count( $operands ) - 1 ][] = $i;
}
return array_map(
static function ( $op ) use ( $tokens ) {
return self::unwrap( $tokens, $op );
},
$operands
);
}

/** The version range an operand tests, or null when it is no version test. */
public static function versions( File $file, array $op ): ?array {
$tokens = $file->getTokens();
return self::range(
array_map(
static function ( $ptr ) use ( $tokens ) {
return $tokens[ $ptr ]['content'];
},
$op
)
);
}

/**
* Whether an operand checks for the name with one of $checks: true for exactly `check( 'name' )`, false for its negation (`! check()`, `false === check()`, `check() === false`), null otherwise.
*/
private static function checks( File $file, array $op, array $checks, string $name ): ?bool {
$tokens = $file->getTokens();
$n = count( $op );
$inner = null;
if ( $n > 1 && T_BOOLEAN_NOT === $tokens[ $op[0] ]['code'] ) {
$inner = self::unwrap( $tokens, array_slice( $op, 1 ) );
} elseif ( $n > 2 && T_IS_IDENTICAL === $tokens[ $op[1] ]['code'] && T_FALSE === $tokens[ $op[0] ]['code'] ) {
$inner = array_slice( $op, 2 );
} elseif ( $n > 2 && T_IS_IDENTICAL === $tokens[ $op[ $n - 2 ] ]['code'] && T_FALSE === $tokens[ $op[ $n - 1 ] ]['code'] ) {
$inner = array_slice( $op, 0, $n - 2 );
}
if ( null !== $inner ) {
return self::named( $file, $inner, $checks, $name ) ? false : null;
}
return self::named( $file, $op, $checks, $name ) ? true : null;
}

/** Whether the tokens are exactly one call of $checks with the name as its first argument. */
private static function named( File $file, array $op, array $checks, string $name ): bool {
$tokens = $file->getTokens();
if ( ! self::call( $tokens, $op, $checks ) ) {
return false;
}
$arg = PassedParameters::getParameter( $file, $op[0], 1, [ 'function', 'class', 'interface', 'enum', 'constant_name' ] );
if ( false === $arg ) {
return false;
}
$given = self::nameOf( $file, $arg['start'], $arg['clean'] );
// A constant's name is case-sensitive, and defined() takes no X::class.
if ( [ 'defined' ] === $checks ) {
return preg_match( '/^[\'"]/', $arg['clean'] ) && $given === ltrim( $name, '\\' );
}
return strtolower( (string) $given ) === strtolower( $name );
}

/**
* The full name an argument gives: 'name' or "name" (parts joined by `\` or `\\`, an optional leading one), or name::class resolved in its namespace (PHPCompatibility reports no new class whose short name a `use` statement imports); null for anything else.
*/
private static function nameOf( File $file, int $ptr, string $arg ): ?string {
if ( preg_match( '/^([\'"])(\\\\{0,2}(?:\w+\\\\{1,2})*\w+)\1$/', $arg, $m ) ) {
// In double quotes `\n`, `\r` and the like are escapes, not separators.
$escape = '"' === $m[1] && preg_match( '/\\\\[nrtvef0-7xu]/', str_replace( '\\\\', '', $m[2] ) );
return $escape ? null : ltrim( str_replace( '\\\\', '\\', $m[2] ), '\\' );
}
if ( ! preg_match( '/^(\\\\?)((?:\w+\\\\)*\w+)::class$/i', $arg, $m ) ) {
return null;
}
if ( '' !== $m[1] ) {
return $m[2];
}
$ns = Namespaces::determineNamespace( $file, $ptr );
return '' === $ns ? $m[2] : "$ns\\$m[2]";
}

/** Whether the tokens are exactly a call of one of $names, `\` allowed. */
private static function call( array $tokens, array $op, array $names ): bool {
$fn = in_array( $tokens[ $op[0] ]['code'] ?? null, [ T_STRING, T_NAME_FULLY_QUALIFIED ], true );
if ( ! $fn || ! in_array( strtolower( ltrim( $tokens[ $op[0] ]['content'], '\\' ) ), $names, true ) || count( $op ) < 3 ) {
return false;
}
return T_OPEN_PARENTHESIS === $tokens[ $op[1] ]['code'] && $tokens[ $op[1] ]['parenthesis_closer'] === end( $op );
}

/** The pointers without parentheses around all of them. */
private static function unwrap( array $tokens, array $op ): array {
while ( count( $op ) > 2 && T_OPEN_PARENTHESIS === $tokens[ $op[0] ]['code'] && $tokens[ $op[0] ]['parenthesis_closer'] === end( $op ) ) {
$op = array_slice( $op, 1, -1 );
}
return $op;
}
}
Loading