PriorityTrait::serializePriorityData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
namespace Hyphper\Frame;
4
5
/**
6
 * Trait for frames that contain priority data. Defines extra fields that can
7
 * be used and set by frames that contain priority data.
8
 *
9
 * @package Hyphper\Frame
10
 */
11
trait PriorityTrait
12
{
13
    protected $depends_on;
14
    protected $stream_weight;
15
    protected $exclusive;
16
17 6 View Code Duplication
    public function __construct(array $options = [])
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...
18
    {
19 6
        parent::__construct($options);
20 5
        $this->depends_on = (int) ($options['depends_on'] ?? 0);
21 5
        $this->stream_weight = (int) ($options['stream_weight'] ?? 0);
22 5
        $this->exclusive = (bool) ($options['exclusive'] ?? false);
23 5
    }
24
25
    /**
26
     * @return bool
27
     */
28 2
    public function getExclusive()
29
    {
30 2
        return $this->exclusive;
31
    }
32
33
    /**
34
     * @param bool $exclusive
35
     *
36
     * @return PriorityTrait
0 ignored issues
show
Comprehensibility Bug introduced by
The return type PriorityTrait is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
37
     */
38 2
    public function setExclusive(bool $exclusive)
39
    {
40 2
        $this->exclusive = $exclusive;
41
42 2
        return $this;
43
    }
44
45
    /**
46
     * @return int
47
     */
48 2
    public function getDependsOn()
49
    {
50 2
        return $this->depends_on;
51
    }
52
53
    /**
54
     * @param int $depends_on
55
     *
56
     * @return PriorityTrait
0 ignored issues
show
Comprehensibility Bug introduced by
The return type PriorityTrait is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
57
     */
58 2
    public function setDependsOn($depends_on)
59
    {
60 2
        $this->depends_on = $depends_on;
61
62 2
        return $this;
63
    }
64
65
    /**
66
     * @return int
67
     */
68 2
    public function getStreamWeight()
69
    {
70 2
        return $this->stream_weight;
71
    }
72
73
    /**
74
     * @param int $stream_weight
75
     *
76
     * @return PriorityTrait
0 ignored issues
show
Comprehensibility Bug introduced by
The return type PriorityTrait is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
77
     */
78 2
    public function setStreamWeight($stream_weight)
79
    {
80 2
        $this->stream_weight = $stream_weight;
81
82 2
        return $this;
83
    }
84
85 3
    protected function serializePriorityData()
86
    {
87 3
        return pack(
88 3
            'NC',
89 3
            $this->depends_on | ((int) ($this->exclusive) << 31),
90 3
            $this->stream_weight
91
        );
92
    }
93
94 3
    protected function parsePriorityData(string $data): int
95
    {
96 3
        if ($unpack = @unpack('Ndepends_on/Cstream_weight', substr($data, 0, 5))) {
97 2
            $this->depends_on = $unpack['depends_on'];
98 2
            $this->stream_weight = $unpack['stream_weight'];
99 2
            $this->exclusive = (bool) ($this->depends_on & PriorityInterface::MASK);
100 2
            $this->depends_on &= ~PriorityInterface::MASK;
101
102 2
            return 5;
103
        }
104
105 1
        throw new \Hyphper\Frame\Exception\InvalidFrameException('Invalid Priority data');
106
    }
107
}
108