Repository navigation
feat(compat-scanner): scan all of app/ and judge every WordPress install by its version #405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
d4fac42
32aed79
a25bebe
6465a9d
7b563ed
14046cc
dbe6a78
e3f9118
a5fd8e6
ce42b7c
7c1ba08
3cecfd8
b34b311
fb44320
2084908
49127be
0245d7e
e24f620
8258722
083658f
242107a
864e0fc
e87f7e6
1949a88
c30c8bf
6250542
5f2e9bb
1c8e19d
8ae627d
39606fd
8fee2fd
97fe322
c7b250b
bb62736
a663f03
7df0f2d
37de1da
43736ba
08f6c14
2cb69ca
c4bf1a6
29f771b
d2d3bd9
4364daa
bacd6fb
6bf18cb
28af03c
69a1e04
29e1aa4
a458768
6caebde
abab5df
57f272f
a817b57
577a96e
0af602a
ce9f838
3a546a9
b75cb47
3dc4ad0
24d4105
ae61aba
33bfa7c
acb699a
b4b5e92
8a800ce
3f5c97d
38733e6
b323539
3e9976f
caabc54
17e5cc1
40219f3
aafa13a
a1d95e8
c43805f
4d9cda5
0ef9e66
901d26f
b093f76
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,253 @@ | ||
| <?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; | ||
| } | ||
| $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 ]; | ||
| } | ||
|
|
||
| /** 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 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 ]; | ||
| $other = array_merge( $other, [ T_LOGICAL_XOR, T_INLINE_THEN, T_COALESCE ] ); | ||
|
|
||
| $operands = [ [] ]; | ||
| for ( $i = $open + 1; $i < $tokens[ $ptr ]['parenthesis_closer']; $i++ ) { | ||
| $code = $tokens[ $i ]['code']; | ||
| if ( isset( Tokens::EMPTY_TOKENS[ $code ] ) ) { | ||
| continue; | ||
| } | ||
| $top = count( $tokens[ $i ]['nested_parenthesis'] ?? [] ) === $depth; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not treat operators inside square brackets as guard separators. In Ignore operators inside bracketed expressions, or reject those expressions as guards. Keep splitting a direct condition such as
|
||
| 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; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| <?php | ||
| /** | ||
| * A global class, enum or interface named like one PHP has in the target, declared unguarded: fatal there ("Cannot declare class"). | ||
| * Guards are those of functions, and a file-scope `if` that holds where the native exists and returns before the declaration. | ||
| */ | ||
|
|
||
| namespace CompatScanner\Sniffs\Classes; | ||
|
|
||
| use CompatScanner\Sniffs\Functions\NativeDeclaredSniff as NativeFunctionSniff; | ||
| use CompatScanner\Guard; | ||
| use PHP_CodeSniffer\Files\File; | ||
| use PHP_CodeSniffer\Util\Tokens; | ||
|
|
||
| class NativeDeclaredSniff extends NativeFunctionSniff { | ||
|
|
||
| /** The checks that see a native of each kind: class_exists() is false for an interface. */ | ||
| private const CHECKS = [ | ||
| 'class' => [ 'class_exists' ], | ||
| 'enum' => [ 'class_exists', 'enum_exists' ], | ||
| 'interface' => [ 'interface_exists' ], | ||
| ]; | ||
|
|
||
| /** @var string */ | ||
| protected $message = 'Class %s is native since PHP %s; declaring it without a class_exists() or interface_exists() check is fatal there'; | ||
|
|
||
| public function register() { | ||
| return [ T_CLASS, T_INTERFACE, T_TRAIT, T_ENUM ]; | ||
| } | ||
|
|
||
| protected function native( string $lower ): ?array { | ||
| $this->table = $this->table ?? require __DIR__ . '/../../../native-classes.php'; | ||
| if ( ! isset( $this->table[ $lower ] ) ) { | ||
| return null; | ||
| } | ||
| list( $since, $kind ) = $this->table[ $lower ]; | ||
| return [ 'since' => $since, 'checks' => self::CHECKS[ $kind ] ]; | ||
| } | ||
|
|
||
| /** | ||
| * PHP declares a class when its declaration runs, so a file-scope statement `if ( <exists> ) { return; }` before it is a guard: the if a statement of its own, its block nothing but the return, and no goto in the file to jump past it. | ||
| */ | ||
| protected function returnsBefore( File $phpcsFile, int $stackPtr, array $native ): bool { | ||
| $tokens = $phpcsFile->getTokens(); | ||
| // In a function body it runs whenever the function is called, which other files can do. | ||
| if ( array_intersect( $tokens[ $stackPtr ]['conditions'], [ T_FUNCTION, T_CLOSURE, T_FN ] ) || false !== $phpcsFile->findNext( T_GOTO, 0 ) ) { | ||
| return false; | ||
| } | ||
| $exists = function ( $op ) use ( $phpcsFile, $native ) { | ||
| return $this->exists( $phpcsFile, $op, $native ); | ||
| }; | ||
| for ( $if = $phpcsFile->findNext( T_IF, 0, $stackPtr ); false !== $if; $if = $phpcsFile->findNext( T_IF, $if + 1, $stackPtr ) ) { | ||
| if ( $this->onlyReturns( $phpcsFile, $if ) && $tokens[ $if ]['scope_closer'] < $stackPtr && Guard::any( $phpcsFile, $if, true, $exists ) ) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** Whether the if is a file-scope statement (not an else's, nor the body of a braceless one) whose block is one return. */ | ||
| private function onlyReturns( File $phpcsFile, int $if ): bool { | ||
| $tokens = $phpcsFile->getTokens(); | ||
| $prev = $phpcsFile->findPrevious( Tokens::EMPTY_TOKENS, $if - 1, null, true ); | ||
| $after = in_array( $tokens[ $prev ]['code'], [ T_OPEN_TAG, T_SEMICOLON, T_CLOSE_CURLY_BRACKET ], true ); | ||
| if ( 0 !== $tokens[ $if ]['level'] || ! $after || ! isset( $tokens[ $if ]['scope_opener'], $tokens[ $if ]['scope_closer'] ) ) { | ||
| return false; | ||
| } | ||
| $return = $phpcsFile->findNext( Tokens::EMPTY_TOKENS, $tokens[ $if ]['scope_opener'] + 1, null, true ); | ||
| if ( T_RETURN !== $tokens[ $return ]['code'] ) { | ||
| return false; | ||
| } | ||
| $end = $phpcsFile->findEndOfStatement( $return ); | ||
| return $phpcsFile->findNext( Tokens::EMPTY_TOKENS, $end + 1, null, true ) === $tokens[ $if ]['scope_closer']; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not convert out-of-range version components into
PHP_VERSION_IDbounds.For a PHP 8.4 target,
version_compare(\PHP_VERSION, '8.3.200', '>=')can protect a block that still runs on every 8.4 release. PHP compares the minor component before the patch component. The scanner instead converts8.3.200to80500and can suppress a compatibility error inside that block. (raw.githubusercontent.com)Reject literals whose components cannot be represented without carry or overflow, or compare the components directly. Keep recognizing ordinary bounds such as
8.5.0.