RegexException::getShortMessage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Gobie\Regex\Wrappers;
4
5
class RegexException extends \RuntimeException
6
{
7
8
    /**
9
     * If pattern is provided, it is appended to the message.
10
     *
11
     * @param string               $message Message
12
     * @param int|null             $code    Code
13
     * @param string|string[]|null $pattern Pattern
14
     */
15 82
    public function __construct($message, $code = null, $pattern = null)
16
    {
17 82
        if (!$message) {
18 3
            $message = 'Unknown error';
19
        }
20
21 82
        if ($pattern !== null) {
22 78
            $message .= '; pattern: ' . \implode(', ', (array) $pattern);
23
        }
24
25 82
        parent::__construct($message, $code);
26 82
    }
27
28
    /**
29
     * Return exception message without function name.
30
     *
31
     * Usual exception message looks like this:
32
     * <pre>
33
     * preg_match(): Some error occurred
34
     * </pre>
35
     * So we strip the "function(): " part.
36
     *
37
     * @return string
38
     */
39 76
    public function getShortMessage()
40
    {
41 76
        $msg = $this->getMessage();
42 76
        if (\strpos($msg, '(): ') !== false) {
43 57
            list(, $msg) = \explode('(): ', $msg);
44
        }
45
46 76
        return $msg;
47
    }
48
}
49