Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
UserVisibleException
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
6 / 6
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getPosition
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMessageForLogs
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMessageObj
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 toArray
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 fromArray
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Extension\AbuseFilter\Parser\Exception;
4
5use 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 */
11class 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    /**
33     * @return int
34     */
35    public function getPosition(): int {
36        return $this->mPosition;
37    }
38
39    /**
40     * Returns the error message for use in logs
41     *
42     * @return string
43     */
44    public function getMessageForLogs(): string {
45        return "ID: {$this->mExceptionID}; position: {$this->mPosition}; params: " . implode( ', ', $this->mParams );
46    }
47
48    /**
49     * @return Message
50     */
51    public function getMessageObj(): Message {
52        // Give grep a chance to find the usages:
53        // abusefilter-exception-unexpectedatend, abusefilter-exception-expectednotfound
54        // abusefilter-exception-unrecognisedkeyword, abusefilter-exception-unexpectedtoken
55        // abusefilter-exception-unclosedstring, abusefilter-exception-invalidoperator
56        // abusefilter-exception-unrecognisedtoken, abusefilter-exception-noparams
57        // abusefilter-exception-dividebyzero, abusefilter-exception-unrecognisedvar
58        // abusefilter-exception-notenoughargs, abusefilter-exception-regexfailure
59        // abusefilter-exception-overridebuiltin, abusefilter-exception-outofbounds
60        // abusefilter-exception-notarray, abusefilter-exception-unclosedcomment
61        // abusefilter-exception-invalidiprange, abusefilter-exception-disabledvar
62        // abusefilter-exception-variablevariable, abusefilter-exception-toomanyargs
63        // abusefilter-exception-negativeoffset, abusefilter-exception-unusedvars
64        // abusefilter-exception-unknownfunction, abusefilter-exception-usebuiltin
65        return new Message(
66            'abusefilter-exception-' . $this->mExceptionID,
67            [ $this->mPosition, ...$this->mParams ]
68        );
69    }
70
71    /**
72     * Serialize data for edit stash
73     * @return array
74     */
75    public function toArray(): array {
76        return [
77            'class' => static::class,
78            'exceptionID' => $this->mExceptionID,
79            'position' => $this->mPosition,
80            'params' => $this->mParams,
81        ];
82    }
83
84    /**
85     * Deserialize data from edit stash
86     * @param array $value
87     * @return static
88     */
89    public static function fromArray( array $value ) {
90        $cls = $value['class'];
91        return new $cls( $value['exceptionID'], $value['position'], $value['params'] );
92    }
93
94}