HeaderFrameParserTrait   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 120
Duplicated Lines 51.67 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 62
loc 120
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
F parseHeaderFrame() 62 113 16

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 PHPDaemon\Clients\AMQP\Driver\Protocol\v091\Parser;
4
5
use PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException;
6
use PHPDaemon\Clients\AMQP\Driver\Protocol\v091\Protocol\Basic;
7
8
/**
9
 * Class HeaderFrameParserTrait
10
 *
11
 * @property TableInterface tableParser
12
 * @author Aleksey I. Kuleshov YOU GLOBAL LIMITED
13
 * @package PHPDaemon\Clients\AMQP\Driver\Protocol\v091\Parser
14
 */
15
trait HeaderFrameParserTrait
16
{
17
    /**
18
     * @return Basic\BasicHeaderFrame
19
     * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException
20
     */
21
    private function parseHeaderFrame()
22
    {
23
        $fields = \unpack('na/nb/Jc/nd', $this->buffer);
0 ignored issues
show
Bug introduced by
The property buffer does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
        $this->buffer = mb_orig_substr($this->buffer, 14);
25
26
        $class = $fields['a'];
27
        $flags = $fields['d'];
28
29
        // class "basic"
30
        if ($class === 60) {
31
            $frame = new Basic\BasicHeaderFrame();
32
            $frame->contentLength = $fields['c'];
33
34
            // consume "content-type" (shortstr)
35 View Code Duplication
            if ($flags & 32768) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
36
                $length = \ord($this->buffer);
37
                $frame->contentType = mb_orig_substr($this->buffer, 1, $length);
38
                $this->buffer = mb_orig_substr($this->buffer, 1 + $length);
39
            }
40
41
            // consume "content-encoding" (shortstr)
42 View Code Duplication
            if ($flags & 16384) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
43
                $length = \ord($this->buffer);
44
                $frame->contentEncoding = mb_orig_substr($this->buffer, 1, $length);
45
                $this->buffer = mb_orig_substr($this->buffer, 1 + $length);
46
            }
47
48
            // consume "headers" (table)e
49
            if ($flags & 8192) {
50
                $frame->headers = $this->tableParser->parse($this->buffer);
51
            }
52
53
            // consume "delivery-mode" (octet)
54 View Code Duplication
            if ($flags & 4096) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
55
                list(, $frame->deliveryMode) = \unpack('c', $this->buffer);
56
                $this->buffer = mb_orig_substr($this->buffer, 1);
57
            }
58
59
            // consume "priority" (octet)
60 View Code Duplication
            if ($flags & 2048) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
61
                list(, $frame->priority) = \unpack('c', $this->buffer);
62
                $this->buffer = mb_orig_substr($this->buffer, 1);
63
            }
64
65
            // consume "correlation-id" (shortstr)
66 View Code Duplication
            if ($flags & 1024) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
67
                $length = \ord($this->buffer);
68
                $frame->correlationId = mb_orig_substr($this->buffer, 1, $length);
69
                $this->buffer = mb_orig_substr($this->buffer, 1 + $length);
70
            }
71
72
            // consume "reply-to" (shortstr)
73 View Code Duplication
            if ($flags & 512) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
74
                $length = \ord($this->buffer);
75
                $frame->replyTo = mb_orig_substr($this->buffer, 1, $length);
76
                $this->buffer = mb_orig_substr($this->buffer, 1 + $length);
77
            }
78
79
            // consume "expiration" (shortstr)
80 View Code Duplication
            if ($flags & 256) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
81
                $length = \ord($this->buffer);
82
                $frame->expiration = mb_orig_substr($this->buffer, 1, $length);
83
                $this->buffer = mb_orig_substr($this->buffer, 1 + $length);
84
            }
85
86
            // consume "message-id" (shortstr)
87 View Code Duplication
            if ($flags & 128) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
88
                $length = \ord($this->buffer);
89
                $frame->messageId = mb_orig_substr($this->buffer, 1, $length);
90
                $this->buffer = mb_orig_substr($this->buffer, 1 + $length);
91
            }
92
93
            // consume "timestamp" (timestamp)
94 View Code Duplication
            if ($flags & 64) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
95
                list(, $frame->timestamp) = \unpack('J', $this->buffer);
96
                $this->buffer = mb_orig_substr($this->buffer, 8);
97
            }
98
99
            // consume "type" (shortstr)
100 View Code Duplication
            if ($flags & 32) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
101
                $length = \ord($this->buffer);
102
                $frame->type = mb_orig_substr($this->buffer, 1, $length);
103
                $this->buffer = mb_orig_substr($this->buffer, 1 + $length);
104
            }
105
106
            // consume "user-id" (shortstr)
107 View Code Duplication
            if ($flags & 16) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
108
                $length = \ord($this->buffer);
109
                $frame->userId = mb_orig_substr($this->buffer, 1, $length);
110
                $this->buffer = mb_orig_substr($this->buffer, 1 + $length);
111
            }
112
113
            // consume "app-id" (shortstr)
114 View Code Duplication
            if ($flags & 8) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
115
                $length = \ord($this->buffer);
116
                $frame->appId = mb_orig_substr($this->buffer, 1, $length);
117
                $this->buffer = mb_orig_substr($this->buffer, 1 + $length);
118
            }
119
120
            // consume "cluster-id" (shortstr)
121 View Code Duplication
            if ($flags & 4) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
122
                $length = \ord($this->buffer);
123
                $frame->clusterId = mb_orig_substr($this->buffer, 1, $length);
124
                $this->buffer = mb_orig_substr($this->buffer, 1 + $length);
125
            }
126
127
            return $frame;
128
        }
129
130
        throw new AMQPProtocolException(
131
            'Frame class (' . $class . ') is invalid or does not support content frames.'
132
        );
133
    }
134
}
135