PaddingTrait   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 59
ccs 16
cts 20
cp 0.8
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setPaddingLength() 0 4 1
A getPaddingLength() 0 4 1
A serializePaddingData() 0 8 2
A parsePaddingData() 0 14 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