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

Logger   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 44%

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 11
cts 25
cp 0.44
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A instance() 0 8 2
B debugMessage() 0 28 6
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