| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles the token management for code analysis. |
| 4 |
* |
| 5 |
* @package WP_Defender\Component\Scan |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Component\Scan; |
| 9 |
|
| 10 |
use WP_Defender\Component; |
| 11 |
|
| 12 |
/** |
| 13 |
* Manages tokens for code analysis. |
| 14 |
*/ |
| 15 |
class Tokens extends Component { |
| 16 |
|
| 17 |
/** |
| 18 |
* Holds all tokens. |
| 19 |
* |
| 20 |
* @var array |
| 21 |
*/ |
| 22 |
public static $tokens = array(); |
| 23 |
|
| 24 |
/** |
| 25 |
* Retrieves tokens by their line number. |
| 26 |
* |
| 27 |
* @param int $line The line number to fetch tokens for. |
| 28 |
* |
| 29 |
* @return array An array of tokens found on the specified line. |
| 30 |
*/ |
| 31 |
public static function get_tokens_by_line( $line ): array { |
| 32 |
$catch = array(); |
| 33 |
foreach ( self::$tokens as $token ) { |
| 34 |
if ( $line === $token['line'] ) { |
| 35 |
$catch[] = $token; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
return $catch; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Formats a list of tokens into a single string. |
| 44 |
* |
| 45 |
* @param array $tokens An array of tokens to format. |
| 46 |
* |
| 47 |
* @return string A string created by concatenating the content of each token. |
| 48 |
*/ |
| 49 |
public static function formatter( $tokens ): string { |
| 50 |
$string = ''; |
| 51 |
foreach ( $tokens as $token ) { |
| 52 |
$string .= $token['content']; |
| 53 |
} |
| 54 |
|
| 55 |
return $string; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Determines the line number from an offset in the content. |
| 60 |
* This method is a polyfill for environments with PHP versions less than 8.0. |
| 61 |
* |
| 62 |
* @param string $content The content to examine. |
| 63 |
* @param int $offset The offset in the content to find the line number for. |
| 64 |
* |
| 65 |
* @return int The line number at the specified offset. |
| 66 |
*/ |
| 67 |
public static function get_line_from_offset( $content, $offset ): int { |
| 68 |
// Polyfill for PHP version < 8.0. |
| 69 |
$offset = max( $offset, 1 ); |
| 70 |
[ $before ] = str_split( $content, $offset ); |
| 71 |
|
| 72 |
return strlen( $before ) - strlen( str_replace( PHP_EOL, '', $before ) ) + 1; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get a list of [line,column] of each token. We will need to use this for highlight the code on frontend. |
| 77 |
* |
| 78 |
* @param array $tokens An array of tokens to map. |
| 79 |
* |
| 80 |
* @return array |
| 81 |
*/ |
| 82 |
public static function get_offsets_map( $tokens = array() ): array { |
| 83 |
$tmp = array(); |
| 84 |
$mapper = array(); |
| 85 |
foreach ( $tokens as $token ) { |
| 86 |
$tmp[ $token['line'] ][] = $token['column'] + $token['length']; |
| 87 |
} |
| 88 |
foreach ( $tmp as $line => &$cols ) { |
| 89 |
sort( $cols ); |
| 90 |
$start = count( $cols ) > 1 ? current( $cols ) : 0; |
| 91 |
$range = array( $start, end( $cols ) ); |
| 92 |
$mapper[] = array( |
| 93 |
'line' => $line, |
| 94 |
'range' => $range, |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
return $mapper; |
| 99 |
} |
| 100 |
} |
| 101 |
|