PaddingTrait::parsePaddingData()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
declare(strict_types=1);
3
namespace Hyphper\Frame;
4
5
trait PaddingTrait
6
{
7
    protected $padding_length;
8
9
    public function __construct(array $options = [])
10
    {
11
        parent::__construct($options);
12
        $this->padding_length = (int) ($options['padding_length'] ?? 0);
13
    }
14
15
    /**
16
     * @param int $padding_length
17
     */
18 1
    public function setPaddingLength(int $padding_length)
19
    {
20 1
        $this->padding_length = $padding_length;
21 1
    }
22
23
    /**
24
     * @return int
25
     */
26 2
    public function getPaddingLength()
27
    {
28 2
        return $this->padding_length;
29
    }
30
31
    /**
32
     * @return string
33
     */
34 10
    protected function serializePaddingData(): string
35
    {
36 10
        if ($this->flags->hasFlag(Flag::PADDED)) {
0 ignored issues
show
Bug introduced by
The property flags 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...
37 1
            return pack('C', $this->padding_length);
38
        }
39
40 9
        return '';
41
    }
42
43
    /**
44
     * @param string $data
45
     *
46
     * @return int
47
     * @throws Exception\InvalidFrameException
48
     */
49 15
    protected function parsePaddingData(string $data): int
50
    {
51 15
        if ($this->flags->hasFlag(Flag::PADDED)) {
52 6
            if (!$unpack = @unpack('Cpadding_length', substr($data, 0, 1))) {
53 1
                throw new \Hyphper\Frame\Exception\InvalidFrameException("Invalid Padding Data");
54
            }
55
56 5
            $this->padding_length = $unpack['padding_length'];
57
58 5
            return static::IS_PADDED;
59
        }
60
61 9
        return static::IS_NOT_PADDED;
62
    }
63
}
64