PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.3.1
Defender Security – Malware Scanner, Login Security & Firewall v5.3.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / component / scan / class-tokens.php

class-tokens.php in Defender Security – Malware Scanner, Login Security & Firewall 5.3.1, at src/component/scan/class-tokens.php

101 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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