AMQPStreamConnection::__construct()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 59
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 6.3183

Importance

Changes 0
Metric Value
cc 5
eloc 30
nc 6
nop 17
dl 0
loc 59
ccs 5
cts 8
cp 0.625
crap 6.3183
rs 9.1288
c 0
b 0
f 0

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace PhpAmqpLib\Connection;
4
5
use PhpAmqpLib\Wire\IO\StreamIO;
6
7
class AMQPStreamConnection extends AbstractConnection
8
{
9
    /**
10
     * @param string $host
11
     * @param int $port
12
     * @param string $user
13
     * @param string $password
14
     * @param string $vhost
15
     * @param bool $insist
16
     * @param string $login_method
17
     * @param null $login_response @deprecated
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $login_response is correct as it would always require null to be passed?
Loading history...
18
     * @param string $locale
19
     * @param float $connection_timeout
20
     * @param float $read_write_timeout
21
     * @param resource|array|null $context
22
     * @param bool $keepalive
23
     * @param int $heartbeat
24
     * @param float $channel_rpc_timeout
25
     * @param string|AMQPConnectionConfig|null $ssl_protocol @deprecated
26
     * @param AMQPConnectionConfig|null $config
27
     * @throws \Exception
28 30
     */
29
    public function __construct(
30
        $host,
31
        $port,
32
        $user,
33
        $password,
34
        $vhost = '/',
35
        $insist = false,
36
        $login_method = 'AMQPLAIN',
37
        $login_response = null,
38
        $locale = 'en_US',
39
        $connection_timeout = 3.0,
40
        $read_write_timeout = 3.0,
41
        $context = null,
42
        $keepalive = false,
43
        $heartbeat = 0,
44
        $channel_rpc_timeout = 0.0,
45
        $ssl_protocol = null,
46
        ?AMQPConnectionConfig $config = null
47 30
    ) {
48 1
        if ($ssl_protocol !== null && $ssl_protocol instanceof AMQPConnectionConfig === false) {
49
            trigger_error(
50
                '$ssl_protocol parameter is deprecated, use stream_context_set_option($context, \'ssl\', \'crypto_method\', $ssl_protocol) instead (see https://www.php.net/manual/en/function.stream-socket-enable-crypto.php for possible values)',
51 29
                E_USER_DEPRECATED
52
            );
53
        } elseif ($ssl_protocol instanceof AMQPConnectionConfig) {
54
            $config = $ssl_protocol;
55
        }
56
57
        if ($channel_rpc_timeout > $read_write_timeout) {
58
            throw new \InvalidArgumentException('channel RPC timeout must not be greater than I/O read-write timeout');
59
        }
60
61
        $io = new StreamIO(
62 29
            $host,
63
            $port,
64
            $connection_timeout,
65
            $read_write_timeout,
66
            $context,
67
            $keepalive,
68
            $heartbeat
69
        );
70
71
        parent::__construct(
0 ignored issues
show
Deprecated Code introduced by
The function PhpAmqpLib\Connection\Ab...nnection::__construct() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

71
        /** @scrutinizer ignore-deprecated */ parent::__construct(
Loading history...
72
            $user,
73
            $password,
74
            $vhost,
75
            $insist,
76
            $login_method,
77
            $login_response,
78 28
            $locale,
79
            $io,
80
            $heartbeat,
81
            $connection_timeout,
82
            $channel_rpc_timeout,
83
            $config
84
        );
85
86
        // save the params for the use of __clone, this will overwrite the parent
87
        $this->construct_params = func_get_args();
88
    }
89
90
    /**
91
     * @deprecated Use AmqpConnectionFactory
92
     * @throws \Exception
93
     */
94
    protected static function try_create_connection($host, $port, $user, $password, $vhost, $options)
95
    {
96
        $insist = isset($options['insist']) ?
97
                        $options['insist'] : false;
98
        $login_method = isset($options['login_method']) ?
99
                              $options['login_method'] : 'AMQPLAIN';
100
        $login_response = isset($options['login_response']) ?
101
                                $options['login_response'] : null;
102
        $locale = isset($options['locale']) ?
103
                        $options['locale'] : 'en_US';
104
        $connection_timeout = isset($options['connection_timeout']) ?
105
                                    $options['connection_timeout'] : 3.0;
106
        $read_write_timeout = isset($options['read_write_timeout']) ?
107
                                    $options['read_write_timeout'] : 3.0;
108
        $context = isset($options['context']) ?
109
                         $options['context'] : null;
110
        $keepalive = isset($options['keepalive']) ?
111
                           $options['keepalive'] : false;
112
        $heartbeat = isset($options['heartbeat']) ?
113
                           $options['heartbeat'] : 60;
114
        $channel_rpc_timeout = isset($options['channel_rpc_timeout']) ?
115
                                    $options['channel_rpc_timeout'] : 0.0;
116
        return new static(
117
            $host,
118
            $port,
119
            $user,
120
            $password,
121
            $vhost,
122
            $insist,
123
            $login_method,
124
            $login_response,
125
            $locale,
126
            $connection_timeout,
127
            $read_write_timeout,
128
            $context,
129
            $keepalive,
130
            $heartbeat,
131
            $channel_rpc_timeout
132
        );
133
    }
134
}
135