Completed
Push — master ( 8a4ca9...acd23c )
by Marco
02:46
created

src/Log/ConsoleHandler.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php namespace Comodojo\Extender\Log;
2
3
use \Monolog\Logger;
4
use \Monolog\Handler\AbstractProcessingHandler;
5
use \Console_Color2;
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 ConsoleHandler extends AbstractProcessingHandler {
31
32
    private $color = null;
33
34
    private static $colors = array(
35
        100 => '%8',
36
        200 => '%g',
37
        250 => '%U',
38
        300 => '%Y',
39
        400 => '%r',
40
        500 => '%R',
41
        550 => '%m',
42
        600 => '%M',
43
    );
44
45 6
    public function __construct($level = Logger::DEBUG, $bubble = true) {
46
47 6
        $this->color = new Console_Color2();
48
49 6
        parent::__construct($level, $bubble);
50
51 6
    }
52
53
    protected function write(array $record) {
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
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
        print $this->color->convert(static::$colors[$level].$message."%n");
0 ignored issues
show
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
        if ( !empty($context) ) print $this->color->convert(static::$colors[$level].var_export($context, true)."%n\n");
0 ignored issues
show
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...
72
73
    }
74
75
}
76