Completed
Push — 2.0 ( 0d93f2...14c778 )
by Marco
16:42
created

LogHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 25 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 1
cbo 2
dl 13
loc 52
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A write() 13 13 2
A toConsole() 0 13 2

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 namespace Comodojo\Extender\Console;
2
3
use \Monolog\Logger;
4
use \Monolog\Handler\AbstractProcessingHandler;
5
use \League\CLImate\CLImate;
6
7
/**
8
 * Log-to-console handler
9
 *
10
 * @package     Comodojo extender
11
 * @author      Marco Giovinazzi <[email protected]>
12
 * @license     GPL-3.0+
13
 *
14
 * LICENSE:
15
 *
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28
 */
29
30
class LogHandler extends AbstractProcessingHandler {
31
32
    private $outputcontroller = null;
33
34
    private static $colors = array(
35
        100 => 'light_green',
36
        200 => 'green',
37
        250 => 'light_yellow',
38
        300 => 'yellow',
39
        400 => 'light_red',
40
        500 => 'red',
41
        550 => 'light_magenta',
42
        600 => 'magenta',
43
    );
44
45
    public function __construct($level = Logger::DEBUG, $bubble = true) {
46
47
        $this->outputcontroller = new CLImate();
48
49
        parent::__construct($level, $bubble);
50
51
    }
52
53 View Code Duplication
    protected function write(array $record) {
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...
54
55
        $level = $record['level'];
56
57
        $message = $record['formatted'];
58
59
        $context = empty($record['context']) ? null : $record['context'];
60
61
        $time = $record['datetime']->format('c');
62
63
        $this->toConsole($time, $level, $message, $context);
64
65
    }
66
67
    private function toConsole($time, $level, $message, $context) {
0 ignored issues
show
Unused Code introduced by
The parameter $time is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
68
69
        $color = static::$colors[$level];
0 ignored issues
show
Bug introduced by
Since $colors is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $colors to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
70
71
        $pattern = "<%s>%s</%s>";
72
73
        $message = sprintf($pattern, $color, $message, $color);
74
75
        $this->outputcontroller->out($message);
76
77
        if ( !empty($context) ) $this->outputcontroller->out(sprintf($pattern, $color, var_export($context, true), $color));
78
79
    }
80
81
}
82