These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php namespace Arcanedev\LogViewer\Entities; |
||
2 | |||
3 | use Carbon\Carbon; |
||
4 | use Illuminate\Contracts\Support\Arrayable; |
||
5 | use Illuminate\Contracts\Support\Jsonable; |
||
6 | use JsonSerializable; |
||
7 | |||
8 | /** |
||
9 | * Class LogEntry |
||
10 | * |
||
11 | * @package Arcanedev\LogViewer\Entities |
||
12 | * @author ARCANEDEV <[email protected]> |
||
13 | */ |
||
14 | class LogEntry implements Arrayable, Jsonable, JsonSerializable |
||
15 | { |
||
16 | /* ----------------------------------------------------------------- |
||
17 | | Properties |
||
18 | | ----------------------------------------------------------------- |
||
19 | */ |
||
20 | |||
21 | /** @var string */ |
||
22 | public $env; |
||
23 | |||
24 | /** @var string */ |
||
25 | public $level; |
||
26 | |||
27 | /** @var \Carbon\Carbon */ |
||
28 | public $datetime; |
||
29 | |||
30 | /** @var string */ |
||
31 | public $header; |
||
32 | |||
33 | /** @var string */ |
||
34 | public $stack; |
||
35 | |||
36 | /* ----------------------------------------------------------------- |
||
37 | | Constructor |
||
38 | | ----------------------------------------------------------------- |
||
39 | */ |
||
40 | |||
41 | /** |
||
42 | * Construct the log entry instance. |
||
43 | * |
||
44 | * @param string $level |
||
45 | * @param string $header |
||
46 | * @param string $stack |
||
47 | */ |
||
48 | 243 | public function __construct($level, $header, $stack) |
|
49 | { |
||
50 | 243 | $this->setLevel($level); |
|
51 | 243 | $this->setHeader($header); |
|
52 | 243 | $this->setStack($stack); |
|
53 | 243 | } |
|
54 | |||
55 | /* ----------------------------------------------------------------- |
||
56 | | Getters & Setters |
||
57 | | ----------------------------------------------------------------- |
||
58 | */ |
||
59 | |||
60 | /** |
||
61 | * Set the entry level. |
||
62 | * |
||
63 | * @param string $level |
||
64 | * |
||
65 | * @return self |
||
66 | */ |
||
67 | 243 | private function setLevel($level) |
|
68 | { |
||
69 | 243 | $this->level = $level; |
|
70 | |||
71 | 243 | return $this; |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * Set the entry header. |
||
76 | * |
||
77 | * @param string $header |
||
78 | * |
||
79 | * @return self |
||
80 | */ |
||
81 | 243 | private function setHeader($header) |
|
82 | { |
||
83 | 243 | $this->setDatetime($this->extractDatetime($header)); |
|
84 | |||
85 | 243 | $header = $this->cleanHeader($header); |
|
86 | |||
87 | 243 | if (preg_match('/^[a-z]+.[A-Z]+:/', $header, $out)) { |
|
88 | 243 | $this->setEnv($out[0]); |
|
89 | 243 | $header = trim(str_replace($out[0], '', $header)); |
|
90 | } |
||
91 | |||
92 | 243 | $this->header = $header; |
|
93 | |||
94 | 243 | return $this; |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * Set entry environment. |
||
99 | * |
||
100 | * @param string $env |
||
101 | * |
||
102 | * @return self |
||
103 | */ |
||
104 | 243 | private function setEnv($env) |
|
105 | { |
||
106 | 243 | $this->env = head(explode('.', $env)); |
|
107 | |||
108 | 243 | return $this; |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * Set the entry date time. |
||
113 | * |
||
114 | * @param string $datetime |
||
115 | * |
||
116 | * @return \Arcanedev\LogViewer\Entities\LogEntry |
||
117 | */ |
||
118 | 243 | private function setDatetime($datetime) |
|
119 | { |
||
120 | 243 | $this->datetime = Carbon::createFromFormat('Y-m-d H:i:s', $datetime); |
|
0 ignored issues
–
show
|
|||
121 | |||
122 | 243 | return $this; |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * Set the entry stack. |
||
127 | * |
||
128 | * @param string $stack |
||
129 | * |
||
130 | * @return self |
||
131 | */ |
||
132 | 243 | private function setStack($stack) |
|
133 | { |
||
134 | 243 | $this->stack = $stack; |
|
135 | |||
136 | 243 | return $this; |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * Get translated level name with icon. |
||
141 | * |
||
142 | * @return string |
||
143 | */ |
||
144 | 9 | public function level() |
|
145 | { |
||
146 | 9 | return $this->icon().' '.$this->name(); |
|
147 | } |
||
148 | |||
149 | /** |
||
150 | * Get translated level name. |
||
151 | * |
||
152 | * @return string |
||
153 | */ |
||
154 | 9 | public function name() |
|
155 | { |
||
156 | 9 | return log_levels()->get($this->level); |
|
157 | } |
||
158 | |||
159 | /** |
||
160 | * Get level icon. |
||
161 | * |
||
162 | * @return string |
||
163 | */ |
||
164 | 9 | public function icon() |
|
165 | { |
||
166 | 9 | return log_styler()->icon($this->level); |
|
167 | } |
||
168 | |||
169 | /** |
||
170 | * Get the entry stack. |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | 12 | public function stack() |
|
175 | { |
||
176 | 12 | return trim(htmlentities($this->stack)); |
|
177 | } |
||
178 | |||
179 | /* ----------------------------------------------------------------- |
||
180 | | Check Methods |
||
181 | | ----------------------------------------------------------------- |
||
182 | */ |
||
183 | |||
184 | /** |
||
185 | * Check if same log level. |
||
186 | * |
||
187 | * @param string $level |
||
188 | * |
||
189 | * @return bool |
||
190 | */ |
||
191 | 27 | public function isSameLevel($level) |
|
192 | { |
||
193 | 27 | return $this->level === $level; |
|
194 | } |
||
195 | |||
196 | /* ----------------------------------------------------------------- |
||
197 | | Convert Methods |
||
198 | | ----------------------------------------------------------------- |
||
199 | */ |
||
200 | |||
201 | /** |
||
202 | * Get the log entry as an array. |
||
203 | * |
||
204 | * @return array |
||
205 | */ |
||
206 | 6 | public function toArray() |
|
207 | { |
||
208 | return [ |
||
209 | 6 | 'level' => $this->level, |
|
210 | 6 | 'datetime' => $this->datetime->format('Y-m-d H:i:s'), |
|
211 | 6 | 'header' => $this->header, |
|
212 | 6 | 'stack' => $this->stack |
|
213 | ]; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Convert the log entry to its JSON representation. |
||
218 | * |
||
219 | * @param int $options |
||
220 | * |
||
221 | * @return string |
||
222 | */ |
||
223 | 3 | public function toJson($options = 0) |
|
224 | { |
||
225 | 3 | return json_encode($this->toArray(), $options); |
|
226 | } |
||
227 | |||
228 | /** |
||
229 | * Serialize the log entry object to json data. |
||
230 | * |
||
231 | * @return array |
||
232 | */ |
||
233 | 3 | public function jsonSerialize() |
|
234 | { |
||
235 | 3 | return $this->toArray(); |
|
236 | } |
||
237 | |||
238 | /* ----------------------------------------------------------------- |
||
239 | | Check Methods |
||
240 | | ----------------------------------------------------------------- |
||
241 | */ |
||
242 | |||
243 | /** |
||
244 | * Check if the entry has a stack. |
||
245 | * |
||
246 | * @return bool |
||
247 | */ |
||
248 | 9 | public function hasStack() |
|
249 | { |
||
250 | 9 | return $this->stack !== "\n"; |
|
251 | } |
||
252 | |||
253 | /* ----------------------------------------------------------------- |
||
254 | | Other Methods |
||
255 | | ----------------------------------------------------------------- |
||
256 | */ |
||
257 | |||
258 | /** |
||
259 | * Clean the entry header. |
||
260 | * |
||
261 | * @param string $header |
||
262 | * |
||
263 | * @return string |
||
264 | */ |
||
265 | 243 | private function cleanHeader($header) |
|
266 | { |
||
267 | 243 | return preg_replace('/\['.REGEX_DATETIME_PATTERN.'\][ ]/', '', $header); |
|
268 | } |
||
269 | |||
270 | /** |
||
271 | * Extract datetime from the header. |
||
272 | * |
||
273 | * @param string $header |
||
274 | * |
||
275 | * @return string |
||
276 | */ |
||
277 | 243 | private function extractDatetime($header) |
|
278 | { |
||
279 | 243 | return preg_replace('/^\[('.REGEX_DATETIME_PATTERN.')\].*/', '$1', $header); |
|
280 | } |
||
281 | } |
||
282 |
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 theid
property of an instance of theAccount
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.