Passed
Push — master ( 67ed62...5da465 )
by Gaetano
02:56
created

Logger::debugMessage()   A

Complexity

Conditions 6
Paths 18

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6.105

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 15
c 2
b 0
f 0
nc 18
nop 2
dl 0
loc 27
ccs 12
cts 14
cp 0.8571
crap 6.105
rs 9.2222
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 44
    public static function instance()
15
    {
16 44
        if (self::$instance === null) {
17 43
            self::$instance = new self();
18
        }
19
20 44
        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 3
    public function debugMessage($message, $encoding = null)
32
    {
33
        // US-ASCII is a warning for PHP and a fatal for HHVM
34 3
        if ($encoding == 'US-ASCII') {
35
            $encoding = 'UTF-8';
36
        }
37
38 3
        if (PHP_SAPI != 'cli') {
39 3
            $flags = ENT_COMPAT;
40
            // avoid warnings on php < 5.4...
41 3
            if (defined('ENT_HTML401')) {
42 3
                $flags =  $flags | ENT_HTML401;
43
            }
44 3
            if (defined('ENT_SUBSTITUTE')) {
45 3
                $flags =  $flags | ENT_SUBSTITUTE;
46
            }
47 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...
48 3
                print "<PRE>\n".htmlentities($message, $flags, $encoding)."\n</PRE>";
49
            } else {
50 3
                print "<PRE>\n".htmlentities($message, $flags)."\n</PRE>";
51
            }
52
        } else {
53
            print "\n$message\n";
54
        }
55
56
        // let the user see this now in case there's a time out later...
57 3
        flush();
58 3
    }
59
60
    /**
61
     * Writes a message to the error log
62
     */
63 41
    public function errorLog($message)
64
    {
65 41
        error_log($message);
66 41
    }
67
}
68