Completed
Push — master ( b06bec...8d39d6 )
by Xeriab
03:49
created

ParseException::__construct()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 19
nc 64
nop 1
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;
0 ignored issues
show
Bug introduced by
The property parsedFile does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
39
        $this->parsedLine = $lineNo;
0 ignored issues
show
Bug introduced by
The property parsedLine does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
40
        $this->snippet = $snippet;
0 ignored issues
show
Bug introduced by
The property snippet does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The variable $snippet does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
41
        $this->rawMessage = $message;
0 ignored issues
show
Bug introduced by
The property rawMessage does not seem to exist. Did you mean message?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
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($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($parsedLine = 0)
123
    {
124
        $this->parsedLine = $parsedLine;
125
126
        $this->updateRepr();
127
    }
128
129
    private function updateRepr()
130
    {
131
        $this->message = $this->rawMessage;
0 ignored issues
show
Bug introduced by
The property rawMessage does not seem to exist. Did you mean message?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
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