| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class WordPress\Plugin_Check\Scanner\Prefix_Scanner |
| 4 |
* |
| 5 |
* @package plugin-check |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WordPress\Plugin_Check\Scanner; |
| 9 |
|
| 10 |
use PhpParser\Node; |
| 11 |
use PhpParser\Node\Const_; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class Prefix_Scanner |
| 15 |
* |
| 16 |
* This class is responsible for analyzing and logging PHP files for specific |
| 17 |
* prefixes used in WordPress plugin and theme development. It identifies potential |
| 18 |
* issues with prefix usage to maintain code consistency and avoid conflicts. |
| 19 |
*/ |
| 20 |
class Prefix_Scanner extends PHP_Parser { |
| 21 |
|
| 22 |
/** |
| 23 |
* List of common prefixes that should be avoided to ensure consistent naming conventions and avoid conflicts. |
| 24 |
* |
| 25 |
* @since 1.7.0 |
| 26 |
* @var array |
| 27 |
*/ |
| 28 |
private array $not_valid_prefixes; |
| 29 |
|
| 30 |
/** |
| 31 |
* Possible prefixes. |
| 32 |
* |
| 33 |
* @since 1.7.0 |
| 34 |
* @var array |
| 35 |
*/ |
| 36 |
private array $possible_prefixes = array(); |
| 37 |
|
| 38 |
/** |
| 39 |
* Processes items. |
| 40 |
* |
| 41 |
* @since 1.7.0 |
| 42 |
* @var array |
| 43 |
*/ |
| 44 |
private array $already_processed_items = array(); |
| 45 |
|
| 46 |
/** |
| 47 |
* Number of namespaces encountered. |
| 48 |
* |
| 49 |
* @since 1.7.0 |
| 50 |
* @var int |
| 51 |
*/ |
| 52 |
private int $namespaces_count = 0; |
| 53 |
|
| 54 |
/** |
| 55 |
* Final potential prefixes. |
| 56 |
* |
| 57 |
* @since 1.7.0 |
| 58 |
* @var array |
| 59 |
*/ |
| 60 |
public array $final_prefixes = array(); |
| 61 |
|
| 62 |
/** |
| 63 |
* Array of actions. |
| 64 |
* |
| 65 |
* @since 1.7.0 |
| 66 |
* @var array |
| 67 |
*/ |
| 68 |
private array $wp_actions; |
| 69 |
|
| 70 |
/** |
| 71 |
* Array of filters. |
| 72 |
* |
| 73 |
* @since 1.7.0 |
| 74 |
* @var array |
| 75 |
*/ |
| 76 |
private array $wp_filters; |
| 77 |
|
| 78 |
/** |
| 79 |
* Array of constants. |
| 80 |
* |
| 81 |
* @since 1.7.0 |
| 82 |
* @var array |
| 83 |
*/ |
| 84 |
private array $wp_constants; |
| 85 |
|
| 86 |
/** |
| 87 |
* Array of global variables. |
| 88 |
* |
| 89 |
* @since 1.7.0 |
| 90 |
* @var array |
| 91 |
*/ |
| 92 |
private array $wp_globals; |
| 93 |
|
| 94 |
/** |
| 95 |
* Array of options. |
| 96 |
* |
| 97 |
* @since 1.7.0 |
| 98 |
* @var array |
| 99 |
*/ |
| 100 |
private array $wp_options; |
| 101 |
|
| 102 |
/** |
| 103 |
* Array of transients. |
| 104 |
* |
| 105 |
* @since 1.7.0 |
| 106 |
* @var array |
| 107 |
*/ |
| 108 |
private array $wp_transients; |
| 109 |
|
| 110 |
/** |
| 111 |
* Array of site transients. |
| 112 |
* |
| 113 |
* @since 1.7.0 |
| 114 |
* @var array |
| 115 |
*/ |
| 116 |
private array $wp_site_transients; |
| 117 |
|
| 118 |
/** |
| 119 |
* Constructor. |
| 120 |
* |
| 121 |
* @since 1.7.0 |
| 122 |
*/ |
| 123 |
public function __construct() { |
| 124 |
parent::__construct(); |
| 125 |
$this->not_valid_prefixes = include dirname( __DIR__ ) . '/Vars/prefix-not-valid.php'; |
| 126 |
|
| 127 |
$wp_actions = include dirname( __DIR__ ) . '/Vars/wp-actions.php'; |
| 128 |
$common_actions = include dirname( __DIR__ ) . '/Vars/common-actions.php'; |
| 129 |
$this->wp_actions = array_merge( $wp_actions, $common_actions ); |
| 130 |
|
| 131 |
$wp_filters = include dirname( __DIR__ ) . '/Vars/wp-filters.php'; |
| 132 |
$common_filters = include dirname( __DIR__ ) . '/Vars/common-filters.php'; |
| 133 |
$this->wp_filters = array_merge( $wp_filters, $common_filters ); |
| 134 |
|
| 135 |
$wp_globals = include dirname( __DIR__ ) . '/Vars/wp-globals.php'; |
| 136 |
$common_globals = include dirname( __DIR__ ) . '/Vars/common-globals.php'; |
| 137 |
$this->wp_globals = array_merge( $wp_globals, $common_globals ); |
| 138 |
|
| 139 |
$this->wp_constants = include dirname( __DIR__ ) . '/Vars/wp-constants.php'; |
| 140 |
$this->wp_options = include dirname( __DIR__ ) . '/Vars/wp-options.php'; |
| 141 |
$this->wp_transients = include dirname( __DIR__ ) . '/Vars/wp-transients.php'; |
| 142 |
$this->wp_site_transients = include dirname( __DIR__ ) . '/Vars/wp-site-transients.php'; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Loads files. |
| 147 |
* |
| 148 |
* @since 1.7.0 |
| 149 |
* |
| 150 |
* @param array $files Array of file paths. |
| 151 |
* @return void |
| 152 |
*/ |
| 153 |
public function load_files( $files ) { |
| 154 |
parent::load_files( $files ); |
| 155 |
|
| 156 |
if ( ! empty( $files ) ) { |
| 157 |
foreach ( $files as $file ) { |
| 158 |
$this->load( $file ); |
| 159 |
} |
| 160 |
|
| 161 |
$this->check_prefixes( 'prefixes_code' ); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Loads file. |
| 167 |
* |
| 168 |
* @since 1.7.0 |
| 169 |
* |
| 170 |
* @param string $file File path. |
| 171 |
* @return null |
| 172 |
*/ |
| 173 |
public function load( $file ) { |
| 174 |
parent::load( $file ); |
| 175 |
|
| 176 |
return null; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Executes a series of logging operations including tracking function calls, method calls, global declarations, and abstraction declarations. |
| 181 |
* |
| 182 |
* @since 1.7.0 |
| 183 |
* |
| 184 |
* @return void |
| 185 |
*/ |
| 186 |
public function find() { |
| 187 |
$this->add_function_calls_to_log(); |
| 188 |
$this->add_method_calls_to_log(); |
| 189 |
$this->add_globals_declarations_to_log(); |
| 190 |
$this->add_abstractions_declarations_to_log(); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Detects calls to specific functions (specially a set of WordPress functions) and adds them to the log with the argument that is of interest regarding prefixes. |
| 195 |
* |
| 196 |
* @since 1.7.0 |
| 197 |
* |
| 198 |
* @return void |
| 199 |
*/ |
| 200 |
private function add_function_calls_to_log() { |
| 201 |
// Find some calls to specific functions. |
| 202 |
$function_calls = $this->node_finder->findInstanceOf( $this->stmts, Node\Expr\FuncCall::class ); |
| 203 |
if ( ! empty( $function_calls ) ) { |
| 204 |
foreach ( $function_calls as $function_call ) { |
| 205 |
if ( $this->has_function_name( $function_call ) ) { |
| 206 |
switch ( $this->get_call_name( $function_call ) ) { |
| 207 |
case 'define': |
| 208 |
if ( isset( $function_call->args[0] ) ) { |
| 209 |
$this->add_call_expression_to_log( $function_call, $function_call->args[0], $this->wp_constants, 0 ); |
| 210 |
} |
| 211 |
break; |
| 212 |
case 'add_option': |
| 213 |
case 'add_site_option': |
| 214 |
case 'update_option': |
| 215 |
case 'update_site_option': |
| 216 |
if ( isset( $function_call->args[0] ) ) { |
| 217 |
$this->add_call_expression_to_log( $function_call, $function_call->args[0], $this->wp_options, 0 ); |
| 218 |
} |
| 219 |
break; |
| 220 |
case 'add_network_option': |
| 221 |
case 'update_network_option': |
| 222 |
case 'register_setting': |
| 223 |
if ( isset( $function_call->args[1] ) ) { |
| 224 |
$this->add_call_expression_to_log( $function_call, $function_call->args[1], $this->wp_options, 1 ); |
| 225 |
} |
| 226 |
break; |
| 227 |
case 'set_transient': |
| 228 |
if ( isset( $function_call->args[0] ) ) { |
| 229 |
$this->add_call_expression_to_log( $function_call, $function_call->args[0], $this->wp_transients, 0 ); |
| 230 |
} |
| 231 |
break; |
| 232 |
case 'set_site_transient': |
| 233 |
if ( isset( $function_call->args[0] ) ) { |
| 234 |
$this->add_call_expression_to_log( $function_call, $function_call->args[0], $this->wp_site_transients, 0 ); |
| 235 |
} |
| 236 |
break; |
| 237 |
case 'add_shortcode': |
| 238 |
case 'register_post_type': |
| 239 |
case 'register_taxonomy': |
| 240 |
if ( isset( $function_call->args[0] ) ) { |
| 241 |
$this->add_call_expression_to_log( $function_call, $function_call->args[0], array(), 0 ); |
| 242 |
} |
| 243 |
break; |
| 244 |
case 'do_action': |
| 245 |
if ( isset( $function_call->args[0] ) ) { |
| 246 |
$this->add_call_expression_to_log( $function_call, $function_call->args[0], $this->wp_actions, 0 ); |
| 247 |
} |
| 248 |
break; |
| 249 |
case 'apply_filters': |
| 250 |
if ( isset( $function_call->args[0] ) ) { |
| 251 |
$this->add_call_expression_to_log( $function_call, $function_call->args[0], $this->wp_filters, 0 ); |
| 252 |
} |
| 253 |
break; |
| 254 |
case 'wp_schedule_event': |
| 255 |
if ( isset( $function_call->args[2] ) ) { |
| 256 |
$this->add_call_expression_to_log( $function_call, $function_call->args[2], array(), 2 ); |
| 257 |
} |
| 258 |
break; |
| 259 |
case 'add_menu_page': |
| 260 |
case 'add_management_page': |
| 261 |
case 'add_options_page': |
| 262 |
case 'add_theme_page': |
| 263 |
case 'add_plugins_page': |
| 264 |
case 'add_users_page': |
| 265 |
case 'add_dashboard_page': |
| 266 |
case 'add_posts_page': |
| 267 |
case 'add_media_page': |
| 268 |
case 'add_links_page': |
| 269 |
case 'add_pages_page': |
| 270 |
case 'add_comments_page': |
| 271 |
if ( isset( $function_call->args[3] ) ) { |
| 272 |
$possible_string = $this->get_possible_string_for_element( $function_call->args[3] ); |
| 273 |
if ( ! empty( $possible_string ) && strlen( $possible_string ) > 8 && preg_match( '/[?\/&]/m', $possible_string ) ) { |
| 274 |
break; // Ignore it if this is enough long url-looking string. |
| 275 |
} |
| 276 |
$this->add_call_expression_to_log( $function_call, $function_call->args[3], array(), 3 ); |
| 277 |
} |
| 278 |
break; |
| 279 |
case 'add_submenu_page': |
| 280 |
if ( isset( $function_call->args[4] ) ) { |
| 281 |
$possible_string = $this->get_possible_string_for_element( $function_call->args[4] ); |
| 282 |
if ( ! empty( $possible_string ) && strlen( $possible_string ) > 8 && preg_match( '/[?\/&]/m', $possible_string ) ) { |
| 283 |
break; // Ignore it if this is enough long url-looking string. |
| 284 |
} |
| 285 |
$this->add_call_expression_to_log( $function_call, $function_call->args[4], array(), 4 ); |
| 286 |
} |
| 287 |
break; |
| 288 |
case 'wp_enqueue_script': |
| 289 |
case 'wp_register_script': |
| 290 |
case 'wp_enqueue_style': |
| 291 |
case 'wp_register_style': |
| 292 |
if ( isset( $function_call->args[0] ) ) { |
| 293 |
// Check only obvious cases where this is not the name of a library. |
| 294 |
$possible_string = $this->get_possible_string_for_element( $function_call->args[0] ); |
| 295 |
if ( ! empty( $possible_string ) ) { |
| 296 |
$re1 = '/^(my|custom|style|script|css|file)(?!lint|ize)/m'; // Obvious names. |
| 297 |
$re2 = '/^(js)(?!lint|on|ize|hint|api|olor|-cookie)[-_]/m'; // Obvious names, but more careful with js. |
| 298 |
$re3 = '/^(admin|wp)(?!-components|-categories|-tags|-custom-fields|-custom-header|-comments|-users|-forms|-gallery|-widgets|-mediaelement|-tinymce|-ajax-response|-lists|-codemirror|-color-picker|-api|-embed|-jquery-ui-dialog|-auth-check|-pointer|-embed-template-ie|-admin|-colopicker|-editor-font|-reusable-blocks|-blocks|-block-directory|-format-library|-block-editor-content|-editor-classic-layout-styles|-reset-editor-styles|-patterns|-block-library-theme|-theme-plugin-editor|-i18n|-a11y|-backbone|-sanitize|-date|-util|-hooks|-api-request|-plupload)[-_]?/m'; // Beginning by admin or wp but ignoring core names. |
| 299 |
if ( preg_match( $re1, $possible_string ) || preg_match( $re2, $possible_string ) || preg_match( $re3, $possible_string ) ) { |
| 300 |
$this->add_call_expression_to_log( $function_call, $function_call->args[0], array(), 0 ); |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
break; |
| 305 |
} |
| 306 |
} else { |
| 307 |
$this->log()->add_call_expr( $function_call, 0, 'prefixes_code', true ); |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* Detects calls to specific methods and adds them to the log. |
| 315 |
* |
| 316 |
* @since 1.7.0 |
| 317 |
* |
| 318 |
* @return void |
| 319 |
*/ |
| 320 |
private function add_method_calls_to_log() { |
| 321 |
$method_calls = $this->node_finder->findInstanceOf( $this->stmts, Node\Expr\MethodCall::class ); |
| 322 |
if ( ! empty( $method_calls ) ) { |
| 323 |
foreach ( $method_calls as $method_call ) { |
| 324 |
if ( ! empty( $method_call->name ) && ! empty( $method_call->var ) ) { |
| 325 |
$methodname = $this->get_possible_string_for_element( $method_call->name ); |
| 326 |
$varname = $this->get_variable_name( $method_call->var ); |
| 327 |
|
| 328 |
// WooCommerce add/update metadata (Heuristic since it checks if the variable is called product). |
| 329 |
if ( str_contains( $varname, 'product' ) && in_array( $methodname, array( 'update_meta_data', 'add_meta_data' ), true ) ) { |
| 330 |
if ( isset( $method_call->args[0] ) ) { |
| 331 |
$this->add_call_expression_to_log( $method_call, $method_call->args[0], array(), 0 ); |
| 332 |
} |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Detects calls to globals variables and adds them to the log. |
| 341 |
* |
| 342 |
* @since 1.7.0 |
| 343 |
* |
| 344 |
* @return void |
| 345 |
*/ |
| 346 |
private function add_globals_declarations_to_log() { |
| 347 |
// Find global variables definitions. |
| 348 |
|
| 349 |
// Eg: global $example variable. |
| 350 |
$globals = $this->node_finder->findInstanceOf( $this->stmts, Node\Stmt\Global_::class ); |
| 351 |
if ( ! empty( $globals ) ) { |
| 352 |
foreach ( $globals as $global ) { |
| 353 |
if ( ! empty( $global->vars ) ) { |
| 354 |
foreach ( $global->vars as $global_variable ) { |
| 355 |
if ( ! empty( $global_variable->name ) && is_string( $global_variable->name ) ) { |
| 356 |
if ( ! in_array( $global_variable->name, $this->wp_globals, true ) ) { // Ignore known core global variables. |
| 357 |
$this->log()->add_var_expr( $global_variable, 'prefixes_code', true ); |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
// Eg: $GLOBALS['example'] variable. |
| 366 |
$assigns = $this->node_finder->findInstanceOf( $this->stmts, Node\Expr\Assign::class ); |
| 367 |
if ( ! empty( $assigns ) ) { |
| 368 |
foreach ( $assigns as $assign ) { |
| 369 |
if ( ! empty( $assign->var ) && is_a( $assign->var, 'PhpParser\Node\Expr\ArrayDimFetch' ) ) { |
| 370 |
$dimfetch = $assign->var; |
| 371 |
if ( 'GLOBALS' === $this->get_variable_name( $dimfetch ) ) { |
| 372 |
$dims = $this->extract_dims_objects( $dimfetch ); |
| 373 |
// Ignores all cases where we can't find the dim elements. |
| 374 |
if ( isset( $dims[0] ) ) { |
| 375 |
$this->add_call_expression_to_log( $dimfetch, $dims[0], $this->wp_globals ); |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
// Find all consts: const EXAMPLE = 'example'. |
| 383 |
$namespaces = $this->node_finder->findInstanceOf( $this->stmts, Node\Stmt\Namespace_::class ); |
| 384 |
if ( empty( $namespaces ) ) { |
| 385 |
$consts = $this->node_finder->findInstanceOf( $this->stmts, Const_::class ); |
| 386 |
if ( ! empty( $consts ) ) { |
| 387 |
foreach ( $consts as $const ) { |
| 388 |
$is_inside_element_type = null; |
| 389 |
$contextual_stmts = $this->get_contextual_stmts_for_element( $const, $is_inside_element_type )['context']; |
| 390 |
if ( ! empty( $contextual_stmts ) && ! in_array( $is_inside_element_type, array( 'PhpParser\Node\Stmt\Class_', 'PhpParser\Node\Stmt\Interface_' ), true ) ) { // Ignore const inside a class. |
| 391 |
if ( isset( $const->name ) && method_exists( $const->name, '__toString' ) && ! empty( $const->name->__toString() ) ) { |
| 392 |
$this->log()->add_var_expr( $const, 'prefixes_code', true ); |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
/** |
| 401 |
* Logs abstraction declarations including namespaces, classes, functions (excluding anonymous and nested ones), |
| 402 |
* interfaces, and traits found in the provided statements. If a namespace is defined, only namespace declarations |
| 403 |
* are logged and the process stops early. Otherwise, all abstraction types are processed and logged accordingly. |
| 404 |
* |
| 405 |
* @since 1.7.0 |
| 406 |
* |
| 407 |
* @return void |
| 408 |
*/ |
| 409 |
private function add_abstractions_declarations_to_log() { |
| 410 |
// Find namespace (if namespace is defined there is no need to continue). |
| 411 |
$namespaces = $this->node_finder->findInstanceOf( $this->stmts, Node\Stmt\Namespace_::class ); |
| 412 |
if ( ! empty( $namespaces ) ) { |
| 413 |
foreach ( $namespaces as $namespace ) { |
| 414 |
$this->log()->add_namespace( $namespace, 'prefixes_code', true ); |
| 415 |
} |
| 416 |
} else { |
| 417 |
// Find all class nodes. |
| 418 |
$classes = $this->node_finder->findInstanceOf( $this->stmts, Node\Stmt\Class_::class ); |
| 419 |
$this->log()->add_abstraction_declarations( $classes, 'prefixes_code', true ); |
| 420 |
|
| 421 |
// Find all functions (ignores anonymous, and inside abstractions). |
| 422 |
$functions = $this->node_finder->findInstanceOf( $this->stmts, Node\Stmt\Function_::class ); |
| 423 |
$this->log()->add_abstraction_declarations( $functions, 'prefixes_code', true ); |
| 424 |
|
| 425 |
// Find all interfaces. |
| 426 |
$interfaces = $this->node_finder->findInstanceOf( $this->stmts, Node\Stmt\Interface_::class ); |
| 427 |
$this->log()->add_abstraction_declarations( $interfaces, 'prefixes_code', true ); |
| 428 |
|
| 429 |
// Find all traits. |
| 430 |
$traits = $this->node_finder->findInstanceOf( $this->stmts, Node\Stmt\Trait_::class ); |
| 431 |
$this->log()->add_abstraction_declarations( $traits, 'prefixes_code', true ); |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
/** |
| 436 |
* Adds a call expression or variable expression to the log based on the given parameters. |
| 437 |
* Handles various cases depending on the type of the call and the potential string element provided. |
| 438 |
* Also manages additional context logging if the expression is found on a different line. |
| 439 |
* |
| 440 |
* @since 1.7.0 |
| 441 |
* |
| 442 |
* @param object $call The call expression node to be logged. |
| 443 |
* @param mixed $expr The expression associated with the call. |
| 444 |
* @param array $filter An optional array of strings to exclude from logging. |
| 445 |
* @param int $arg_position The argument position to be used in logging, default is 0. |
| 446 |
* @return void |
| 447 |
*/ |
| 448 |
private function add_call_expression_to_log( $call, $expr, $filter = array(), $arg_position = 0 ) { |
| 449 |
if ( ! isset( $expr ) ) { |
| 450 |
return; |
| 451 |
} |
| 452 |
$possible_string_accurate = false; |
| 453 |
$found_in_same_line = true; |
| 454 |
$possible_string = $this->get_possible_string_for_element( $expr, $found_in_same_line, $possible_string_accurate ); |
| 455 |
|
| 456 |
if ( empty( $possible_string ) ) { |
| 457 |
$this->log()->add_call_expr( $call, 0, 'prefixes_code', true ); |
| 458 |
return; |
| 459 |
} |
| 460 |
|
| 461 |
if ( ! in_array( $possible_string, $filter, true ) ) { // Ignore some strings. |
| 462 |
$call_class = get_class( $call ); |
| 463 |
if ( in_array( $call_class, array( 'PhpParser\Node\Expr\FuncCall', 'PhpParser\Node\Expr\MethodCall' ), true ) ) { |
| 464 |
$this->log()->add_call_expr( $call, $arg_position, 'prefixes_code', true, $possible_string_accurate ); |
| 465 |
} elseif ( in_array( $call_class, array( 'PhpParser\Node\Expr\ArrayDimFetch' ), true ) ) { |
| 466 |
$this->log()->add_var_expr( $call, 'prefixes_code', true, $possible_string_accurate ); |
| 467 |
} |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
/** |
| 472 |
* Analyzes a log of items to check naming prefixes, identifying and normalizing |
| 473 |
* common prefixes, and logging any potential issues or observations regarding |
| 474 |
* prefix usage. |
| 475 |
* |
| 476 |
* @since 1.7.0 |
| 477 |
* |
| 478 |
* @param string $logid The identifier of the log to analyze. |
| 479 |
* @return void |
| 480 |
*/ |
| 481 |
private function check_prefixes( string $logid ) { |
| 482 |
$log = $this->log()->get( $logid ); |
| 483 |
|
| 484 |
// Removes exceptions and logs items whose prefix cannot be read. |
| 485 |
$log = $this->check_prefixes_process_exceptions_from_log( $log ); |
| 486 |
|
| 487 |
// Lowercase all the names. |
| 488 |
$log = $this->lowercase_item_names( $log ); |
| 489 |
|
| 490 |
$this->possible_prefixes = array(); |
| 491 |
$this->already_processed_items = array(); |
| 492 |
$this->namespaces_count = 0; |
| 493 |
|
| 494 |
// Check anything having a name. Extract possible prefixes. |
| 495 |
foreach ( $log as $key => $log_item ) { |
| 496 |
$item_identifier = $log_item['name'] . ':' . $log_item['type']; |
| 497 |
if ( isset( $log_item['name'] ) && ! in_array( $item_identifier, $this->already_processed_items, true ) ) { |
| 498 |
$this->already_processed_items[] = $item_identifier; |
| 499 |
|
| 500 |
$should_check_other_prefixes = $this->check_prefixes_add_special_prefixes( $log_item, $key ); |
| 501 |
|
| 502 |
if ( $should_check_other_prefixes ) { |
| 503 |
$this->check_prefixes_add_possible_prefixes( $log_item, $key, '_' ); |
| 504 |
$this->check_prefixes_add_possible_prefixes( $log_item, $key, '-' ); |
| 505 |
$this->check_prefixes_add_possible_prefixes( $log_item, $key, '\\' ); |
| 506 |
} |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
$this->already_processed_items = array_unique( $this->already_processed_items ); |
| 511 |
|
| 512 |
// Repetitions of each possible prefix. |
| 513 |
if ( ! empty( $this->possible_prefixes ) ) { |
| 514 |
$logsize = count( $log ) - ( count( $log ) - count( $this->already_processed_items ) ) - $this->namespaces_count; |
| 515 |
|
| 516 |
$return = $this->normalize_prefixes_array( $this->possible_prefixes, $log ); |
| 517 |
$common_prefixes = $return['commonprefixes']; |
| 518 |
$minimum_quantity_for_considering_prefix = $this->calculate_minimum_prefix_quantity( $logsize ); |
| 519 |
|
| 520 |
// Checking for prefixed used commonly. |
| 521 |
if ( ! empty( $common_prefixes ) ) { |
| 522 |
// In some situations they use similar prefixes like "PRTPR" and "PRT_PR" we process those cases together as they were a similar prefix regarding the prefixes_analysis_prefixed log. |
| 523 |
$similar_prefixes = $this->get_too_similar_prefixes_array( $common_prefixes ); |
| 524 |
$prefixes_already_processed = array(); |
| 525 |
foreach ( $common_prefixes as $prefix => $incidences ) { |
| 526 |
$incidences_count = count( $incidences ); |
| 527 |
if ( ! empty( $similar_prefixes[ $prefix ] ) ) { |
| 528 |
$incidences_count = $similar_prefixes[ $prefix ]['numberincidences']; |
| 529 |
} |
| 530 |
if ( $incidences_count >= $minimum_quantity_for_considering_prefix || reset( $incidences ) === 'namespace' ) { |
| 531 |
if ( ! in_array( $prefix, $prefixes_already_processed, true ) ) { |
| 532 |
if ( ! empty( $similar_prefixes[ $prefix ] ) ) { |
| 533 |
$this->final_prefixes = array_merge( $this->final_prefixes, $similar_prefixes[ $prefix ]['prefixes'] ); |
| 534 |
} else { |
| 535 |
$this->final_prefixes[] = $prefix; |
| 536 |
} |
| 537 |
} |
| 538 |
} |
| 539 |
if ( ! empty( $similar_prefixes[ $prefix ] ) ) { |
| 540 |
$prefixes_already_processed = array_merge( $prefixes_already_processed, $similar_prefixes[ $prefix ]['prefixes'] ); |
| 541 |
} else { |
| 542 |
$prefixes_already_processed[] = $prefix; |
| 543 |
} |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
// Discard prefixes that are less than 4 characters long. |
| 548 |
$this->final_prefixes = array_filter( |
| 549 |
$this->final_prefixes, |
| 550 |
function ( $item ) { |
| 551 |
return strlen( $item ) >= 4; |
| 552 |
} |
| 553 |
); |
| 554 |
|
| 555 |
// Discard not valid prefixes. |
| 556 |
$this->final_prefixes = array_diff( $this->final_prefixes, $this->not_valid_prefixes ); |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
/** |
| 561 |
* Checks if the given log item's name has a specific special prefix and updates the possible prefixes list accordingly. |
| 562 |
* |
| 563 |
* @since 1.7.0 |
| 564 |
* |
| 565 |
* @param array $log_item The log item array containing information such as 'name' and 'type'. |
| 566 |
* @param mixed $key The key used to associate the log item's type with the special prefix in the internal structure. |
| 567 |
* @return bool Returns false if a matching special prefix is found and processed, otherwise true. |
| 568 |
*/ |
| 569 |
private function check_prefixes_add_special_prefixes( array $log_item, $key ) { |
| 570 |
$special_prefixes = array( '__', '_', '-' ); |
| 571 |
|
| 572 |
foreach ( $special_prefixes as $special_prefix ) { |
| 573 |
if ( str_starts_with( $log_item['name'], $special_prefix ) && |
| 574 |
( 'abstraction' === $log_item['type'] || $special_prefix === $log_item['name'] ) |
| 575 |
) { |
| 576 |
$this->possible_prefixes[ $special_prefix ][ $key ] = $log_item['type']; |
| 577 |
return false; |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
return true; |
| 582 |
} |
| 583 |
|
| 584 |
/** |
| 585 |
* Processes and filters a log array to detect and handle items without names, |
| 586 |
* and exceptions based on specific conditions. Alters the $log array by |
| 587 |
* removing items that meet exclusion criteria while maintaining a record |
| 588 |
* of unidentified prefix items. |
| 589 |
* |
| 590 |
* @since 1.7.0 |
| 591 |
* |
| 592 |
* @param array $log The log array to process, containing items with keys such as 'name', 'type', and 'text'. |
| 593 |
* @return array The filtered log array after processing and exclusion of specific items. |
| 594 |
*/ |
| 595 |
private function check_prefixes_process_exceptions_from_log( array $log ) { |
| 596 |
$log_without_name = array(); |
| 597 |
foreach ( $log as $key => $item ) { |
| 598 |
// Log anything that doesn't has name. |
| 599 |
// No name or is a function defined by a variable. |
| 600 |
if ( ! isset( $item['name'] ) || 'function_call-PhpParser\Node\Expr\Variable' === $item['type'] || false === $item['name'] ) { |
| 601 |
$log_without_name[ $key ] = $item; |
| 602 |
unset( $log[ $key ] ); |
| 603 |
} |
| 604 |
|
| 605 |
// Exceptions. |
| 606 |
if ( ! empty( $item['name'] ) && 'abstraction' === $item['type'] && str_starts_with( $item['text'], 'class ' ) ) { |
| 607 |
if ( preg_match( '/^wc_gateway_/i', $item['name'] ) ) { |
| 608 |
unset( $log[ $key ] ); // Ignoring classes that begins with WC_Gateway as that's recommended in the WC documentation. |
| 609 |
} |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
if ( ! empty( $log_without_name ) ) { |
| 614 |
$this->log()->add( '0', '# ██ ⚠There are some elements we are not able to detect their prefixes.', 'prefixes_analysis_noname' ); |
| 615 |
$this->log()->merge( $log_without_name, 'prefixes_analysis_noname' ); |
| 616 |
} |
| 617 |
|
| 618 |
return $log; |
| 619 |
} |
| 620 |
|
| 621 |
/** |
| 622 |
* Converts the 'name' value of each item in the provided array to lowercase if it exists. |
| 623 |
* |
| 624 |
* @since 1.7.0 |
| 625 |
* |
| 626 |
* @param array $log An array of items, where each item may contain a 'name' key. |
| 627 |
* @return array The modified array with the 'name' values converted to lowercase. |
| 628 |
*/ |
| 629 |
private function lowercase_item_names( array $log ) { |
| 630 |
foreach ( $log as $key => $item ) { |
| 631 |
if ( isset( $item['name'] ) ) { |
| 632 |
$log[ $key ]['name'] = strtolower( $item['name'] ); |
| 633 |
} |
| 634 |
} |
| 635 |
return $log; |
| 636 |
} |
| 637 |
|
| 638 |
/** |
| 639 |
* Determines the minimum prefix quantity based on the provided log count, |
| 640 |
* using varying thresholds and calculations depending on the value of the input. |
| 641 |
* |
| 642 |
* @since 1.7.0 |
| 643 |
* |
| 644 |
* @param int $log_count The total number of logs for which the minimum prefix quantity is to be calculated. |
| 645 |
* @return int The calculated minimum prefix quantity. |
| 646 |
*/ |
| 647 |
private function calculate_minimum_prefix_quantity( int $log_count ) { |
| 648 |
if ( $log_count <= 2 ) { |
| 649 |
return 1; |
| 650 |
} elseif ( $log_count <= 4 ) { |
| 651 |
return 2; |
| 652 |
} elseif ( $log_count <= 8 ) { |
| 653 |
return 3; |
| 654 |
} elseif ( $log_count < 20 ) { |
| 655 |
return intval( $log_count / 4 ); |
| 656 |
} |
| 657 |
return (int) ( log( $log_count ) * 2 ); |
| 658 |
} |
| 659 |
|
| 660 |
/** |
| 661 |
* Processes the input item to identify potential prefixes and add them to the list of possible prefixes. |
| 662 |
* Handles namespace counting, formats complete names, splits prefixes using a specified separator, |
| 663 |
* and checks for multi-part prefixes. |
| 664 |
* |
| 665 |
* @since 1.7.0 |
| 666 |
* |
| 667 |
* @param array $item Array containing details, including 'name' and 'type', used to process and identify prefixes. |
| 668 |
* @param string $key Key associated with the prefix being processed. |
| 669 |
* @param string $separator Optional separator used to split the item name for extracting prefixes. Default is '_'. |
| 670 |
* @return void |
| 671 |
*/ |
| 672 |
private function check_prefixes_add_possible_prefixes( $item, $key, $separator = '_' ) { |
| 673 |
$complete_name_formatted = str_replace( array( '-', '\\' ), '_', $item['name'] ); |
| 674 |
|
| 675 |
if ( 'namespace' === $item['type'] ) { |
| 676 |
++$this->namespaces_count; |
| 677 |
} |
| 678 |
|
| 679 |
$this->possible_prefixes[ $complete_name_formatted ][ $key ] = $item['type']; |
| 680 |
|
| 681 |
$extract_prefixes = explode( $separator, $complete_name_formatted ); |
| 682 |
|
| 683 |
// Remove special characters at the beginning. |
| 684 |
if ( empty( $extract_prefixes[0] ) ) { |
| 685 |
array_shift( $extract_prefixes ); |
| 686 |
} |
| 687 |
|
| 688 |
$prefix_parts_count = count( $extract_prefixes ); |
| 689 |
|
| 690 |
if ( $prefix_parts_count > 1 ) { |
| 691 |
$this->check_prefixes_add_possible_prefix( $item, $key, $extract_prefixes[0] ); |
| 692 |
if ( $prefix_parts_count > 2 ) { |
| 693 |
$this->check_prefixes_add_possible_prefix( $item, $key, $extract_prefixes[0] . '_' . $extract_prefixes[1] ); |
| 694 |
} |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
/** |
| 699 |
* Updates the possible prefixes by adding the type of the provided item |
| 700 |
* under the specified prefix and key. |
| 701 |
* |
| 702 |
* @since 1.7.0 |
| 703 |
* |
| 704 |
* @param array $item The item containing data, including its type, to be added. |
| 705 |
* @param string $key The key under which the item type is stored for the prefix. |
| 706 |
* @param string $prefix The prefix to which the item type is associated. |
| 707 |
* @return void |
| 708 |
*/ |
| 709 |
private function check_prefixes_add_possible_prefix( $item, $key, $prefix ) { |
| 710 |
$this->possible_prefixes[ $prefix ][ $key ] = $item['type']; |
| 711 |
} |
| 712 |
|
| 713 |
/** |
| 714 |
* Normalizes the given array of common prefixes by performing several tasks including: |
| 715 |
* filtering invalid or warning prefixes, removing redundant incidences, |
| 716 |
* ordering prefixes by length, and removing prefixes that are contained within others. |
| 717 |
* |
| 718 |
* @since 1.7.0 |
| 719 |
* |
| 720 |
* @param array $common_prefixes An associative array where keys are prefixes and values are arrays of incidences. |
| 721 |
* @param array $log A log array that tracks incidences and messages related to prefixes. |
| 722 |
* @return array An associative array containing the updated 'commonprefixes' and 'log' after processing. |
| 723 |
*/ |
| 724 |
private function normalize_prefixes_array( $common_prefixes, $log ) { |
| 725 |
if ( ! empty( $common_prefixes ) ) { |
| 726 |
|
| 727 |
// Remove incidences for just one element, unless it's a namespace, or there is just really one element. |
| 728 |
if ( sizeof( $log ) > 1 ) { |
| 729 |
foreach ( $common_prefixes as $prefix => $array ) { |
| 730 |
if ( sizeof( $array ) <= 1 ) { |
| 731 |
if ( reset( $array ) !== 'namespace' ) { |
| 732 |
unset( $common_prefixes[ $prefix ] ); |
| 733 |
} |
| 734 |
} |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
// Order by bigger prefixes. |
| 739 |
$keys = array_map( 'strlen', array_keys( $common_prefixes ) ); |
| 740 |
array_multisort( $keys, SORT_DESC, $common_prefixes ); |
| 741 |
|
| 742 |
// Remove prefixes that are inside other prefixes. |
| 743 |
foreach ( $common_prefixes as $prefix => $incidences ) { |
| 744 |
foreach ( $common_prefixes as $prefix_2 => $incidences_2 ) { |
| 745 |
if ( $prefix !== $prefix_2 ) { |
| 746 |
if ( str_starts_with( $prefix, $prefix_2 ) ) { // If $prefix starts with $prefix2. |
| 747 |
// Remove short prefixes incidences that are within longer prefixes. |
| 748 |
$within = ! array_diff_assoc( $incidences_2, $incidences ); |
| 749 |
if ( $within ) { |
| 750 |
unset( $common_prefixes[ $prefix_2 ] ); |
| 751 |
} else { |
| 752 |
// Remove longer prefixes incidences that are within shorter prefixes. |
| 753 |
$within = ! array_diff_assoc( $incidences, $incidences_2 ); |
| 754 |
if ( $within ) { |
| 755 |
unset( $common_prefixes[ $prefix ] ); |
| 756 |
} |
| 757 |
} |
| 758 |
} |
| 759 |
} |
| 760 |
} |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
return array( |
| 765 |
'commonprefixes' => $common_prefixes, |
| 766 |
'log' => $log, |
| 767 |
); |
| 768 |
} |
| 769 |
|
| 770 |
/** |
| 771 |
* Identifies and groups prefixes from the provided list that are too similar |
| 772 |
* based on normalization criteria. It returns an associative array containing |
| 773 |
* the group of similar prefixes along with their total incidences. |
| 774 |
* |
| 775 |
* @since 1.7.0 |
| 776 |
* |
| 777 |
* @param array $common_prefixes An associative array where keys are prefixes and values are arrays of incidences for each prefix. |
| 778 |
* @return array An associative array where each key is a prefix and the corresponding value is an array containing similar prefixes and the total number of incidences. |
| 779 |
*/ |
| 780 |
private function get_too_similar_prefixes_array( $common_prefixes ) { |
| 781 |
$similar_prefixes = array(); |
| 782 |
if ( ! empty( $common_prefixes ) ) { |
| 783 |
foreach ( $common_prefixes as $prefix => $incidences ) { |
| 784 |
foreach ( $common_prefixes as $prefix_search => $incidences_search ) { |
| 785 |
if ( $prefix !== $prefix_search && $this->get_prefix_name_normalized( $prefix ) === $this->get_prefix_name_normalized( $prefix_search ) ) { |
| 786 |
if ( empty( $similar_prefixes[ $prefix ] ) ) { |
| 787 |
$similar_prefixes[ $prefix ] = array( |
| 788 |
'prefixes' => array( $prefix ), |
| 789 |
'numberincidences' => count( $incidences ), |
| 790 |
); |
| 791 |
} |
| 792 |
$similar_prefixes[ $prefix ]['prefixes'][] = $prefix_search; |
| 793 |
$similar_prefixes[ $prefix ]['numberincidences'] += count( $incidences_search ); |
| 794 |
} |
| 795 |
} |
| 796 |
} |
| 797 |
} |
| 798 |
return $similar_prefixes; |
| 799 |
} |
| 800 |
|
| 801 |
/** |
| 802 |
* Normalizes a given prefix by removing underscores and hyphens. |
| 803 |
* |
| 804 |
* @since 1.7.0 |
| 805 |
* |
| 806 |
* @param string $prefix The original prefix to be normalized. |
| 807 |
* @return string The normalized prefix. |
| 808 |
*/ |
| 809 |
private function get_prefix_name_normalized( $prefix ) { |
| 810 |
$normalized = preg_replace( '/[\-_]/', '', $prefix ); |
| 811 |
|
| 812 |
return empty( $normalized ) ? $prefix : $normalized; |
| 813 |
} |
| 814 |
} |
| 815 |
|