Passed
Push — master ( da2c1e...110169 )
by Gaetano
04:55 queued 01:13
created

Logger::instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
ccs 4
cts 4
cp 1
crap 2
1
<?php
2
3
namespace PhpXmlRpc\Helper;
4
5
/**
6
 * @todo make constructor private to force users to go through `instance()`
7
 */
8
class Logger
9
{
10
    protected static $instance = null;
11
12
    /**
13
     * This class is singleton, so that later we can move to DI patterns.
14
     *
15
     * @return Logger
16
     */
17 44
    public static function instance()
18
    {
19 44
        if (self::$instance === null) {
20 43
            self::$instance = new self();
21
        }
22
23 44
        return self::$instance;
24
    }
25
26
    /**
27
     * Echoes a debug message, taking care of escaping it when not in console mode.
28
     * NB: if the encoding of the message is not known or wrong, and we are working in web mode, there is no guarantee
29
     *     of 100% accuracy, which kind of defeats the purpose of debugging
30
     *
31
     * @param string $message
32
     * @param string $encoding
33
     */
34 3
    public function debugMessage($message, $encoding = null)
35
    {
36
        // US-ASCII is a warning for PHP and a fatal for HHVM
37 3
        if ($encoding == 'US-ASCII') {
38
            $encoding = 'UTF-8';
39
        }
40
41 3
        if (PHP_SAPI != 'cli') {
42 3
            $flags = ENT_COMPAT;
43
            // avoid warnings on php < 5.4...
44 3
            if (defined('ENT_HTML401')) {
45 3
                $flags =  $flags | ENT_HTML401;
46
            }
47 3
            if (defined('ENT_SUBSTITUTE')) {
48 3
                $flags =  $flags | ENT_SUBSTITUTE;
49
            }
50 3
            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...
51 3
                print "<PRE>\n".htmlentities($message, $flags, $encoding)."\n</PRE>";
52
            } else {
53 3
                print "<PRE>\n".htmlentities($message, $flags)."\n</PRE>";
54
            }
55
        } else {
56
            print "\n$message\n";
57
        }
58
59
        // let the user see this now in case there's a time out later...
60 3
        flush();
61 3
    }
62
63
    /**
64
     * Writes a message to the error log
65
     * @param string $message
66
     */
67 41
    public function errorLog($message)
68
    {
69 41
        error_log($message);
70 41
    }
71
}
72