| 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 |
final class Check_Result { |
| 16 |
|
| 17 |
/** |
| 18 |
* Context for the plugin to check. |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
* @var Check_Context |
| 22 |
*/ |
| 23 |
protected $check_context; |
| 24 |
|
| 25 |
/** |
| 26 |
* List of errors. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
* @var array |
| 30 |
*/ |
| 31 |
protected $errors = array(); |
| 32 |
|
| 33 |
/** |
| 34 |
* List of warnings. |
| 35 |
* |
| 36 |
* @since 1.0.0 |
| 37 |
* @var array |
| 38 |
*/ |
| 39 |
protected $warnings = array(); |
| 40 |
|
| 41 |
/** |
| 42 |
* Number of errors. |
| 43 |
* |
| 44 |
* @since 1.0.0 |
| 45 |
* @var int |
| 46 |
*/ |
| 47 |
protected $error_count = 0; |
| 48 |
|
| 49 |
/** |
| 50 |
* Number of warnings. |
| 51 |
* |
| 52 |
* @since 1.0.0 |
| 53 |
* @var int |
| 54 |
*/ |
| 55 |
protected $warning_count = 0; |
| 56 |
|
| 57 |
/** |
| 58 |
* Sets the context for the plugin to check. |
| 59 |
* |
| 60 |
* @since 1.0.0 |
| 61 |
* |
| 62 |
* @param Check_Context $check_context Check context instance for the plugin. |
| 63 |
*/ |
| 64 |
public function __construct( Check_Context $check_context ) { |
| 65 |
$this->check_context = $check_context; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Returns the context for the plugin to check. |
| 70 |
* |
| 71 |
* @since 1.0.0 |
| 72 |
* |
| 73 |
* @return Check_Context Plugin context instance. |
| 74 |
*/ |
| 75 |
public function plugin() { |
| 76 |
return $this->check_context; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Adds an error or warning to the respective stack. |
| 81 |
* |
| 82 |
* @since 1.0.0 |
| 83 |
* |
| 84 |
* @param bool $error Whether it is an error message. |
| 85 |
* @param string $message The message. |
| 86 |
* @param array $args { |
| 87 |
* Additional message arguments. |
| 88 |
* |
| 89 |
* @type string $code Violation code according to the message. Default empty string. |
| 90 |
* @type string $file The file in which the message occurred. Default empty string (unknown file). |
| 91 |
* @type int $line The line on which the message occurred. Default 0 (unknown line). |
| 92 |
* @type int $column The column on which the message occurred. Default 0 (unknown column). |
| 93 |
* @type string $link View in code editor link. Default empty string. |
| 94 |
* } |
| 95 |
*/ |
| 96 |
public function add_message( $error, $message, $args = array() ) { |
| 97 |
$defaults = array( |
| 98 |
'code' => '', |
| 99 |
'file' => '', |
| 100 |
'line' => 0, |
| 101 |
'column' => 0, |
| 102 |
'link' => '', |
| 103 |
'docs' => '', |
| 104 |
'severity' => 5, |
| 105 |
); |
| 106 |
|
| 107 |
$data = array_merge( |
| 108 |
array( |
| 109 |
'message' => $message, |
| 110 |
), |
| 111 |
$defaults, |
| 112 |
array_intersect_key( $args, $defaults ) |
| 113 |
); |
| 114 |
|
| 115 |
$file = str_replace( $this->plugin()->path( '/' ), '', $data['file'] ); |
| 116 |
$line = $data['line']; |
| 117 |
$column = $data['column']; |
| 118 |
unset( $data['line'], $data['column'], $data['file'] ); |
| 119 |
|
| 120 |
if ( $error ) { |
| 121 |
if ( ! isset( $this->errors[ $file ] ) ) { |
| 122 |
$this->errors[ $file ] = array(); |
| 123 |
} |
| 124 |
if ( ! isset( $this->errors[ $file ][ $line ] ) ) { |
| 125 |
$this->errors[ $file ][ $line ] = array(); |
| 126 |
} |
| 127 |
if ( ! isset( $this->errors[ $file ][ $line ][ $column ] ) ) { |
| 128 |
$this->errors[ $file ][ $line ][ $column ] = array(); |
| 129 |
} |
| 130 |
$this->errors[ $file ][ $line ][ $column ][] = $data; |
| 131 |
++$this->error_count; |
| 132 |
} else { |
| 133 |
if ( ! isset( $this->warnings[ $file ] ) ) { |
| 134 |
$this->warnings[ $file ] = array(); |
| 135 |
} |
| 136 |
if ( ! isset( $this->warnings[ $file ][ $line ] ) ) { |
| 137 |
$this->warnings[ $file ][ $line ] = array(); |
| 138 |
} |
| 139 |
if ( ! isset( $this->warnings[ $file ][ $line ][ $column ] ) ) { |
| 140 |
$this->warnings[ $file ][ $line ][ $column ] = array(); |
| 141 |
} |
| 142 |
$this->warnings[ $file ][ $line ][ $column ][] = $data; |
| 143 |
++$this->warning_count; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Returns all errors. |
| 149 |
* |
| 150 |
* @since 1.0.0 |
| 151 |
* |
| 152 |
* @return array All errors with their data. |
| 153 |
*/ |
| 154 |
public function get_errors() { |
| 155 |
return $this->errors; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Returns all warnings. |
| 160 |
* |
| 161 |
* @since 1.0.0 |
| 162 |
* |
| 163 |
* @return array All warnings with their data. |
| 164 |
*/ |
| 165 |
public function get_warnings() { |
| 166 |
return $this->warnings; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Returns the number of errors. |
| 171 |
* |
| 172 |
* @since 1.0.0 |
| 173 |
* |
| 174 |
* @return int Number of errors found. |
| 175 |
*/ |
| 176 |
public function get_error_count() { |
| 177 |
return $this->error_count; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Returns the number of warnings. |
| 182 |
* |
| 183 |
* @since 1.0.0 |
| 184 |
* |
| 185 |
* @return int Number of warnings found. |
| 186 |
*/ |
| 187 |
public function get_warning_count() { |
| 188 |
return $this->warning_count; |
| 189 |
} |
| 190 |
} |
| 191 |
|