Completed
Push — master ( fdb7de...e17409 )
by Sergey
03:32
created

Frame   F

Complexity

Total Complexity 76

Size/Duplication

Total Lines 535
Duplicated Lines 37.94 %

Coupling/Cohesion

Components 0
Dependencies 75

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 76
c 1
b 0
f 0
lcom 0
cbo 75
dl 203
loc 535
rs 1.3043

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Frame often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Frame, and based on these observations, apply Extract Interface, too.

1
<?php
2
/*
3
 * This file is automatically generated.
4
 */
5
6
namespace ButterAMQP\Framing;
7
8
use ButterAMQP\Buffer;
9
use ButterAMQP\Binary;
10
use ButterAMQP\Value;
11
12
/**
13
 * @codeCoverageIgnore
14
 */
15
abstract class Frame
16
{
17
    /**
18
     * @var int
19
     */
20
    protected $channel;
21
22
    /**
23
     * @param int $channel
24
     */
25
    public function __construct($channel)
26
    {
27
        $this->channel = $channel;
28
    }
29
30
    /**
31
     * @return int
32
     */
33
    public function getChannel()
34
    {
35
        return $this->channel;
36
    }
37
38
    /**
39
     * @return string
40
     */
41
    abstract public function encode();
42
43
    /**
44
     * @param Buffer $data
45
     *
46
     * @return $this
47
     */
48
    public static function decode(Buffer $data)
49
    {
50
        $header = unpack('Ctype/nchannel/Nsize', $data->read(7));
51
52
        if ($header['type'] === 1) {
53
            $class = $data->read(2);
54
            $method = $data->read(2);
55
56
            if ($class === "\x00\x0A") {
57
                if ($method === "\x00\x0A") {
58
                    return new Method\ConnectionStart(
59
                        $header['channel'],
60
                        Value\OctetValue::decode($data),
61
                        Value\OctetValue::decode($data),
62
                        Value\TableValue::decode($data),
63
                        Value\LongStringValue::decode($data),
64
                        Value\LongStringValue::decode($data)
65
                    );
66
                }
67
                if ($method === "\x00\x0B") {
68
                    return new Method\ConnectionStartOk(
69
                        $header['channel'],
70
                        Value\TableValue::decode($data),
71
                        Value\ShortStringValue::decode($data),
72
                        Value\LongStringValue::decode($data),
73
                        Value\ShortStringValue::decode($data)
74
                    );
75
                }
76
                if ($method === "\x00\x14") {
77
                    return new Method\ConnectionSecure(
78
                        $header['channel'],
79
                        Value\LongStringValue::decode($data)
80
                    );
81
                }
82
                if ($method === "\x00\x15") {
83
                    return new Method\ConnectionSecureOk(
84
                        $header['channel'],
85
                        Value\LongStringValue::decode($data)
86
                    );
87
                }
88
                if ($method === "\x00\x1E") {
89
                    return new Method\ConnectionTune(
90
                        $header['channel'],
91
                        Value\ShortValue::decode($data),
92
                        Value\LongValue::decode($data),
93
                        Value\ShortValue::decode($data)
94
                    );
95
                }
96
                if ($method === "\x00\x1F") {
97
                    return new Method\ConnectionTuneOk(
98
                        $header['channel'],
99
                        Value\ShortValue::decode($data),
100
                        Value\LongValue::decode($data),
101
                        Value\ShortValue::decode($data)
102
                    );
103
                }
104
                if ($method === "\x00\x28") {
105
                    return new Method\ConnectionOpen(
106
                        $header['channel'],
107
                        Value\ShortStringValue::decode($data),
108
                        Value\ShortStringValue::decode($data),
109
                        Value\BooleanValue::decode($data)
110
                    );
111
                }
112
                if ($method === "\x00\x29") {
113
                    return new Method\ConnectionOpenOk(
114
                        $header['channel'],
115
                        Value\ShortStringValue::decode($data)
116
                    );
117
                }
118
                if ($method === "\x00\x32") {
119
                    return new Method\ConnectionClose(
120
                        $header['channel'],
121
                        Value\ShortValue::decode($data),
122
                        Value\ShortStringValue::decode($data),
123
                        Value\ShortValue::decode($data),
124
                        Value\ShortValue::decode($data)
125
                    );
126
                }
127
                if ($method === "\x00\x33") {
128
                    return new Method\ConnectionCloseOk(
129
                        $header['channel']
130
                    );
131
                }
132
                if ($method === "\x00\x3C") {
133
                    return new Method\ConnectionBlocked(
134
                        $header['channel'],
135
                        Value\ShortStringValue::decode($data)
136
                    );
137
                }
138
                if ($method === "\x00\x3D") {
139
                    return new Method\ConnectionUnblocked(
140
                        $header['channel']
141
                    );
142
                }
143
            }
144
145
            if ($class === "\x00\x14") {
146
                if ($method === "\x00\x0A") {
147
                    return new Method\ChannelOpen(
148
                        $header['channel'],
149
                        Value\ShortStringValue::decode($data)
150
                    );
151
                }
152
                if ($method === "\x00\x0B") {
153
                    return new Method\ChannelOpenOk(
154
                        $header['channel'],
155
                        Value\LongStringValue::decode($data)
156
                    );
157
                }
158
                if ($method === "\x00\x14") {
159
                    return new Method\ChannelFlow(
160
                        $header['channel'],
161
                        Value\BooleanValue::decode($data)
162
                    );
163
                }
164
                if ($method === "\x00\x15") {
165
                    return new Method\ChannelFlowOk(
166
                        $header['channel'],
167
                        Value\BooleanValue::decode($data)
168
                    );
169
                }
170
                if ($method === "\x00\x28") {
171
                    return new Method\ChannelClose(
172
                        $header['channel'],
173
                        Value\ShortValue::decode($data),
174
                        Value\ShortStringValue::decode($data),
175
                        Value\ShortValue::decode($data),
176
                        Value\ShortValue::decode($data)
177
                    );
178
                }
179
                if ($method === "\x00\x29") {
180
                    return new Method\ChannelCloseOk(
181
                        $header['channel']
182
                    );
183
                }
184
            }
185
186
            if ($class === "\x00\x28") {
187
                if ($method === "\x00\x0A") {
188
                    return new Method\ExchangeDeclare(
189
                        $header['channel'],
190
                        Value\ShortValue::decode($data),
191
                        Value\ShortStringValue::decode($data),
192
                        Value\ShortStringValue::decode($data),
193
                        (bool) ($flags = Value\OctetValue::decode($data)) & 1,
194
                        (bool) $flags & 2,
195
                        (bool) $flags & 4,
196
                        (bool) $flags & 8,
197
                        (bool) $flags & 16,
198
                        Value\TableValue::decode($data)
199
                    );
200
                }
201
                if ($method === "\x00\x0B") {
202
                    return new Method\ExchangeDeclareOk(
203
                        $header['channel']
204
                    );
205
                }
206
                if ($method === "\x00\x14") {
207
                    return new Method\ExchangeDelete(
208
                        $header['channel'],
209
                        Value\ShortValue::decode($data),
210
                        Value\ShortStringValue::decode($data),
211
                        (bool) ($flags = Value\OctetValue::decode($data)) & 1,
212
                        (bool) $flags & 2
213
                    );
214
                }
215
                if ($method === "\x00\x15") {
216
                    return new Method\ExchangeDeleteOk(
217
                        $header['channel']
218
                    );
219
                }
220
                if ($method === "\x00\x1E") {
221
                    return new Method\ExchangeBind(
222
                        $header['channel'],
223
                        Value\ShortValue::decode($data),
224
                        Value\ShortStringValue::decode($data),
225
                        Value\ShortStringValue::decode($data),
226
                        Value\ShortStringValue::decode($data),
227
                        Value\BooleanValue::decode($data),
228
                        Value\TableValue::decode($data)
229
                    );
230
                }
231
                if ($method === "\x00\x1F") {
232
                    return new Method\ExchangeBindOk(
233
                        $header['channel']
234
                    );
235
                }
236
                if ($method === "\x00\x28") {
237
                    return new Method\ExchangeUnbind(
238
                        $header['channel'],
239
                        Value\ShortValue::decode($data),
240
                        Value\ShortStringValue::decode($data),
241
                        Value\ShortStringValue::decode($data),
242
                        Value\ShortStringValue::decode($data),
243
                        Value\BooleanValue::decode($data),
244
                        Value\TableValue::decode($data)
245
                    );
246
                }
247
                if ($method === "\x00\x33") {
248
                    return new Method\ExchangeUnbindOk(
249
                        $header['channel']
250
                    );
251
                }
252
            }
253
254
            if ($class === "\x00\x32") {
255
                if ($method === "\x00\x0A") {
256
                    return new Method\QueueDeclare(
257
                        $header['channel'],
258
                        Value\ShortValue::decode($data),
259
                        Value\ShortStringValue::decode($data),
260
                        (bool) ($flags = Value\OctetValue::decode($data)) & 1,
261
                        (bool) $flags & 2,
262
                        (bool) $flags & 4,
263
                        (bool) $flags & 8,
264
                        (bool) $flags & 16,
265
                        Value\TableValue::decode($data)
266
                    );
267
                }
268
                if ($method === "\x00\x0B") {
269
                    return new Method\QueueDeclareOk(
270
                        $header['channel'],
271
                        Value\ShortStringValue::decode($data),
272
                        Value\LongValue::decode($data),
273
                        Value\LongValue::decode($data)
274
                    );
275
                }
276
                if ($method === "\x00\x14") {
277
                    return new Method\QueueBind(
278
                        $header['channel'],
279
                        Value\ShortValue::decode($data),
280
                        Value\ShortStringValue::decode($data),
281
                        Value\ShortStringValue::decode($data),
282
                        Value\ShortStringValue::decode($data),
283
                        Value\BooleanValue::decode($data),
284
                        Value\TableValue::decode($data)
285
                    );
286
                }
287
                if ($method === "\x00\x15") {
288
                    return new Method\QueueBindOk(
289
                        $header['channel']
290
                    );
291
                }
292
                if ($method === "\x00\x32") {
293
                    return new Method\QueueUnbind(
294
                        $header['channel'],
295
                        Value\ShortValue::decode($data),
296
                        Value\ShortStringValue::decode($data),
297
                        Value\ShortStringValue::decode($data),
298
                        Value\ShortStringValue::decode($data),
299
                        Value\TableValue::decode($data)
300
                    );
301
                }
302
                if ($method === "\x00\x33") {
303
                    return new Method\QueueUnbindOk(
304
                        $header['channel']
305
                    );
306
                }
307
                if ($method === "\x00\x1E") {
308
                    return new Method\QueuePurge(
309
                        $header['channel'],
310
                        Value\ShortValue::decode($data),
311
                        Value\ShortStringValue::decode($data),
312
                        Value\BooleanValue::decode($data)
313
                    );
314
                }
315
                if ($method === "\x00\x1F") {
316
                    return new Method\QueuePurgeOk(
317
                        $header['channel'],
318
                        Value\LongValue::decode($data)
319
                    );
320
                }
321
                if ($method === "\x00\x28") {
322
                    return new Method\QueueDelete(
323
                        $header['channel'],
324
                        Value\ShortValue::decode($data),
325
                        Value\ShortStringValue::decode($data),
326
                        (bool) ($flags = Value\OctetValue::decode($data)) & 1,
327
                        (bool) $flags & 2,
328
                        (bool) $flags & 4
329
                    );
330
                }
331
                if ($method === "\x00\x29") {
332
                    return new Method\QueueDeleteOk(
333
                        $header['channel'],
334
                        Value\LongValue::decode($data)
335
                    );
336
                }
337
            }
338
339
            if ($class === "\x00\x3C") {
340
                if ($method === "\x00\x0A") {
341
                    return new Method\BasicQos(
342
                        $header['channel'],
343
                        Value\LongValue::decode($data),
344
                        Value\ShortValue::decode($data),
345
                        Value\BooleanValue::decode($data)
346
                    );
347
                }
348
                if ($method === "\x00\x0B") {
349
                    return new Method\BasicQosOk(
350
                        $header['channel']
351
                    );
352
                }
353
                if ($method === "\x00\x14") {
354
                    return new Method\BasicConsume(
355
                        $header['channel'],
356
                        Value\ShortValue::decode($data),
357
                        Value\ShortStringValue::decode($data),
358
                        Value\ShortStringValue::decode($data),
359
                        (bool) ($flags = Value\OctetValue::decode($data)) & 1,
360
                        (bool) $flags & 2,
361
                        (bool) $flags & 4,
362
                        (bool) $flags & 8,
363
                        Value\TableValue::decode($data)
364
                    );
365
                }
366
                if ($method === "\x00\x15") {
367
                    return new Method\BasicConsumeOk(
368
                        $header['channel'],
369
                        Value\ShortStringValue::decode($data)
370
                    );
371
                }
372
                if ($method === "\x00\x1E") {
373
                    return new Method\BasicCancel(
374
                        $header['channel'],
375
                        Value\ShortStringValue::decode($data),
376
                        Value\BooleanValue::decode($data)
377
                    );
378
                }
379
                if ($method === "\x00\x1F") {
380
                    return new Method\BasicCancelOk(
381
                        $header['channel'],
382
                        Value\ShortStringValue::decode($data)
383
                    );
384
                }
385
                if ($method === "\x00\x28") {
386
                    return new Method\BasicPublish(
387
                        $header['channel'],
388
                        Value\ShortValue::decode($data),
389
                        Value\ShortStringValue::decode($data),
390
                        Value\ShortStringValue::decode($data),
391
                        (bool) ($flags = Value\OctetValue::decode($data)) & 1,
392
                        (bool) $flags & 2
393
                    );
394
                }
395
                if ($method === "\x00\x32") {
396
                    return new Method\BasicReturn(
397
                        $header['channel'],
398
                        Value\ShortValue::decode($data),
399
                        Value\ShortStringValue::decode($data),
400
                        Value\ShortStringValue::decode($data),
401
                        Value\ShortStringValue::decode($data)
402
                    );
403
                }
404
                if ($method === "\x00\x3C") {
405
                    return new Method\BasicDeliver(
406
                        $header['channel'],
407
                        Value\ShortStringValue::decode($data),
408
                        Value\LongLongValue::decode($data),
409
                        Value\BooleanValue::decode($data),
410
                        Value\ShortStringValue::decode($data),
411
                        Value\ShortStringValue::decode($data)
412
                    );
413
                }
414
                if ($method === "\x00\x46") {
415
                    return new Method\BasicGet(
416
                        $header['channel'],
417
                        Value\ShortValue::decode($data),
418
                        Value\ShortStringValue::decode($data),
419
                        Value\BooleanValue::decode($data)
420
                    );
421
                }
422
                if ($method === "\x00\x47") {
423
                    return new Method\BasicGetOk(
424
                        $header['channel'],
425
                        Value\LongLongValue::decode($data),
426
                        Value\BooleanValue::decode($data),
427
                        Value\ShortStringValue::decode($data),
428
                        Value\ShortStringValue::decode($data),
429
                        Value\LongValue::decode($data)
430
                    );
431
                }
432
                if ($method === "\x00\x48") {
433
                    return new Method\BasicGetEmpty(
434
                        $header['channel'],
435
                        Value\ShortStringValue::decode($data)
436
                    );
437
                }
438
                if ($method === "\x00\x50") {
439
                    return new Method\BasicAck(
440
                        $header['channel'],
441
                        Value\LongLongValue::decode($data),
442
                        Value\BooleanValue::decode($data)
443
                    );
444
                }
445
                if ($method === "\x00\x5A") {
446
                    return new Method\BasicReject(
447
                        $header['channel'],
448
                        Value\LongLongValue::decode($data),
449
                        Value\BooleanValue::decode($data)
450
                    );
451
                }
452
                if ($method === "\x00\x64") {
453
                    return new Method\BasicRecoverAsync(
454
                        $header['channel'],
455
                        Value\BooleanValue::decode($data)
456
                    );
457
                }
458
                if ($method === "\x00\x6E") {
459
                    return new Method\BasicRecover(
460
                        $header['channel'],
461
                        Value\BooleanValue::decode($data)
462
                    );
463
                }
464
                if ($method === "\x00\x6F") {
465
                    return new Method\BasicRecoverOk(
466
                        $header['channel']
467
                    );
468
                }
469
                if ($method === "\x00\x78") {
470
                    return new Method\BasicNack(
471
                        $header['channel'],
472
                        Value\LongLongValue::decode($data),
473
                        (bool) ($flags = Value\OctetValue::decode($data)) & 1,
474
                        (bool) $flags & 2
475
                    );
476
                }
477
            }
478
479
            if ($class === "\x00\x5A") {
480
                if ($method === "\x00\x0A") {
481
                    return new Method\TxSelect(
482
                        $header['channel']
483
                    );
484
                }
485
                if ($method === "\x00\x0B") {
486
                    return new Method\TxSelectOk(
487
                        $header['channel']
488
                    );
489
                }
490
                if ($method === "\x00\x14") {
491
                    return new Method\TxCommit(
492
                        $header['channel']
493
                    );
494
                }
495
                if ($method === "\x00\x15") {
496
                    return new Method\TxCommitOk(
497
                        $header['channel']
498
                    );
499
                }
500
                if ($method === "\x00\x1E") {
501
                    return new Method\TxRollback(
502
                        $header['channel']
503
                    );
504
                }
505
                if ($method === "\x00\x1F") {
506
                    return new Method\TxRollbackOk(
507
                        $header['channel']
508
                    );
509
                }
510
            }
511
512
            if ($class === "\x00\x55") {
513
                if ($method === "\x00\x0A") {
514
                    return new Method\ConfirmSelect(
515
                        $header['channel'],
516
                        Value\BooleanValue::decode($data)
517
                    );
518
                }
519
                if ($method === "\x00\x0B") {
520
                    return new Method\ConfirmSelectOk(
521
                        $header['channel']
522
                    );
523
                }
524
            }
525
526
            throw new \InvalidArgumentException(sprintf(
527
                'Invalid method received %d:%d',
528
                Binary::unpackbe('s', $class),
529
                Binary::unpackbe('s', $method)
530
            ));
531
        } elseif ($header['type'] === 2) {
532
            $parameters = unpack('nclass/nweight/Jsize', $data->read(12));
533
534
            return new Header(
535
                $header['channel'],
536
                $parameters['class'],
537
                $parameters['weight'],
538
                $parameters['size'],
539
                Properties::decode($data)
540
            );
541
        } elseif ($header['type'] === 3) {
542
            return new Content($header['channel'], $data->read($header['size']));
543
        } elseif ($header['type'] === 8) {
544
            return new Heartbeat($header['channel']);
545
        }
546
547
        throw new \InvalidArgumentException(sprintf('Invalid frame type %d', $header['type']));
548
    }
549
}
550