PluginProbe
Plugin Check (PCP) / trunk
Plugin Check (PCP) vtrunk
2.1.0 trunk 0.1 0.2.0 0.2.1 0.2.2 0.2.3 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.3.0 1.3.1 1.4.0 1.5.0 1.6.0 1.7.0 1.8.0 1.9.0 2.0.0 ci-artifacts
plugin-check / includes / Scanner / Log.php

Log.php in Plugin Check (PCP) trunk, at includes/Scanner/Log.php

322 lines 10.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class WordPress\Plugin_Check\Scanner\Log
4 *
5 * @package plugin-check
6 */
7
8 namespace WordPress\Plugin_Check\Scanner;
9
10 /**
11 * Log class.
12 *
13 * @since 1.7.0
14 */
15 class Log {
16
17 /**
18 * An array of logs.
19 *
20 * @since 1.7.0
21 * @var array
22 */
23 private array $log = array();
24
25 /**
26 * An array of logs with longer location.
27 *
28 * @since 1.7.0
29 * @var array
30 */
31 private array $log_longer_location = array();
32
33 /**
34 * An instance of the PHP_Parser class.
35 *
36 * @since 1.7.0
37 * @var PHP_Parser
38 */
39 public PHP_Parser $parser_object;
40
41 /**
42 * An array of strings to be removed from the log.
43 *
44 * @since 1.7.0
45 * @var string[]
46 */
47 private array $remove_strings_from_log = array();
48
49 /**
50 * Constructor.
51 *
52 * @since 1.7.0
53 *
54 * @param PHP_Parser $parser An instance of the PHP_Parser class.
55 */
56 public function __construct( $parser ) {
57 $this->parser_object = $parser;
58 }
59
60 /**
61 * Adds a log entry with the specified details to the log storage.
62 *
63 * @since 1.7.0
64 *
65 * @param int $line_number The line number where the log entry originates from.
66 * @param string $text The text content of the log entry.
67 * @param string $logid Optional. The identifier for the log group. Defaults to 'default'.
68 * @param bool $unique Optional. Whether the log entry should be unique within the group. Defaults to false.
69 * @return void
70 */
71 public function add( $line_number, $text, $logid = 'default', $unique = false ) {
72 if ( ! empty( $this->remove_strings_from_log ) ) {
73 foreach ( $this->remove_strings_from_log as $remove_string ) {
74 $text = str_replace( $remove_string, '', $text );
75 }
76 }
77
78 $log_line = array(
79 'location' => '',
80 'line' => $line_number,
81 'text' => $text,
82 );
83
84 if ( ! empty( $line_number ) ) {
85 $log_line['location'] = $this->get_location_string_for_current_file_and_line( $line_number, $this->parser_object->file_relative );
86 }
87
88 if ( ! isset( $this->log_longer_location[ $logid ] ) ) {
89 $this->log_longer_location[ $logid ] = 0;
90 }
91
92 if ( strlen( $log_line['location'] ) > $this->log_longer_location[ $logid ] ) {
93 $this->log_longer_location[ $logid ] = strlen( $log_line['location'] );
94 }
95
96 if ( $unique ) {
97 $line_id = $this->get_line_id( $line_number );
98
99 $this->log[ $logid ][ $line_id ] = $log_line;
100 } else {
101 $this->log[ $logid ][] = $log_line;
102 }
103 }
104
105 /**
106 * Generates a unique identifier for a specific line in a file.
107 *
108 * @since 1.7.0
109 *
110 * @param int $line_number The line number for which the identifier is generated.
111 * @return string The MD5 hash representing the unique identifier for the line.
112 */
113 public function get_line_id( $line_number ) {
114 return md5( $this->parser_object->file_relative . '_' . $line_number );
115 }
116
117 /**
118 * Retrieves the log data associated with the provided log identifier.
119 *
120 * @param string $logid The identifier of the log to retrieve. Defaults to 'default'.
121 *
122 * @return array The log data associated with the provided identifier, or an empty array if no data exists.
123 */
124 public function get( $logid = 'default' ) {
125 return $this->log[ $logid ] ?? array();
126 }
127
128 /**
129 * Merges the provided log data with the existing log data for the specified identifier.
130 *
131 * @since 1.7.0
132 *
133 * @param array $log The log data to merge.
134 * @param string $logid The identifier of the log to merge into. Defaults to 'default'.
135 * @return void
136 */
137 public function merge( $log, $logid = 'default' ) {
138 if ( empty( $this->log[ $logid ] ) ) {
139 $this->log[ $logid ] = array();
140 }
141
142 $this->log[ $logid ] = array_merge( $this->log[ $logid ], $log );
143 }
144
145 /**
146 * Adds a namespace to the log for a given identifier.
147 *
148 * @since 1.7.0
149 *
150 * @param object $namespace_obj The namespace object to add, which should include a name property.
151 * @param string $logid The identifier of the log where the namespace information should be added.
152 * @param bool $unique Optional. Determines whether the log entry should be unique. Defaults to false.
153 * @return void
154 */
155 public function add_namespace( $namespace_obj, $logid, $unique = false ) {
156 if ( ! empty( $namespace_obj->name ) ) {
157 $line_text = 'namespace ' . $namespace_obj->name->toCodeString();
158
159 $line_number = method_exists( $namespace_obj, 'getStartLine' ) ? $namespace_obj->getStartLine() : 0;
160 $this->add( $line_number, $line_text, $logid, $unique );
161
162 $log_key = array_key_last( $this->log[ $logid ] );
163
164 $this->log[ $logid ][ $log_key ]['type'] = 'namespace';
165 $this->log[ $logid ][ $log_key ]['name'] = $namespace_obj->name->__toString();
166 }
167 }
168
169 /**
170 * Adds a function call expression to the log and assigns metadata for processing.
171 *
172 * @since 1.7.0
173 *
174 * @param object $func_call The function call expression object to be logged.
175 * @param int $argposition The position of the argument in the function call to extract metadata from.
176 * @param string $logid The identifier for the log where the function call should be added.
177 * @param bool $unique Optional. Whether the function call entry should be unique in the log. Defaults to false.
178 * @param bool $accurate Optional. Whether to enable accurate parsing when extracting the argument's metadata. Defaults to true.
179 * @return void
180 */
181 public function add_call_expr( $func_call, $argposition, $logid, $unique = false, $accurate = true ) {
182 $func_call->setAttribute( 'comments', null );
183
184 $line_number = method_exists( $func_call, 'getStartLine' ) ? $func_call->getStartLine() : 0;
185 $this->add( $line_number, $this->parser_object->pretty_printer->prettyPrint( array( $func_call ) ) . ';', $logid, $unique );
186
187 $log_key = array_key_last( $this->log[ $logid ] );
188 $func_name = $this->parser_object->get_call_name( $func_call );
189
190 $this->log[ $logid ][ $log_key ]['type'] = 'function_call-' . $func_name;
191
192 if ( isset( $func_call->args[ $argposition ] ) ) {
193 $arg = $func_call->args[ $argposition ];
194
195 $found_in_same_line = true; // Default.
196
197 $this->log[ $logid ][ $log_key ]['name'] = $this->parser_object->get_possible_string_for_element( $arg, $found_in_same_line, $accurate );
198 }
199 }
200
201 /**
202 * Adds a variable expression to the log and records its metadata.
203 *
204 * @since 1.7.0
205 *
206 * @param object $var_call The variable expression object to be logged. It is expected to be an instance of specific PhpParser classes.
207 * @param string $logid The identifier of the log where the variable expression should be added.
208 * @param bool $unique Optional. Whether the log entry should be unique. Defaults to false.
209 * @param bool $accurate Optional. Determines if processing should consider accuracy when extracting the variable name. Defaults to true.
210 * @return void
211 */
212 public function add_var_expr( $var_call, $logid, $unique = false, $accurate = true ) {
213 $var_call->setAttribute( 'comments', null );
214
215 $line_number = method_exists( $var_call, 'getStartLine' ) ? $var_call->getStartLine() : 0;
216 $this->add( $line_number, $this->parser_object->pretty_printer->prettyPrint( array( $var_call ) ) . ';', $logid, $unique );
217
218 $log_key = array_key_last( $this->log[ $logid ] );
219 $var_name = '';
220
221 if ( is_a( $var_call, 'PhpParser\Node\Expr\ArrayDimFetch' ) ) {
222 $dims = $this->parser_object->extract_dims_objects( $var_call );
223
224 if ( isset( $dims[0] ) ) {
225 $var_name = $this->parser_object->get_possible_string_for_element( $dims[0], $found_in_same_line, $accurate );
226 }
227 } elseif ( is_a( $var_call, 'PhpParser\Node\Const_' ) ) {
228 $var_name = $var_call->name->__toString();
229 } else {
230 $var_name = $var_call->name;
231 }
232
233 $this->log[ $logid ][ $log_key ]['type'] = 'variable';
234 $this->log[ $logid ][ $log_key ]['name'] = $var_name;
235 }
236
237 /**
238 * Adds abstraction declarations like classes, functions, interfaces, or traits to the log with specific context and type.
239 *
240 * @since 1.7.0
241 *
242 * @param array $abstractions An array of abstraction objects to process and add to the log. Each object must have a type and name.
243 * @param string $logid The identifier for the log to which the abstraction declarations will be added.
244 * @param bool $unique Whether to ensure unique entries in the log. Defaults to false.
245 * @return void
246 */
247 public function add_abstraction_declarations( $abstractions, $logid, $unique = false ) {
248 if ( ! empty( $abstractions ) ) {
249 foreach ( $abstractions as $abstract ) {
250 $continue = true;
251 $type = 'unknown';
252
253 switch ( $abstract->getType() ) {
254 case 'Stmt_Class':
255 $type = 'class';
256 break;
257 case 'Stmt_Function':
258 $contextual_stmts = $this->parser_object->get_contextual_stmts_for_element( $abstract )['context'];
259 if ( $contextual_stmts !== $abstract->stmts && ! empty( $abstract->stmts ) ) {
260 $continue = false; // Function declared inside another function.
261 }
262 $type = 'function';
263 break;
264 case 'Stmt_Interface':
265 $type = 'interface';
266 break;
267 case 'Stmt_Trait':
268 $type = 'trait';
269 break;
270 }
271
272 if ( empty( $abstract->name ) ) { // Ignore anonymous declarations.
273 $continue = false;
274 }
275
276 if ( $continue ) { // Ignore anonymous declarations.
277 $line_text = $type . ' ' . $abstract->name->toString();
278
279 $line_number = method_exists( $abstract, 'getStartLine' ) ? $abstract->getStartLine() : 0;
280 $this->add( $line_number, $line_text, $logid, $unique );
281
282 $log_key = array_key_last( $this->log[ $logid ] );
283
284 $this->log[ $logid ][ $log_key ]['type'] = 'abstraction';
285 $this->log[ $logid ][ $log_key ]['name'] = $abstract->name->__toString();
286 }
287 }
288 }
289 }
290
291 /**
292 * Checks whether log data exists for the specified log identifier.
293 *
294 * @since 1.7.0
295 *
296 * @param string $logid The identifier of the log to check for existence. Defaults to 'default'.
297 * @return bool True if log data exists for the provided identifier, otherwise false.
298 */
299 public function exists( $logid = 'default' ) {
300 return ! empty( $this->log[ $logid ] );
301 }
302
303 /**
304 * Constructs a location string consisting of the relative file path and line number.
305 *
306 * @since 1.7.0
307 *
308 * @param int $line_number The line number in the file.
309 * @param string $file_relative The file path relative to the root directory.
310 * @return string The formatted location string in the format "file_path:line_number ".
311 */
312 public function get_location_string_for_current_file_and_line( $line_number, $file_relative ) {
313 $parts = explode( '/', $file_relative, 2 );
314
315 if ( ! empty( $parts[1] ) ) {
316 $file_relative = $parts[1];
317 }
318
319 return $file_relative . ':' . $line_number . ' ';
320 }
321 }
322