Completed
Push — feature/0.7.0 ( 79c386...7f3169 )
by Ryuichi
03:02
created

LoggerFormatter   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 116
Duplicated Lines 6.9 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 18
c 5
b 0
f 0
lcom 1
cbo 1
dl 8
loc 116
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getFormattedMessage() 0 15 1
A compile() 0 4 1
A compileApplicationName() 0 10 4
B compileDateTime() 0 22 6
B compileLogLevel() 8 14 5

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\Module\Container;
5
use WebStream\Module\Utility\LoggerUtils;
6
7
/**
8
 * LoggerFormatterクラス
9
 * ログフォーマッタ処理を行う
10
 * @author Ryuichi Tanaka
11
 * @since 2015/12/06
12
 * @version 0.7
13
 */
14
class LoggerFormatter
15
{
16
    use LoggerUtils;
17
18
    /**
19
     * @var Container ログ設定コンテナ
20
     */
21
    private $logConfig;
22
23
    /**
24
     * コンストラクタ
25
     * @param string 設定ファイルパス
26
     */
27
    public function __construct(Container $logConfig)
28
    {
29
        // $this->loadConfig($configPath);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
30
        $this->logConfig = $logConfig;
31
        $this->compile();
32
    }
33
34
    /**
35
     * フォーマット済みメッセージを返却する
36
     * @param  string メッセージ
37
     * @param  string ログレベル
38
     * @return フォーマット済みメッセージ
39
     */
40
    public function getFormattedMessage($message, $logLevel)
41
    {
42
        $formattedMessage = $this->logConfig->format;
0 ignored issues
show
Documentation introduced by
The property format does not exist on object<WebStream\Module\Container>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
43
44
        // 日付
45
        $formattedMessage = $this->compileDateTime($formattedMessage);
46
47
        // ログレベル
48
        $formattedMessage = $this->compileLogLevel($formattedMessage, $logLevel);
49
50
        // メッセージ
51
        $formattedMessage = preg_replace('/%m/', $message, $formattedMessage);
52
53
        return $formattedMessage . "\n";
54
    }
55
56
    /**
57
     * 固定の項目を埋め込む
58
     */
59
    private function compile()
60
    {
61
        $this->logConfig->format = $this->compileApplicationName($this->logConfig->format, $this->logConfig->applicationName);
0 ignored issues
show
Documentation introduced by
The property format does not exist on object<WebStream\Module\Container>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property format does not exist on object<WebStream\Module\Container>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property applicationName does not exist on object<WebStream\Module\Container>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
62
    }
63
64
    /**
65
     * アプリケーション名項目を埋め込む
66
     * @param  string メッセージ
67
     * @param  string アプリケーション名
68
     * @return 埋め込み済みメッセージ
69
     */
70
    private function compileApplicationName($message, $applicationName)
71
    {
72
        // アプリケーション名
73
        if ($applicationName !== null && preg_match('/%([0-9]{0,})c/', $this->logConfig->format, $matches)) {
0 ignored issues
show
Documentation introduced by
The property format does not exist on object<WebStream\Module\Container>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
74
            $applicationName = $matches[1] !== null ? str_pad($applicationName, intval($matches[1]), ' ') : $applicationName;
75
            $message = preg_replace('/%(?:[0-9]{0,})c/', $applicationName, $message);
76
        }
77
78
        return $message;
79
    }
80
81
    /**
82
     * 日付項目を埋め込む
83
     * @param  string メッセージ
84
     * @return 埋め込み済みメッセージ
85
     */
86
    private function compileDateTime($message)
87
    {
88
        if (preg_match('/%([0-9]{0,})d(?:\{(.+?)\}){1}/', $message, $formatMatches)) {
89
            $message = preg_replace('/%[0-9]{0,}d/', '%d', $message);
90
            $now = microtime(true);
91
            $decimal = "000";
92
            if (preg_match('/^[0-9]*\\.([0-9]+)$/', $now, $matches) === 1) {
93
                $decimal = str_pad(substr($matches[1], 0, 3), 3, "0");
94
            }
95
            $dateTimeFormat = preg_replace('/(%f)/', $decimal, $formatMatches[2]);
96
            $dateTime = strftime($dateTimeFormat, $now);
97
            $dateTime = empty($formatMatches[1]) ? $dateTime : str_pad($dateTime, $formatMatches[1], ' ');
98
            $message = preg_replace('/%d\{.+?\}/', $dateTime, $message);
99
        } elseif (preg_match('/%([0-9]{0,})d/', $message, $formatMatches)) {
100
            $message = preg_replace('/%[0-9]{0,}d/', '%d', $message);
101
            $dateTime = strftime($this->defaultDateTimeFormatter());
102
            $dateTime = empty($formatMatches[1]) ? $dateTime : str_pad($dateTime, $formatMatches[1], ' ');
103
            $message = preg_replace('/%d/', $dateTime, $message);
104
        }
105
106
        return $message;
107
    }
108
109
    /**
110
     * ログレベル項目を埋め込む
111
     * @param  string メッセージ
112
     * @param  string ログレベル
113
     * @return 埋め込み済みメッセージ
114
     */
115
    private function compileLogLevel($message, $logLevel)
116
    {
117
        // ログレベル
118 View Code Duplication
        if (preg_match('/%([0-9]{0,})L/', $message, $matches)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
119
            $upperLevel = strtoupper(empty($matches[1]) ? $logLevel : str_pad($logLevel, $matches[1], ' '));
120
            $message = preg_replace('/%([0-9]{0,})L/', $upperLevel, $message);
121
        }
122 View Code Duplication
        if (preg_match('/%([0-9]{0,})l/', $message, $matches)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
123
            $lowerLevel = empty($matches[1]) ? $logLevel : str_pad($logLevel, $matches[1], ' ');
124
            $message = preg_replace('/%([0-9]{0,})l/', $lowerLevel, $message);
125
        }
126
127
        return $message;
128
    }
129
}
130