| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class WordPress\Plugin_Check\Checker\Check_Result |
| 4 |
* |
| 5 |
* @package plugin-check |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WordPress\Plugin_Check\Checker; |
| 9 |
|
| 10 |
/** |
| 11 |
* Result for running checks on a plugin. |
| 12 |
* |
| 13 |
* @since 1.0.0 |
| 14 |
* |
| 15 |
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
| 16 |
*/ |
| 17 |
final class Check_Result { |
| 18 |
|
| 19 |
/** |
| 20 |
* Context for the plugin to check. |
| 21 |
* |
| 22 |
* @since 1.0.0 |
| 23 |
* @var Check_Context |
| 24 |
*/ |
| 25 |
protected $check_context; |
| 26 |
|
| 27 |
/** |
| 28 |
* List of errors. |
| 29 |
* |
| 30 |
* @since 1.0.0 |
| 31 |
* @var array |
| 32 |
*/ |
| 33 |
protected $errors = array(); |
| 34 |
|
| 35 |
/** |
| 36 |
* List of warnings. |
| 37 |
* |
| 38 |
* @since 1.0.0 |
| 39 |
* @var array |
| 40 |
*/ |
| 41 |
protected $warnings = array(); |
| 42 |
|
| 43 |
/** |
| 44 |
* Number of errors. |
| 45 |
* |
| 46 |
* @since 1.0.0 |
| 47 |
* @var int |
| 48 |
*/ |
| 49 |
protected $error_count = 0; |
| 50 |
|
| 51 |
/** |
| 52 |
* Number of warnings. |
| 53 |
* |
| 54 |
* @since 1.0.0 |
| 55 |
* @var int |
| 56 |
*/ |
| 57 |
protected $warning_count = 0; |
| 58 |
|
| 59 |
/** |
| 60 |
* AI analysis results for false positives. |
| 61 |
* |
| 62 |
* @since 2.0.0 |
| 63 |
* @var array |
| 64 |
*/ |
| 65 |
protected $ai_analysis = array(); |
| 66 |
|
| 67 |
/** |
| 68 |
* AI statistics (tokens spent, false positives count, etc.). |
| 69 |
* |
| 70 |
* @since 2.0.0 |
| 71 |
* @var array |
| 72 |
*/ |
| 73 |
protected $ai_stats = array(); |
| 74 |
|
| 75 |
/** |
| 76 |
* Sets the context for the plugin to check. |
| 77 |
* |
| 78 |
* @since 1.0.0 |
| 79 |
* |
| 80 |
* @param Check_Context $check_context Check context instance for the plugin. |
| 81 |
*/ |
| 82 |
public function __construct( Check_Context $check_context ) { |
| 83 |
$this->check_context = $check_context; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Returns the context for the plugin to check. |
| 88 |
* |
| 89 |
* @since 1.0.0 |
| 90 |
* |
| 91 |
* @return Check_Context Plugin context instance. |
| 92 |
*/ |
| 93 |
public function plugin() { |
| 94 |
return $this->check_context; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Adds an error or warning to the respective stack. |
| 99 |
* |
| 100 |
* @since 1.0.0 |
| 101 |
* |
| 102 |
* @param bool $error Whether it is an error message. |
| 103 |
* @param string $message The message. |
| 104 |
* @param array $args { |
| 105 |
* Additional message arguments. |
| 106 |
* |
| 107 |
* @type string $code Violation code according to the message. Default empty string. |
| 108 |
* @type string $file The file in which the message occurred. Default empty string (unknown file). |
| 109 |
* @type int $line The line on which the message occurred. Default 0 (unknown line). |
| 110 |
* @type int $column The column on which the message occurred. Default 0 (unknown column). |
| 111 |
* @type string $link View in code editor link. Default empty string. |
| 112 |
* } |
| 113 |
*/ |
| 114 |
public function add_message( $error, $message, $args = array() ) { |
| 115 |
$defaults = array( |
| 116 |
'code' => '', |
| 117 |
'file' => '', |
| 118 |
'line' => 0, |
| 119 |
'column' => 0, |
| 120 |
'link' => '', |
| 121 |
'docs' => '', |
| 122 |
'severity' => 5, |
| 123 |
); |
| 124 |
|
| 125 |
$data = array_merge( |
| 126 |
array( |
| 127 |
'message' => $message, |
| 128 |
), |
| 129 |
$defaults, |
| 130 |
array_intersect_key( $args, $defaults ) |
| 131 |
); |
| 132 |
|
| 133 |
$file = str_replace( $this->plugin()->path( '/' ), '', $data['file'] ); |
| 134 |
$line = $data['line']; |
| 135 |
$column = $data['column']; |
| 136 |
unset( $data['line'], $data['column'], $data['file'] ); |
| 137 |
|
| 138 |
if ( $error ) { |
| 139 |
if ( ! isset( $this->errors[ $file ] ) ) { |
| 140 |
$this->errors[ $file ] = array(); |
| 141 |
} |
| 142 |
if ( ! isset( $this->errors[ $file ][ $line ] ) ) { |
| 143 |
$this->errors[ $file ][ $line ] = array(); |
| 144 |
} |
| 145 |
if ( ! isset( $this->errors[ $file ][ $line ][ $column ] ) ) { |
| 146 |
$this->errors[ $file ][ $line ][ $column ] = array(); |
| 147 |
} |
| 148 |
$this->errors[ $file ][ $line ][ $column ][] = $data; |
| 149 |
++$this->error_count; |
| 150 |
} else { |
| 151 |
if ( ! isset( $this->warnings[ $file ] ) ) { |
| 152 |
$this->warnings[ $file ] = array(); |
| 153 |
} |
| 154 |
if ( ! isset( $this->warnings[ $file ][ $line ] ) ) { |
| 155 |
$this->warnings[ $file ][ $line ] = array(); |
| 156 |
} |
| 157 |
if ( ! isset( $this->warnings[ $file ][ $line ][ $column ] ) ) { |
| 158 |
$this->warnings[ $file ][ $line ][ $column ] = array(); |
| 159 |
} |
| 160 |
$this->warnings[ $file ][ $line ][ $column ][] = $data; |
| 161 |
++$this->warning_count; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Transforms existing messages. |
| 167 |
* |
| 168 |
* The callback receives the message data and location. Return an array with |
| 169 |
* updated data to keep the message, or false/null to remove it. The returned |
| 170 |
* array may include `error`, `file`, `line`, or `column` to move the message. |
| 171 |
* |
| 172 |
* @since 2.0.0 |
| 173 |
* |
| 174 |
* @param callable $callback Callback to transform each message. |
| 175 |
*/ |
| 176 |
public function transform_messages( callable $callback ) { |
| 177 |
$collections = array( |
| 178 |
true => $this->errors, |
| 179 |
false => $this->warnings, |
| 180 |
); |
| 181 |
|
| 182 |
$this->errors = array(); |
| 183 |
$this->warnings = array(); |
| 184 |
$this->error_count = 0; |
| 185 |
$this->warning_count = 0; |
| 186 |
|
| 187 |
foreach ( $collections as $is_error => $collection ) { |
| 188 |
foreach ( $collection as $file => $lines ) { |
| 189 |
foreach ( $lines as $line => $columns ) { |
| 190 |
foreach ( $columns as $column => $messages ) { |
| 191 |
foreach ( $messages as $message ) { |
| 192 |
$updated = $callback( $message, (bool) $is_error, $file, $line, $column ); |
| 193 |
if ( empty( $updated ) || ! is_array( $updated ) ) { |
| 194 |
continue; |
| 195 |
} |
| 196 |
|
| 197 |
if ( empty( $updated['message'] ) ) { |
| 198 |
continue; |
| 199 |
} |
| 200 |
|
| 201 |
$new_error = array_key_exists( 'error', $updated ) ? (bool) $updated['error'] : (bool) $is_error; |
| 202 |
$new_file = array_key_exists( 'file', $updated ) ? (string) $updated['file'] : (string) $file; |
| 203 |
$new_line = array_key_exists( 'line', $updated ) ? (int) $updated['line'] : (int) $line; |
| 204 |
$new_column = array_key_exists( 'column', $updated ) ? (int) $updated['column'] : (int) $column; |
| 205 |
|
| 206 |
unset( $updated['error'], $updated['file'], $updated['line'], $updated['column'] ); |
| 207 |
$updated['file'] = $new_file; |
| 208 |
$updated['line'] = $new_line; |
| 209 |
$updated['column'] = $new_column; |
| 210 |
|
| 211 |
$this->add_message( $new_error, $updated['message'], $updated ); |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Returns all errors. |
| 221 |
* |
| 222 |
* @since 1.0.0 |
| 223 |
* |
| 224 |
* @return array All errors with their data. |
| 225 |
*/ |
| 226 |
public function get_errors() { |
| 227 |
return $this->errors; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Returns all warnings. |
| 232 |
* |
| 233 |
* @since 1.0.0 |
| 234 |
* |
| 235 |
* @return array All warnings with their data. |
| 236 |
*/ |
| 237 |
public function get_warnings() { |
| 238 |
return $this->warnings; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Returns the number of errors. |
| 243 |
* |
| 244 |
* @since 1.0.0 |
| 245 |
* |
| 246 |
* @return int Number of errors found. |
| 247 |
*/ |
| 248 |
public function get_error_count() { |
| 249 |
return $this->error_count; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Returns the number of warnings. |
| 254 |
* |
| 255 |
* @since 1.0.0 |
| 256 |
* |
| 257 |
* @return int Number of warnings found. |
| 258 |
*/ |
| 259 |
public function get_warning_count() { |
| 260 |
return $this->warning_count; |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Sets AI analysis results. |
| 265 |
* |
| 266 |
* @since 2.0.0 |
| 267 |
* |
| 268 |
* @param array $analysis AI analysis results. |
| 269 |
*/ |
| 270 |
public function set_ai_analysis( array $analysis ) { |
| 271 |
$this->ai_analysis = $analysis; |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Returns AI analysis results. |
| 276 |
* |
| 277 |
* @since 2.0.0 |
| 278 |
* |
| 279 |
* @return array AI analysis results. |
| 280 |
*/ |
| 281 |
public function get_ai_analysis() { |
| 282 |
return $this->ai_analysis; |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Sets AI statistics. |
| 287 |
* |
| 288 |
* @since 2.0.0 |
| 289 |
* |
| 290 |
* @param array $stats AI statistics. |
| 291 |
*/ |
| 292 |
public function set_ai_stats( array $stats ) { |
| 293 |
$this->ai_stats = $stats; |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Returns AI statistics. |
| 298 |
* |
| 299 |
* @since 2.0.0 |
| 300 |
* |
| 301 |
* @return array AI statistics. |
| 302 |
*/ |
| 303 |
public function get_ai_stats() { |
| 304 |
return $this->ai_stats; |
| 305 |
} |
| 306 |
} |
| 307 |
|