Passed
Push — master ( 9f5262...c72dd6 )
by Gaetano
09:22
created

Logger::debug()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2.5

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 6
ccs 1
cts 2
cp 0.5
crap 2.5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpXmlRpc\Helper;
4
5
/**
6
 * @todo implement an interface
7
 * @todo make constructor private to force users to go through `instance()` ?
8
 */
9
class Logger
10
{
11
    protected static $instance = null;
12
13
    /**
14
     * This class can be used as singleton, so that later we can move to DI patterns (ish...)
15
     *
16
     * @return Logger
17
     */
18 51
    public static function instance()
19
    {
20 51
        if (self::$instance === null) {
21 47
            self::$instance = new self();
22
        }
23
24 51
        return self::$instance;
25
    }
26
27
    // *** Implement the same interface as PSR/LOG, for the sake of interoperability ***
28
29
    /**
30
     * NB: unlike other "traditional" loggers, this one echoes to screen the debug messages instead of logging them.
31
     *
32
     * @param string $message
33
     * @param array $context known key: 'encoding'
34
     * @return void
35 3
     */
36
    public function debug($message, $context = array())
37
    {
38 3
        if (isset($context['encoding'])) {
39
            $this->debugMessage($message, $context['encoding']);
40
        } else {
41
            $this->debugMessage($message);
42 3
        }
43 3
    }
44
45 3
    /**
46 3
     * Triggers the writing of a message to php's error log
47
     *
48 3
     * @param string $message
49 3
     * @param array $context
50
     * @return void
51 3
     */
52 3
    public function error($message, $context = array())
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

52
    public function error($message, /** @scrutinizer ignore-unused */ $context = array())

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

Loading history...
53
    {
54 3
        $this->errorLog($message);
55
    }
56
57
    // BC interface
58
59
    /**
60
     * Echoes a debug message, taking care of escaping it when not in console mode.
61 3
     * NB: if the encoding of the message is not known or wrong, and we are working in web mode, there is no guarantee
62 3
     *     of 100% accuracy, which kind of defeats the purpose of debugging
63
     *
64
     * @param string $message
65
     * @param string $encoding deprecated
66
     * @return void
67
     *
68 68
     * @internal left in purely for BC
69
     */
70 68
    public function debugMessage($message, $encoding = null)
71 68
    {
72
        // US-ASCII is a warning for PHP and a fatal for HHVM
73
        if ($encoding == 'US-ASCII') {
74
            $encoding = 'UTF-8';
75
        }
76
77
        if (PHP_SAPI != 'cli') {
78
            $flags = ENT_COMPAT;
79
            // avoid warnings on php < 5.4...
80
            if (defined('ENT_HTML401')) {
81
                $flags =  $flags | ENT_HTML401;
82
            }
83
            if (defined('ENT_SUBSTITUTE')) {
84
                $flags =  $flags | ENT_SUBSTITUTE;
85
            }
86
            if ($encoding != null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $encoding of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
87
                print "<PRE>\n".htmlentities($message, $flags, $encoding)."\n</PRE>";
88
            } else {
89
                print "<PRE>\n".htmlentities($message, $flags)."\n</PRE>";
90
            }
91
        } else {
92
            print "\n$message\n";
93
        }
94
95
        // let the user see this now in case there's a time-out later...
96
        flush();
97
    }
98
99
    /**
100
     * Writes a message to the error log.
101
     *
102
     * @param string $message
103
     * @return void
104
     *
105
     * @internal left in purely for BC
106
     */
107
    public function errorLog($message)
108
    {
109
        error_log($message);
110
    }
111
}
112