LineFormatter   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 99
Duplicated Lines 9.09 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 20
c 3
b 0
f 1
lcom 1
cbo 2
dl 9
loc 99
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 3
A getMessageFormat() 0 4 1
A getDateFormat() 0 4 1
C format() 9 30 7
B convertToString() 0 20 8

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
3
namespace Oqq\Minc\Log\Formatter;
4
5
use Oqq\Minc\Log\Record;
6
7
/**
8
 * @author Eric Braun <[email protected]>
9
 */
10
class LineFormatter implements FormatterInterface
11
{
12
    const DEFAULT_MESSAGE_FORMAT = '[%datetime%] %channel%.%level_name%: %message% %context% %extras%';
13
    const DEFAULT_DATE_FORMAT    = 'Y-m-d H:i:s';
14
15
    /** @var string */
16
    protected $messageFormat;
17
18
    /** @var string */
19
    protected $dateFormat;
20
21
    /**
22
     * @param string $messageFormat
23
     * @param string $dateFormat
24
     */
25
    public function __construct($messageFormat = null, $dateFormat = null)
26
    {
27
        $this->messageFormat = $messageFormat ?: static::DEFAULT_MESSAGE_FORMAT;
28
        $this->dateFormat = $dateFormat ?: static::DEFAULT_DATE_FORMAT;
29
    }
30
31
    /**
32
     * @return string
33
     */
34
    public function getMessageFormat()
35
    {
36
        return $this->messageFormat;
37
    }
38
39
    /**
40
     * @return string
41
     */
42
    public function getDateFormat()
43
    {
44
        return $this->dateFormat;
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50
    public function format(Record $record)
51
    {
52
        $replacements = [];
53
54 View Code Duplication
        if (false !== strpos($this->messageFormat, '%datetime%')) {
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...
55
            $replacements['%datetime%'] = $this->convertToString($record->getCreatedAt());
56
        }
57
58
        if (false !== strpos($this->messageFormat, '%channel%')) {
59
            $replacements['%channel%'] = $record->getChannel();
60
        }
61
62
        if (false !== strpos($this->messageFormat, '%level_name%')) {
63
            $replacements['%level_name%'] = $record->getLevel()->getName();
64
        }
65
66
        if (false !== strpos($this->messageFormat, '%message%')) {
67
            $replacements['%message%'] = $record->getMessage();
68
        }
69
70 View Code Duplication
        if (false !== strpos($this->messageFormat, '%context%')) {
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...
71
            $replacements['%context%'] = $this->convertToString($record->getContext(), true);
72
        }
73
74 View Code Duplication
        if (false !== strpos($this->messageFormat, '%extras%')) {
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...
75
            $replacements['%extras%'] = $this->convertToString($record->getExtras(), true);
76
        }
77
78
        return strtr($this->messageFormat, $replacements);
79
    }
80
81
    /**
82
     * @todo use a minc helper class for string converting
83
     * @param mixed $data
84
     * @param bool $ignoreEmptyData
85
     *
86
     * @return string
87
     */
88
    protected function convertToString($data, $ignoreEmptyData = false)
89
    {
90
        if ($ignoreEmptyData && empty($data)) {
91
            return '';
92
        }
93
94
        if (null === $data || is_bool($data)) {
95
            return var_export($data, true);
96
        }
97
98
        if (is_scalar($data) || method_exists($data, '__toString')) {
99
            return (string) $data;
100
        }
101
102
        if ($data instanceof \DateTime) {
103
            return $data->format($this->dateFormat);
104
        }
105
106
        return json_encode($data);
107
    }
108
}
109