Completed
Push — master ( 5c24e5...c105d3 )
by Xeriab
04:23
created

ParseException   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 1
dl 0
loc 145
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getSnippet() 0 4 1
A setSnippet() 0 6 1
A getParsedFile() 0 4 1
A setParsedFile() 0 6 1
A getParsedLine() 0 4 1
A setParsedLine() 0 6 1
B updateRepr() 0 30 6
D __construct() 0 33 8
1
<?php
2
3
/**
4
 * Konfig
5
 *
6
 * Yet another simple configuration loader library.
7
 *
8
 * @author  Xeriab Nabil (aka KodeBurner) <[email protected]>
9
 * @license https://raw.github.com/xeriab/konfig/master/LICENSE MIT
10
 * @link    https://xeriab.github.io/projects/konfig
11
 */
12
13
namespace Exen\Konfig\Exception;
14
15
class ParseException extends ErrorException
16
{
17
    private $parsedFile;
18
    private $parsedLine;
19
    private $snippet;
20
    private $rawMessage;
21
22
    /**
23
     * Constructor.
24
     *
25
     * @param array      $error    The error array
26
     */
27
    public function __construct(array $error)
28
    {
29
        $message   = isset($error['message']) ? $error['message'] : 'There was an error parsing the file';
30
        $code      = isset($error['code']) ? $error['code'] : 0;
31
        $severity  = isset($error['type']) ? $error['type'] : 1;
32
        $fileName  = isset($error['file']) ? $error['file'] : __FILE__;
33
        $lineNo    = isset($error['line']) ? $error['line'] : __LINE__;
34
        $exception = isset($error['exception']) ? $error['exception'] : null;
35
        $snippet   = isset($error['snippet']) ? $error['snippet'] : null;
36
        // $previous  = isset($error['previous']) ? $error['previous'] : null;
37
38
        $this->parsedFile = $fileName;
39
        $this->parsedLine = $lineNo;
40
        $this->snippet = $snippet;
41
        $this->rawMessage = $message;
42
43
        $this->updateRepr();
44
45
        // parent::__construct(
46
        //     $this->message,
47
        //     0,
48
        //     $previous
49
        // );
50
51
        parent::__construct(
52
            $message,
53
            $code,
54
            $severity,
55
            $fileName,
56
            $lineNo,
57
            $exception
58
        );
59
    }
60
61
    /**
62
     * Gets the snippet of code near the error.
63
     *
64
     * @return string The snippet of code
65
     */
66
    public function getSnippet()
67
    {
68
        return $this->snippet;
69
    }
70
71
    /**
72
     * Sets the snippet of code near the error.
73
     *
74
     * @param string $snippet The code snippet
75
     */
76
    public function setSnippet($snippet)
77
    {
78
        $this->snippet = $snippet;
79
80
        $this->updateRepr();
81
    }
82
83
    /**
84
     * Gets the filename where the error occurred.
85
     *
86
     * This method returns null if a string is parsed.
87
     *
88
     * @return string The filename
89
     */
90
    public function getParsedFile()
91
    {
92
        return $this->parsedFile;
93
    }
94
95
    /**
96
     * Sets the filename where the error occurred.
97
     *
98
     * @param string $parsedFile The filename
99
     */
100
    public function setParsedFile(string $parsedFile = null)
101
    {
102
        $this->parsedFile = $parsedFile;
103
104
        $this->updateRepr();
105
    }
106
107
    /**
108
     * Gets the line where the error occurred.
109
     *
110
     * @return int The file line
111
     */
112
    public function getParsedLine()
113
    {
114
        return $this->parsedLine;
115
    }
116
117
    /**
118
     * Sets the line where the error occurred.
119
     *
120
     * @param int $parsedLine The file line
121
     */
122
    public function setParsedLine(int $parsedLine = 0)
123
    {
124
        $this->parsedLine = $parsedLine;
125
126
        $this->updateRepr();
127
    }
128
129
    private function updateRepr()
130
    {
131
        $this->message = $this->rawMessage;
132
133
        $dot = false;
134
135
        if ('.' === substr($this->message, -1)) {
136
            $this->message = substr($this->message, 0, -1);
137
            $dot = true;
138
        }
139
140
        if (null !== $this->parsedFile) {
141
            $this->message .= sprintf(
142
                ' in %s',
143
                json_encode($this->parsedFile, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
144
            );
145
        }
146
147
        if ($this->parsedLine >= 0) {
148
            $this->message .= sprintf(' at line %d', $this->parsedLine);
149
        }
150
151
        if ($this->snippet) {
152
            $this->message .= sprintf(' (near "%s")', $this->snippet);
153
        }
154
155
        if ($dot) {
156
            $this->message .= '.';
157
        }
158
    }
159
}
160
161
// END OF ./src/Exception/ParseException.php FILE
162