Conditions | 1 |
Paths | 1 |
Total Lines | 313 |
Code Lines | 13 |
Lines | 313 |
Ratio | 100 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
159 | View Code Duplication | public function testFullReport() |
|
160 | { |
||
161 | $commandTester = $this->prepareCommand([ |
||
162 | 'report_file' => $this->reportPaths['fix'] . 'clover_log.xml', |
||
163 | '--skip-dir' => '', |
||
164 | '--full-report' => true |
||
165 | ]); |
||
166 | |||
167 | $output = <<<EOT |
||
168 | |||
169 | Clover report generator. |
||
170 | ======================== |
||
171 | |||
172 | [Coverage report file] tests/reports/fixed/clover_log.xml |
||
173 | |||
174 | - 84.211% SimpleLog\\Log |
||
175 | 0:-: <?php |
||
176 | 1:-: |
||
177 | 2:-: namespace SimpleLog; |
||
178 | 3:-: |
||
179 | 4:-: use Psr\\Log\\LoggerInterface; |
||
180 | 5:-: use Psr\\Log\\LoggerTrait; |
||
181 | 6:-: use Psr\\Log\\LogLevel; |
||
182 | 7:-: use Psr\\Log\\InvalidArgumentException; |
||
183 | 8:-: use SimpleLog\\Storage\\StorageInterface; |
||
184 | 9:-: use SimpleLog\\Message\\MessageInterface; |
||
185 | 10:-: |
||
186 | 11:-: class Log implements LogInterface, LoggerInterface |
||
187 | 12:-: { |
||
188 | 13:-: use LoggerTrait; |
||
189 | 14:-: |
||
190 | 15:-: /** |
||
191 | 16:-: * @var array |
||
192 | 17:-: */ |
||
193 | 18:-: protected \$defaultParams = [ |
||
194 | 19:-: 'log_path' => './log', |
||
195 | 20:-: 'level' => 'notice', |
||
196 | 21:-: 'storage' => \\SimpleLog\\Storage\\File::class, |
||
197 | 22:-: 'message' => \\SimpleLog\\Message\\DefaultMessage::class, |
||
198 | 23:-: ]; |
||
199 | 24:-: |
||
200 | 25:-: /** |
||
201 | 26:-: * @var \\SimpleLog\\Storage\\StorageInterface |
||
202 | 27:-: */ |
||
203 | 28:-: protected \$storage; |
||
204 | 29:-: |
||
205 | 30:-: /** |
||
206 | 31:-: * @var array |
||
207 | 32:-: */ |
||
208 | 33:-: protected \$levels = []; |
||
209 | 34:-: |
||
210 | 35:-: /** |
||
211 | 36:-: * @var \\SimpleLog\\Message\\MessageInterface |
||
212 | 37:-: */ |
||
213 | 38:-: protected \$message; |
||
214 | 39:-: |
||
215 | 40:-: /** |
||
216 | 41:-: * @param array \$params |
||
217 | 42:-: * @throws \\ReflectionException |
||
218 | 43:-: */ |
||
219 | 44:9: public function __construct(array \$params = []) |
||
220 | 45:-: { |
||
221 | 46:9: \$this->defaultParams = array_merge(\$this->defaultParams, \$params); |
||
222 | 47:-: |
||
223 | 48:9: \$levels = new \\ReflectionClass(new LogLevel); |
||
224 | 49:9: \$this->levels = \$levels->getConstants(); |
||
225 | 50:-: |
||
226 | 51:9: \$this->reloadStorage(); |
||
227 | 52:9: \$this->reloadMessage(); |
||
228 | 53:9: } |
||
229 | 54:-: |
||
230 | 55:-: /** |
||
231 | 56:-: * log event information into file |
||
232 | 57:-: * |
||
233 | 58:-: * @param array|string|object \$message |
||
234 | 59:-: * @param array \$context |
||
235 | 60:-: * @return \$this |
||
236 | 61:-: */ |
||
237 | 62:8: public function makeLog(\$message, array \$context = []) |
||
238 | 63:-: { |
||
239 | 64:8: \$this->log(\$this->defaultParams['level'], \$message, \$context); |
||
240 | 65:-: |
||
241 | 66:-: return \$this; |
||
242 | 67:-: } |
||
243 | 68:-: |
||
244 | 69:-: /** |
||
245 | 70:-: * @param string \$level |
||
246 | 71:-: * @param string|array|object \$message |
||
247 | 72:-: * @param array \$context |
||
248 | 73:10: * @throws \\Psr\\Log\\InvalidArgumentException |
||
249 | 74:-: */ |
||
250 | 75:10: public function log(\$level, \$message, array \$context = []) |
||
251 | 76:1: { |
||
252 | 77:-: if (!in_array(\$level, \$this->levels, true)) { |
||
253 | 78:-: throw new InvalidArgumentException('Level not defined: ' . \$level); |
||
254 | 79:9: } |
||
255 | 80:9: |
||
256 | 81:8: \$newMessage = \$this->message |
||
257 | 82:-: ->createMessage(\$message, \$context) |
||
258 | 83:8: ->getMessage(); |
||
259 | 84:8: |
||
260 | 85:-: \$this->storage->store(\$newMessage, \$level); |
||
261 | 86:-: } |
||
262 | 87:-: |
||
263 | 88:-: /** |
||
264 | 89:10: * @return \$this |
||
265 | 90:-: */ |
||
266 | 91:10: protected function reloadStorage() |
||
267 | 93:0: { |
||
268 | 94:0: if (\$this->defaultParams['storage'] instanceof StorageInterface) { |
||
269 | 94:-: \$this->storage = \$this->defaultParams['storage']; |
||
270 | 95:-: return \$this; |
||
271 | 96:10: } |
||
272 | 97:10: |
||
273 | 98:-: \$this->storage = new \$this->defaultParams['storage'](\$this->defaultParams); |
||
274 | 99:-: return \$this; |
||
275 | 100:-: } |
||
276 | 101:-: |
||
277 | 102:-: /** |
||
278 | 103:9: * @return \$this |
||
279 | 104:-: */ |
||
280 | 105:9: protected function reloadMessage() |
||
281 | 107:0: { |
||
282 | 108:0: if (\$this->defaultParams['message'] instanceof MessageInterface) { |
||
283 | 108:-: \$this->message = \$this->defaultParams['message']; |
||
284 | 109:-: return \$this; |
||
285 | 110:9: } |
||
286 | 111:9: |
||
287 | 112:-: \$this->message = new \$this->defaultParams['message'](\$this->defaultParams); |
||
288 | 113:-: return \$this; |
||
289 | 114:-: } |
||
290 | 115:-: |
||
291 | 116:-: /** |
||
292 | 117:-: * set log option for all future executions of makeLog |
||
293 | 118:-: * |
||
294 | 119:-: * @param string \$key |
||
295 | 120:-: * @param mixed \$val |
||
296 | 121:3: * @return \$this |
||
297 | 122:-: */ |
||
298 | 123:3: public function setOption(\$key, \$val) |
||
299 | 124:3: { |
||
300 | 125:-: \$this->defaultParams[\$key] = \$val; |
||
301 | 126:-: return \$this->reloadStorage(); |
||
302 | 127:-: } |
||
303 | 128:-: |
||
304 | 129:-: /** |
||
305 | 130:-: * return all configuration or only given key value |
||
306 | 131:-: * |
||
307 | 132:-: * @param null|string \$key |
||
308 | 133:3: * @return array|mixed |
||
309 | 134:-: */ |
||
310 | 135:3: public function getOption(\$key = null) |
||
311 | 136:1: { |
||
312 | 137:-: if (is_null(\$key)) { |
||
313 | 138:-: return \$this->defaultParams; |
||
314 | 139:3: } |
||
315 | 140:-: |
||
316 | 141:-: return \$this->defaultParams[\$key]; |
||
317 | 142:-: } |
||
318 | 143:-: |
||
319 | 144:-: /** |
||
320 | 145:1: * @return string |
||
321 | 146:-: */ |
||
322 | 147:1: public function getLastMessage() |
||
323 | 148:-: { |
||
324 | 149:-: return \$this->message->getMessage(); |
||
325 | 150:-: } |
||
326 | 151:-: } |
||
327 | 152:-: |
||
328 | |||
329 | - 100% SimpleLog\\LogStatic |
||
330 | 0:-: <?php |
||
331 | 1:-: |
||
332 | 2:-: namespace SimpleLog; |
||
333 | 3:-: |
||
334 | 4:-: class LogStatic |
||
335 | 5:-: { |
||
336 | 6:-: /** |
||
337 | 7:-: * @var Log |
||
338 | 8:-: */ |
||
339 | 9:-: protected static \$instance; |
||
340 | 10:-: |
||
341 | 11:-: /** |
||
342 | 12:-: * log event information into file |
||
343 | 13:-: * |
||
344 | 14:-: * @param string \$level |
||
345 | 15:-: * @param array|string \$message |
||
346 | 16:-: * @param array \$context |
||
347 | 17:-: * @param array \$params |
||
348 | 18:-: */ |
||
349 | 19:-: public static function log(\$level, \$message, array \$context = [], array \$params = []) |
||
350 | 20:1: { |
||
351 | 21:-: self::init(\$params); |
||
352 | 22:1: self::\$instance->log(\$level, \$message, \$context); |
||
353 | 23:1: } |
||
354 | 24:-: |
||
355 | 25:-: /** |
||
356 | 26:-: * log event information into file |
||
357 | 27:-: * |
||
358 | 28:-: * @param array|string \$message |
||
359 | 29:-: * @param array \$context |
||
360 | 30:-: * @param array \$params |
||
361 | 31:-: */ |
||
362 | 32:-: public static function makeLog(\$message, array \$context = [], array \$params = []) |
||
363 | 33:-: { |
||
364 | 34:1: self::init(\$params); |
||
365 | 35:-: self::\$instance->makeLog(\$message, \$context); |
||
366 | 36:1: } |
||
367 | 37:1: |
||
368 | 38:-: /** |
||
369 | 39:-: * set log option for all future executions of makeLog |
||
370 | 40:-: * |
||
371 | 41:-: * @param string \$key |
||
372 | 42:-: * @param mixed \$val |
||
373 | 43:-: * @return Log |
||
374 | 44:-: */ |
||
375 | 45:-: public static function setOption(\$key, \$val) |
||
376 | 46:-: { |
||
377 | 47:2: self::init(); |
||
378 | 48:-: return self::\$instance->setOption(\$key, \$val); |
||
379 | 49:2: } |
||
380 | 50:2: |
||
381 | 51:-: /** |
||
382 | 52:-: * return all configuration or only given key value |
||
383 | 53:-: * |
||
384 | 54:-: * @param null|string \$key |
||
385 | 55:-: * @return array|mixed |
||
386 | 56:-: */ |
||
387 | 57:-: public static function getOption(\$key = null) |
||
388 | 58:-: { |
||
389 | 59:2: self::init(); |
||
390 | 60:-: return self::\$instance->getOption(\$key); |
||
391 | 61:2: } |
||
392 | 62:2: |
||
393 | 63:-: /** |
||
394 | 64:-: * create Log object if not exists |
||
395 | 65:-: * |
||
396 | 66:-: * @param array \$params |
||
397 | 67:-: */ |
||
398 | 68:-: protected static function init(array \$params = []) |
||
399 | 69:-: { |
||
400 | 70:2: if (is_null(self::\$instance)) { |
||
401 | 71:-: self::\$instance = new Log(\$params); |
||
402 | 72:2: } |
||
403 | 73:1: } |
||
404 | 74:-: } |
||
405 | 75:2: |
||
406 | |||
407 | - 0% SimpleLog\\Message\\DefaultJsonMessage |
||
408 | 0:-: <?php |
||
409 | 1:-: |
||
410 | 2:-: namespace SimpleLog\\Message; |
||
411 | 3:-: |
||
412 | 4:-: class DefaultJsonMessage extends DefaultMessage |
||
413 | 5:-: { |
||
414 | 6:-: /** |
||
415 | 7:-: * @var array |
||
416 | 8:-: */ |
||
417 | 9:-: protected \$messageScheme = []; |
||
418 | 10:-: |
||
419 | 11:-: /** |
||
420 | 12:-: * @var array |
||
421 | 13:-: */ |
||
422 | 14:-: protected \$context = []; |
||
423 | 15:-: |
||
424 | 16:-: /** |
||
425 | 18:0: * @param string|array|object \$message |
||
426 | 18:-: * @param array \$context |
||
427 | 19:-: * @return \$this |
||
428 | 21:0: */ |
||
429 | 21:-: public function createMessage(\$message, array \$context) |
||
430 | 22:-: { |
||
431 | 23:-: \$this->context = \$context; |
||
432 | 24:-: |
||
433 | 26:0: list(\$date, \$time) = explode(';', strftime(self::DATE_FORMAT . ';' . self::TIME_FORMAT, time())); |
||
434 | 26:-: |
||
435 | 28:0: \$this->messageScheme['date'] = \$date; |
||
436 | 28:-: \$this->messageScheme['time'] = \$time; |
||
437 | 29:-: |
||
438 | 30:-: if (method_exists(\$message, '__toString')) { |
||
439 | 31:-: \$message = (string)\$message; |
||
440 | 32:-: } |
||
441 | 33:-: |
||
442 | 34:-: \$this->messageScheme['data'] = \$message; |
||
443 | 35:-: |
||
444 | 36:-: return \$this; |
||
445 | 37:-: } |
||
446 | 38:-: |
||
447 | 39:-: /** |
||
448 | 40:-: * @return string |
||
449 | 41:-: */ |
||
450 | 42:-: public function getMessage() |
||
451 | 43:-: { |
||
452 | 44:-: \$this->message = json_encode(\$this->messageScheme); |
||
453 | 45:-: \$this->buildContext(\$this->context); |
||
454 | 46:-: |
||
455 | 47:-: return \$this->message; |
||
456 | 48:-: } |
||
457 | 49:-: } |
||
458 | 50:-: |
||
459 | |||
460 | Total coverage: 61.404% |
||
461 | EOT; |
||
462 | |||
463 | $this->assertEquals( |
||
464 | $output, |
||
465 | $this->clearSpaces( |
||
466 | $this->clearExecutionTime( |
||
467 | $commandTester->getDisplay() |
||
468 | ) |
||
469 | ) |
||
470 | ); |
||
471 | } |
||
472 | |||
542 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.