Stdout   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 117
Duplicated Lines 7.69 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 9
loc 117
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getColoredString() 0 16 3
D log() 9 39 9

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
declare(strict_types = 1);
3
4
/**
5
 * Micro
6
 *
7
 * @author    Raffael Sahli <[email protected]>
8
 * @copyright Copyright (c) 2017 gyselroth GmbH (https://gyselroth.com)
9
 * @license   MIT https://opensource.org/licenses/MIT
10
 */
11
12
namespace Micro\Log\Adapter;
13
14
use \Psr\Log\LogLevel;
15
16
class Stdout extends AbstractAdapter
17
{
18
    /**
19
     * Bash fg colors
20
     *
21
     * @var array
22
     */
23
    public $foreground_colors = [
24
        'black'        => '0;30',
25
        'dark_gray'    => '1;30',
26
        'blue'         => '0;34',
27
        'light_blue'   => '1;34',
28
        'green'        => '0;32',
29
        'light_green'  => '1;32',
30
        'cyan'         => '0;36',
31
        'light_cyan'   => '1;36',
32
        'red'          => '0;31',
33
        'light_red'    => '1;31',
34
        'purple'       => '0;35',
35
        'light_purple' => '1;35',
36
        'brown'        => '0;33',
37
        'yellow'       => '1;33',
38
        'light_gray'   => '0;37',
39
        'white'        => '1;37',
40
    ];
41
42
43
    /**
44
     * Bash bg colors
45
     *
46
     * @var array
47
     */
48
    public $background_colors = [
49
        'black'        => '40',
50
        'red'          => '41',
51
        'green'        => '42',
52
        'yellow'       => '43',
53
        'blue'         => '44',
54
        'magenta'      => '45',
55
        'cyan'         => '46',
56
        'light_gray'   => '47',
57
    ];
58
59
60
    /**
61
     * Convert to bash color
62
     *
63
     * @param   string $string
64
     * @param   string $foreground_color
65
     * @param   string $background_color
66
     * @return  string
67
     */
68
    public function getColoredString(string $string, string $foreground_color = null, string $background_color = null): string
69
    {
70
        $colored_string = "";
71
72
        if (isset($this->foreground_colors[$foreground_color])) {
73
            $colored_string .= "\033[".$this->foreground_colors[$foreground_color]."m";
74
        }
75
76
        if (isset($this->background_colors[$background_color])) {
77
            $colored_string .= "\033[".$this->background_colors[$background_color]."m";
78
        }
79
80
        $colored_string .= $string."\033[0m";
81
82
        return $colored_string;
83
    }
84
85
86
    /**
87
     * Log
88
     *
89
     * @param   string $level
90
     * @param   string $message
91
     * @return  bool
92
     */
93
    public function log(string $level, string $message): bool
94
    {
95
        switch ($level) {
96
            case LogLevel::EMERGENCY:
97
                $message = '['.$this->getColoredString('EMERG', 'red').']'."\t".$message;
98
            break;
99
100
            case LogLevel::ALERT:
101
                $message = '['.$this->getColoredString('ALERT', 'red').']'."\t".$message;
102
            break;
103
104 View Code Duplication
            case LogLevel::CRITICAL:
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...
105
                $message = '['.$this->getColoredString('CRIT', 'red').']'."\t".$message;
106
            break;
107
108
            case LogLevel::ERROR:
109
                $message = '['.$this->getColoredString('ERR', 'light_red').']'."\t".$message;
110
            break;
111
112
            case LogLevel::WARNING:
113
                $message = '['.$this->getColoredString('WARN', 'yellow').']'."\t".$message;
114
            break;
115
116 View Code Duplication
            case LogLevel::INFO:
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...
117
                $message = '['.$this->getColoredString('INFO', 'cyan').']'."\t".$message;
118
            break;
119
120
            case LogLevel::NOTICE:
121
                $message = '['.$this->getColoredString('NOTICE', 'green').']'."\t".$message;
122
            break;
123
124 View Code Duplication
            case LogLevel::DEBUG:
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...
125
                $message = '['.$this->getColoredString('DEBUG', 'blue').']'."\t".$message;
126
            break;
127
        }
128
129
        echo $message."\n";
130
        return true;
131
    }
132
}
133