Completed
Push — master ( e6c41e...f385a2 )
by Gaetano
04:45
created

Logger::instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PhpXmlRpc\Helper;
4
5
class Logger
6
{
7
    protected static $instance = null;
8
9
    /**
10
     * This class is singleton, so that later we can move to DI patterns.
11
     *
12
     * @return Logger
13
     */
14 543
    public static function instance()
15
    {
16 543
        if (self::$instance === null) {
17 1
            self::$instance = new self();
18 1
        }
19
20 543
        return self::$instance;
21
    }
22
23
    /**
24
     * Echoes a debug message, taking care of escaping it when not in console mode.
25
     * NB: if the encoding of the message is not known or wrong, and we are working in web mode, there is no guarantee
26
     *     of 100% accuracy, which kind of defeats the purpose of debugging
27
     *
28
     * @param string $message
29
     * @param string $encoding
30
     */
31 543
    public function debugMessage($message, $encoding=null)
32
    {
33
        // US-ASCII is a warning for PHP and a fatal for HHVM
34 543
        if ($encoding == 'US-ASCII') {
35
            $encoding = 'UTF-8';
36
        }
37
38 543
        if (PHP_SAPI != 'cli') {
39
            $flags = ENT_COMPAT;
40
            // avoid warnings on php < 5.4...
41
            if (defined('ENT_HTML401')) {
42
                $flags =  $flags | ENT_HTML401;
43
            }
44
            if (defined('ENT_SUBSTITUTE')) {
45
                $flags =  $flags | ENT_SUBSTITUTE;
46
            }
47
            if ($encoding != null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $encoding of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
48
                print "<PRE>\n".htmlentities($message, $flags, $encoding)."\n</PRE>";
49
            } else {
50
                print "<PRE>\n".htmlentities($message, $flags)."\n</PRE>";
51
            }
52
        } else {
53 543
            print "\n$message\n";
54
        }
55
56
        // let the user see this now in case there's a time out later...
57 543
        flush();
58 543
    }
59
}
60