Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
18 / 18 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| UserVisibleException | |
100.00% |
18 / 18 |
|
100.00% |
6 / 6 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getPosition | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMessageForLogs | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMessageObj | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| fromArray | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Extension\AbuseFilter\Parser\Exception; |
| 4 | |
| 5 | use MediaWiki\Message\Message; |
| 6 | |
| 7 | /** |
| 8 | * Exceptions that we might conceivably want to report to ordinary users |
| 9 | * (i.e. exceptions that don't represent bugs in the extension itself) |
| 10 | */ |
| 11 | class UserVisibleException extends ExceptionBase { |
| 12 | /** @var string */ |
| 13 | public $mExceptionID; |
| 14 | /** @var int */ |
| 15 | protected $mPosition; |
| 16 | /** @var array */ |
| 17 | protected $mParams; |
| 18 | |
| 19 | /** |
| 20 | * @param string $exception_id |
| 21 | * @param int $position |
| 22 | * @param array $params |
| 23 | */ |
| 24 | public function __construct( $exception_id, $position, $params ) { |
| 25 | $this->mExceptionID = $exception_id; |
| 26 | $this->mPosition = $position; |
| 27 | $this->mParams = $params; |
| 28 | |
| 29 | parent::__construct( $exception_id ); |
| 30 | } |
| 31 | |
| 32 | public function getPosition(): int { |
| 33 | return $this->mPosition; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Returns the error message for use in logs |
| 38 | */ |
| 39 | public function getMessageForLogs(): string { |
| 40 | return "ID: {$this->mExceptionID}; position: {$this->mPosition}; params: " . implode( ', ', $this->mParams ); |
| 41 | } |
| 42 | |
| 43 | public function getMessageObj(): Message { |
| 44 | // Give grep a chance to find the usages: |
| 45 | // abusefilter-exception-unexpectedatend, abusefilter-exception-expectednotfound |
| 46 | // abusefilter-exception-unrecognisedkeyword, abusefilter-exception-unexpectedtoken |
| 47 | // abusefilter-exception-unclosedstring, abusefilter-exception-invalidoperator |
| 48 | // abusefilter-exception-unrecognisedtoken, abusefilter-exception-noparams |
| 49 | // abusefilter-exception-dividebyzero, abusefilter-exception-unrecognisedvar |
| 50 | // abusefilter-exception-notenoughargs, abusefilter-exception-regexfailure |
| 51 | // abusefilter-exception-overridebuiltin, abusefilter-exception-outofbounds |
| 52 | // abusefilter-exception-notarray, abusefilter-exception-unclosedcomment |
| 53 | // abusefilter-exception-invalidiprange, abusefilter-exception-disabledvar |
| 54 | // abusefilter-exception-variablevariable, abusefilter-exception-toomanyargs |
| 55 | // abusefilter-exception-negativeoffset, abusefilter-exception-unusedvars |
| 56 | // abusefilter-exception-unknownfunction, abusefilter-exception-usebuiltin |
| 57 | return new Message( |
| 58 | 'abusefilter-exception-' . $this->mExceptionID, |
| 59 | [ $this->mPosition, ...$this->mParams ] |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Serialize data for edit stash |
| 65 | */ |
| 66 | public function toArray(): array { |
| 67 | return [ |
| 68 | 'class' => static::class, |
| 69 | 'exceptionID' => $this->mExceptionID, |
| 70 | 'position' => $this->mPosition, |
| 71 | 'params' => $this->mParams, |
| 72 | ]; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Deserialize data from edit stash |
| 77 | * @param array $value |
| 78 | * @return static |
| 79 | */ |
| 80 | public static function fromArray( array $value ) { |
| 81 | $cls = $value['class']; |
| 82 | return new $cls( $value['exceptionID'], $value['position'], $value['params'] ); |
| 83 | } |
| 84 | |
| 85 | } |