RuntimeException::setParsedFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/**
4
 * Konfig.
5
 *
6
 * Yet another simple configuration loader library.
7
 *
8
 * PHP version 5
9
 *
10
 * @category Library
11
 * @package  Konfig
12
 * @author   Xeriab Nabil (aka KodeBurner) <[email protected]>
13
 * @license  https://raw.github.com/xeriab/konfig/master/LICENSE MIT
14
 * @link     https://xeriab.github.io/projects/konfig
15
 */
16
17
namespace Exen\Konfig\Exception;
18
19
/**
20
 * Runtime exception class
21
 *
22
 * @category Exception.
23
 * @package  Konfig
24
 * @author   Xeriab Nabil (aka KodeBurner) <[email protected]>
25
 * @license  https://raw.github.com/xeriab/konfig/master/LICENSE MIT
26
 * @link     https://xeriab.github.io/projects/konfig
27
 */
28
class RuntimeException extends \RuntimeException implements ExceptionInterface
29
{
30
    /**
31
     * Path to the parsed file.
32
     *
33
     * @var string $parsedFile
34
     *
35
     * @since 0.2.8
36
     */
37
    private $parsedFile = null;
38
39
    /**
40
     * Parsed line number.
41
     *
42
     * @var int $parsedLine
43
     *
44
     * @since 0.2.8
45
     */
46
    private $parsedLine = 0;
47
48
    /**
49
     * Snippet.
50
     *
51
     * @var string|null $snippet
52
     *
53
     * @since 0.2.8
54
     */
55
    private $snippet = null;
56
57
    /**
58
     * Raw error message.
59
     *
60
     * @var string $rawMessage
61
     *
62
     * @since 0.2.8
63
     */
64
    private $rawMessage = null;
65
66
    /**
67
     * Constructor.
68
     *
69
     * @param string          $message     Error message
70
     * @param int             $parsed_line Line where the error occurred
71
     * @param string|int|null $snippet     Snippet of code near the problem
72
     * @param string          $parsed_file File name where the error occurred
73
     * @param Exception       $previous    The previous exception
74
     *
75
     * @codeCoverageIgnore
76
     */
77
    public function __construct(
78
        $message,
79
        $parsed_line = -1,
80
        $snippet = null,
81
        $parsed_file = null,
82
        Exception $previous = null
83
    ) {
84
        $this->parsedFile = $parsed_file;
85
        $this->parsedLine = $parsed_line;
86
        $this->snippet = $snippet;
0 ignored issues
show
Documentation Bug introduced by
It seems like $snippet can also be of type integer. However, the property $snippet is declared as type string|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
87
        $this->rawMessage = $message;
88
89
        $this->updateRepr();
90
91
        parent::__construct($this->message, 0, $previous);
92
    }
93
94
    /**
95
     * Gets the snippet of code near the error.
96
     *
97
     * @return             string The snippet of code
98
     * @codeCoverageIgnore
99
     */
100
    public function getSnippet()
101
    {
102
        return $this->snippet;
103
    }
104
105
    /**
106
     * Sets the snippet of code near the error.
107
     *
108
     * @param string $snippet The code snippet
109
     *
110
     * @return             void Void
111
     * @codeCoverageIgnore
112
     */
113
    public function setSnippet($snippet)
114
    {
115
        $this->snippet = $snippet;
116
117
        $this->updateRepr();
118
    }
119
120
    /**
121
     * Gets the filename where the error occurred.
122
     *
123
     * This method returns null if a string is parsed.
124
     *
125
     * @return             string The filename
126
     * @codeCoverageIgnore
127
     */
128
    public function getParsedFile()
129
    {
130
        return $this->parsedFile;
131
    }
132
133
    /**
134
     * Sets the filename where the error occurred.
135
     *
136
     * @param string $parsed_file The filename
137
     *
138
     * @return             void Void
139
     * @codeCoverageIgnore
140
     */
141
    public function setParsedFile($parsed_file = null)
142
    {
143
        $this->parsedFile = $parsed_file;
144
145
        $this->updateRepr();
146
    }
147
148
    /**
149
     * Gets the line where the error occurred.
150
     *
151
     * @return int The file line
152
     *
153
     * @codeCoverageIgnore
154
     */
155
    public function getParsedLine()
156
    {
157
        return $this->parsedLine;
158
    }
159
160
    /**
161
     * Sets the line where the error occurred.
162
     *
163
     * @param int $parsed_line The file line
164
     *
165
     * @return             void Void
166
     * @codeCoverageIgnore
167
     */
168
    public function setParsedLine($parsed_line = 0)
169
    {
170
        $this->parsedLine = $parsed_line;
171
172
        $this->updateRepr();
173
    }
174
175
    /**
176
     * Update message.
177
     *
178
     * @return             void Void
179
     * @codeCoverageIgnore
180
     */
181
    private function updateRepr()
182
    {
183
        $this->message = $this->rawMessage;
184
185
        $dot = false;
186
187
        if ('.' === substr($this->message, -1)) {
188
            $this->message = substr($this->message, 0, -1);
189
            $dot = true;
190
        }
191
192
        if (null !== $this->parsedFile) {
193
            $this->message .= sprintf(
194
                ' in %s',
195
                json_encode(
196
                    $this->parsedFile,
197
                    JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
198
                )
199
            );
200
        }
201
202
        if ($this->parsedLine >= 0) {
203
            $this->message .= sprintf(' at line %d', $this->parsedLine);
204
        }
205
206
        if ($this->snippet) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->snippet of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
207
            $this->message .= sprintf(' (near "%s")', $this->snippet);
208
        }
209
210
        if ($dot) {
211
            $this->message .= '.';
212
        }
213
    }
214
}
215
216
// END OF ./src/Exception/RuntimeException.php FILE
217