Completed
Push — feature/0.7.0 ( 38440f...02d6a4 )
by Ryuichi
07:28
created

LoggerUtils   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 75
Duplicated Lines 34.67 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 26
loc 75
rs 10
c 0
b 0
f 0
wmc 16
lcom 0
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A defaultLoggerFormatter() 0 4 1
A defaultDateTimeFormatter() 0 4 1
A addStackTrace() 0 14 3
C toLogLevelValue() 26 26 11

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace WebStream\Log;
3
4
use WebStream\Exception\Extend\LoggerException;
5
6
/**
7
 * LoggerUtils
8
 * @author Ryuichi Tanaka
9
 * @since 2015/12/26
10
 * @version 0.7
11
 */
12
trait LoggerUtils
13
{
14
    /**
15
     * デフォルトロガーフォーマッタ
16
     * @return string デフォルトロガーフォーマッタ
17
     */
18
    public function defaultLoggerFormatter()
19
    {
20
        return '[%d{' . $this->defaultDateTimeFormatter() . '.%f}][%5L] %m';
21
    }
22
23
    /**
24
     * デフォルトDateTimeフォーマッタ
25
     * @return string デフォルトDateTimeフォーマッタ
26
     */
27
    public function defaultDateTimeFormatter()
28
    {
29
        return "%Y-%m-%d %H:%M:%S";
30
    }
31
32
    /**
33
     * ログメッセージにスタックトレースの内容を追加する
34
     * @param string ログメッセージ
35
     * @param string スタックトレース文字列
36
     * @return string 加工済みログメッセージ
37
     */
38
    public function addStackTrace($msg, $stacktrace)
39
    {
40
        // スタックトレースから原因となるエラー箇所のみ抽出
41
        $stacktraceList = explode("#", $stacktrace);
42
        foreach ($stacktraceList as $stacktraceLine) {
43
            if ($stacktraceLine === "") {
44
                continue;
45
            }
46
            $msg .= PHP_EOL;
47
            $msg .= "\t#" . trim($stacktraceLine);
48
        }
49
50
        return $msg;
51
    }
52
53
    /**
54
     * ログレベルを数値に変換
55
     * ログレベルはWebStream独自、PSR-3両方対応
56
     * @param string ログレベル文字列
57
     * @throws LoggerException
58
     * @return int ログレベル数値
59
     */
60 View Code Duplication
    public function toLogLevelValue(string $level)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
61
    {
62
        switch (strtolower($level)) {
63
            case 'debug':
64
                return 1;
65
            case 'info':
66
                return 2;
67
            case 'notice':    // PSR-3
68
                return 3;
69
            case 'warn':
70
            case 'warning':   // PSR-3
71
                return 4;
72
            case 'error':
73
                return 5;
74
            case 'critical':  // PSR-3
75
                return 6;
76
            case 'alert':     // PSR-3
77
                return 7;
78
            case 'emergency': // PSR-3
79
                return 8;
80
            case 'fatal':
81
                return 9;
82
            default:
83
                throw new LoggerException("Undefined log level: $level");
84
        }
85
    }
86
}
87