Queue   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 284
Duplicated Lines 16.9 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 68.42%

Importance

Changes 0
Metric Value
wmc 34
lcom 2
cbo 2
dl 48
loc 284
ccs 52
cts 76
cp 0.6842
rs 9.68
c 0
b 0
f 0

27 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getChannel() 0 4 1
A getConnection() 0 4 1
A setName() 0 4 1
A getName() 0 4 1
A setArgument() 0 4 1
A setArguments() 0 4 1
A getArgument() 0 4 1
A getArguments() 0 4 1
A hasArgument() 0 4 1
A setFlags() 0 4 1
A getFlags() 0 4 1
A getConsumerTag() 0 4 1
A declareQueue() 0 4 1
A bind() 0 4 1
A unbind() 0 4 1
A get() 8 8 2
A consume() 8 8 1
A ack() 0 4 1
A nack() 0 4 1
A reject() 0 4 1
A cancel() 0 4 1
A delete() 0 4 1
A purge() 0 4 1
A getDelegate() 0 4 1
A convertToDelegateFlags() 16 16 4
A convertFromDelegateFlags() 16 16 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace TreeHouse\Queue\Amqp\Driver\Amqp;
4
5
use TreeHouse\Queue\Amqp\ChannelInterface;
6
use TreeHouse\Queue\Amqp\QueueInterface;
7
8
class Queue implements QueueInterface
9
{
10
    /**
11
     * @var \AMQPQueue
12
     */
13
    protected $delegate;
14
15
    /**
16
     * @var ChannelInterface
17
     */
18
    protected $channel;
19
20
    /**
21
     * @var array<integer, integer>
22
     */
23
    protected static $flagMap = [
24
        self::NOPARAM => AMQP_NOPARAM,
25
        self::DURABLE => AMQP_DURABLE,
26
        self::PASSIVE => AMQP_PASSIVE,
27
        self::EXCLUSIVE => AMQP_EXCLUSIVE,
28
        self::AUTODELETE => AMQP_AUTODELETE,
29
        self::MULTIPLE => AMQP_MULTIPLE,
30
        self::AUTOACK => AMQP_AUTOACK,
31
        self::REQUEUE => AMQP_REQUEUE,
32
        self::IFUNUSED => AMQP_IFUNUSED,
33
        self::IFEMPTY => AMQP_IFEMPTY,
34
    ];
35
36
    /**
37
     * @param \AMQPQueue       $delegate
38
     * @param ChannelInterface $channel
39
     */
40 3
    public function __construct(\AMQPQueue $delegate, ChannelInterface $channel)
41
    {
42 3
        $this->delegate = $delegate;
43 3
        $this->channel = $channel;
44 3
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49 1
    public function getChannel()
50
    {
51 1
        return $this->channel;
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57 1
    public function getConnection()
58
    {
59 1
        return $this->channel->getConnection();
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65 1
    public function setName($name)
66
    {
67 1
        return $this->delegate->setName($name);
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73 2
    public function getName()
74
    {
75 2
        return $this->delegate->getName();
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81 1
    public function setArgument($key, $value)
82
    {
83 1
        return $this->delegate->setArgument($key, $value);
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89 1
    public function setArguments(array $arguments)
90
    {
91 1
        return $this->delegate->setArguments($arguments);
92
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97 2
    public function getArgument($key)
98
    {
99 2
        return $this->delegate->getArgument($key);
100
    }
101
102
    /**
103
     * @inheritdoc
104
     */
105 2
    public function getArguments()
106
    {
107 2
        return $this->delegate->getArguments();
108
    }
109
110
    /**
111
     * @inheritdoc
112
     */
113 1
    public function hasArgument($key)
114
    {
115 1
        return $this->delegate->hasArgument($key);
116
    }
117
118
    /**
119
     * @inheritDoc
120
     */
121 1
    public function setFlags($flags)
122
    {
123 1
        return $this->delegate->setFlags(self::convertToDelegateFlags($flags));
124
    }
125
126
    /**
127
     * @inheritdoc
128
     */
129 2
    public function getFlags()
130
    {
131 2
        return self::convertFromDelegateFlags($this->delegate->getFlags());
132
    }
133
134
    /**
135
     * @inheritdoc
136
     */
137
    public function getConsumerTag()
138
    {
139
        return $this->delegate->getConsumerTag();
0 ignored issues
show
Bug introduced by
The method getConsumerTag() does not exist on AMQPQueue. Did you maybe mean consume()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
140
    }
141
142
    /**
143
     * @inheritdoc
144
     */
145 1
    public function declareQueue()
146
    {
147 1
        return $this->delegate->declareQueue();
148
    }
149
150
    /**
151
     * @inheritdoc
152
     */
153 1
    public function bind($exchangeName, $routingKey, array $arguments = [])
154
    {
155 1
        return $this->delegate->bind($exchangeName, $routingKey, $arguments);
156
    }
157
158
    /**
159
     * @inheritdoc
160
     */
161
    public function unbind($exchangeName, $routingKey = null, array $arguments = [])
162
    {
163
        return $this->delegate->unbind($exchangeName, $routingKey, $arguments);
164
    }
165
166
    /**
167
     * @inheritdoc
168
     */
169 1 View Code Duplication
    public function get($flags = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
170
    {
171 1
        if (false !== $envelope = $this->delegate->get(self::convertToDelegateFlags($flags))) {
172 1
            $envelope = new Envelope($envelope);
0 ignored issues
show
Bug introduced by
It seems like $envelope defined by new \TreeHouse\Queue\Amq...mqp\Envelope($envelope) on line 172 can also be of type boolean; however, TreeHouse\Queue\Amqp\Dri...Envelope::__construct() does only seem to accept object<AMQPEnvelope>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
173
        }
174
175 1
        return $envelope;
176
    }
177
178
    /**
179
     * @inheritdoc
180
     */
181 View Code Duplication
    public function consume(callable $callback, $flags = null, $consumerTag = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
    {
183
        $wrapper = function (\AMQPEnvelope $envelope) use ($callback) {
184
            return $callback(new Envelope($envelope));
185
        };
186
187
        $this->delegate->consume($wrapper, self::convertToDelegateFlags($flags), $consumerTag);
188
    }
189
190
    /**
191
     * @inheritdoc
192
     */
193
    public function ack($deliveryTag, $flags = null)
194
    {
195
        return $this->delegate->ack($deliveryTag, self::convertToDelegateFlags($flags));
196
    }
197
198
    /**
199
     * @inheritdoc
200
     */
201
    public function nack($deliveryTag, $flags = AMQP_NOPARAM)
202
    {
203
        $this->delegate->nack($deliveryTag, self::convertToDelegateFlags($flags));
204
    }
205
206
    /**
207
     * @inheritdoc
208
     */
209
    public function reject($deliveryTag, $flags = AMQP_NOPARAM)
210
    {
211
        $this->delegate->reject($deliveryTag, self::convertToDelegateFlags($flags));
212
    }
213
214
    /**
215
     * @inheritdoc
216
     */
217
    public function cancel($consumerTag = '')
218
    {
219
        return $this->delegate->cancel($consumerTag);
220
    }
221
222
    /**
223
     * @inheritdoc
224
     */
225 3
    public function delete($flags = null)
226
    {
227 3
        $this->delegate->delete(self::convertToDelegateFlags($flags));
228 3
    }
229
230
    /**
231
     * @inheritdoc
232
     */
233
    public function purge()
234
    {
235
        $this->delegate->purge();
236
    }
237
238
    /**
239
     * @inheritdoc
240
     *
241
     * @return \AMQPQueue
242
     */
243
    public function getDelegate()
244
    {
245
        return $this->delegate;
246
    }
247
248
    /**
249
     * @param int|null $flags
250
     *
251
     * @return int
252
     */
253 4 View Code Duplication
    public static function convertToDelegateFlags($flags = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
254
    {
255 4
        if (null === $flags) {
256 3
            return AMQP_NOPARAM;
257
        }
258
259 3
        $converted = 0;
260
261 3
        foreach (self::$flagMap as $from => $to) {
262 3
            if ($flags & $from) {
263 3
                $converted |= $to;
264
            }
265
        }
266
267 3
        return $converted;
268
    }
269
270
    /**
271
     * @param int|null $flags
272
     *
273
     * @return int
274
     */
275 3 View Code Duplication
    public static function convertFromDelegateFlags($flags = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
276
    {
277 3
        if (null === $flags) {
278
            return self::NOPARAM;
279
        }
280
281 3
        $converted = 0;
282
283 3
        foreach (self::$flagMap as $from => $to) {
284 3
            if ($flags & $to) {
285 3
                $converted |= $from;
286
            }
287
        }
288
289 3
        return $converted;
290
    }
291
}
292