HeaderFrameParserTrait::parseHeaderFrame()   F
last analyzed

Complexity

Conditions 16
Paths 16385

Size

Total Lines 113

Duplication

Lines 62
Ratio 54.87 %

Importance

Changes 0
Metric Value
cc 16
dl 62
loc 113
rs 1.1198
c 0
b 0
f 0
nc 16385
nop 0

How to fix   Long Method    Complexity   

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:

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