DebugHelper   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Test Coverage

Coverage 45.95%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 40
c 4
b 1
f 0
dl 0
loc 123
ccs 17
cts 37
cp 0.4595
rs 10
wmc 16

8 Methods

Rating   Name   Duplication   Size   Complexity  
A debug_allowed_methods() 0 9 3
A debug_msg() 0 4 2
A debug_method_signature1() 0 3 1
A debug_connection_start() 0 11 2
A debug_hexdump() 0 8 2
A debug_method_signature() 0 7 2
A __construct() 0 9 3
A print_msg() 0 3 1
1
<?php
2
3
namespace PhpAmqpLib\Helper;
4
5
use PhpAmqpLib\Wire\Constants;
6
7
class DebugHelper
8
{
9
    /**
10
     * @var bool
11
     */
12
    protected $debug;
13
14
    /**
15
     * @var resource
16
     */
17
    protected $debug_output;
18
19
    /**
20
     * @var Constants
21
     */
22
    protected $constants;
23
24
    /**
25
     * @param Constants $constants
26
     */
27 50
    public function __construct(Constants $constants)
28
    {
29 50
        $this->debug = defined('AMQP_DEBUG') ? AMQP_DEBUG : false;
0 ignored issues
show
Bug introduced by
The constant PhpAmqpLib\Helper\AMQP_DEBUG was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
30 50
        if (defined('AMQP_DEBUG_OUTPUT')) {
31
            $this->debug_output = AMQP_DEBUG_OUTPUT;
0 ignored issues
show
Bug introduced by
The constant PhpAmqpLib\Helper\AMQP_DEBUG_OUTPUT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
32
        } else {
33 50
            $this->debug_output = fopen('php://output', 'wb');
34
        }
35 50
        $this->constants = $constants;
36
    }
37
38
    /**
39
     * @param string $msg
40
     */
41 50
    public function debug_msg($msg)
42
    {
43 50
        if ($this->debug) {
44
            $this->print_msg($msg);
45
        }
46
    }
47
48
    /**
49
     * @param array|null $allowed_methods
50
     */
51 47
    public function debug_allowed_methods($allowed_methods)
52
    {
53 47
        if ($this->debug) {
54
            if ($allowed_methods) {
55
                $msg = 'waiting for ' . implode(', ', $allowed_methods);
56
            } else {
57
                $msg = 'waiting for any method';
58
            }
59
            $this->debug_msg($msg);
60
        }
61
    }
62
63
    /**
64
     * @param string|array $method_sig
65
     */
66 47
    public function debug_method_signature1($method_sig)
67
    {
68 47
        $this->debug_method_signature('< %s:', $method_sig);
69
    }
70
71
    /**
72
     * @param string $msg
73
     * @param string|array $method_sig
74
     */
75 47
    public function debug_method_signature($msg, $method_sig)
76
    {
77 47
        if ($this->debug) {
78
            $constants = $this->constants;
79
            $methods = $constants::$GLOBAL_METHOD_NAMES;
80
            $key = MiscHelper::methodSig($method_sig);
81
            $this->debug_msg(sprintf($msg . ': %s', $key, $methods[$key]));
82
        }
83
    }
84
85
    /**
86
     * @param string $data
87
     */
88 47
    public function debug_hexdump($data)
89
    {
90 47
        if ($this->debug) {
91
            $this->debug_msg(
92
                sprintf(
93
                    '< [hex]: %s%s',
94
                    PHP_EOL,
95
                    MiscHelper::hexdump($data, $htmloutput = false, $uppercase = true, $return = true)
96
                )
97
            );
98
        }
99
    }
100
101
    /**
102
     * @param int $version_major
103
     * @param int $version_minor
104
     * @param array $server_properties
105
     * @param array $mechanisms
106
     * @param array $locales
107
     */
108 47
    public function debug_connection_start($version_major, $version_minor, $server_properties, $mechanisms, $locales)
109
    {
110 47
        if ($this->debug) {
111
            $this->debug_msg(
112
                sprintf(
113
                    'Start from server, version: %d.%d, properties: %s, mechanisms: %s, locales: %s',
114
                    $version_major,
115
                    $version_minor,
116
                    MiscHelper::dump_table($server_properties),
117
                    implode(', ', $mechanisms),
118
                    implode(', ', $locales)
119
                )
120
            );
121
        }
122
    }
123
124
    /**
125
     * @param string $s
126
     */
127
    protected function print_msg($s)
128
    {
129
        fwrite($this->debug_output, $s . PHP_EOL);
130
    }
131
}
132