Params   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 0
loc 102
c 0
b 0
f 0
ccs 44
cts 44
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getValues() 0 4 1
B unpackPayload() 0 37 8
B packPayload() 0 27 6
1
<?php declare(strict_types=1);
2
3
namespace Lisachenko\Protocol\FCGI\Record;
4
5
use Lisachenko\Protocol\FCGI;
6
use Lisachenko\Protocol\FCGI\Record;
7
8
/**
9
 * Params request record
10
 *
11
 * @author Alexander.Lisachenko
12
 */
13
class Params extends Record
14
{
15
16
    /**
17
     * List of params
18
     *
19
     * @var array
20
     */
21
    protected $values = [];
22
23
    /**
24
     * Constructs a param request
25
     *
26
     * @param array $values
27
     */
28
    public function __construct(array $values = [])
29
    {
30 7
        $this->type = FCGI::PARAMS;
31
        $this->values = $values;
32 7
        $this->setContentData($this->packPayload());
33 7
    }
34 7
35 7
    /**
36
     * Returns an associative list of parameters
37
     */
38
    public function getValues(): array
39
    {
40
        return $this->values;
41
    }
42 6
43
    /**
44 6
     * {@inheritdoc}
45
     * @param static $self
46
     */
47
    protected static function unpackPayload($self, string $data): void
48
    {
49
        $currentOffset = 0;
50
        do {
51
            [$nameLengthHigh] = array_values(unpack('CnameLengthHigh', $data));
0 ignored issues
show
Bug introduced by
The variable $nameLengthHigh does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
52
            $isLongName = ($nameLengthHigh >> 7 == 1);
53
            $valueOffset = $isLongName ? 4 : 1;
54
55 3
            [$valueLengthHigh] = array_values(unpack('CvalueLengthHigh', substr($data, $valueOffset)));
0 ignored issues
show
Bug introduced by
The variable $valueLengthHigh does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
56
            $isLongValue = ($valueLengthHigh >> 7 == 1);
57 3
            $dataOffset = $valueOffset + ($isLongValue ? 4 : 1);
58
59 3
            $formatParts = [
60 3
                $isLongName ? 'NnameLength' : 'CnameLength',
61 3
                $isLongValue ? 'NvalueLength' : 'CvalueLength',
62
            ];
63 3
            $format = join('/', $formatParts);
64 3
            [$nameLength, $valueLength] = array_values(unpack($format, $data));
0 ignored issues
show
Bug introduced by
The variable $nameLength does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $valueLength does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
65 3
66
            // Clear top bit for long record
67
            $nameLength &= ($isLongName ? 0x7fffffff : 0x7f);
68 3
            $valueLength &= ($isLongValue ? 0x7fffffff : 0x7f);
69 3
70 3
            [$nameData, $valueData] = array_values(
0 ignored issues
show
Bug introduced by
The variable $nameData does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $valueData does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
71 3
                unpack(
72
                    "a{$nameLength}nameData/a{$valueLength}valueData",
73
                    substr($data, $dataOffset)
74
                )
75 3
            );
76
77
            $self->values[$nameData] = $valueData;
78 3
79 3
            $keyValueLength = $dataOffset + $nameLength + $valueLength;
80
            $data = substr($data, $keyValueLength);
81 3
            $currentOffset += $keyValueLength;
82 3
        } while ($currentOffset < $self->getContentLength());
83 3
    }
84 3
85
    /** {@inheritdoc} */
86 3
    protected function packPayload(): string
87
    {
88 3
        $payload = '';
89 3
        foreach ($this->values as $nameData => $valueData) {
90 3
            $nameLength = strlen($nameData);
91 3
            $valueLength = strlen((string) $valueData);
92
            $isLongName = $nameLength > 127;
93 3
            $isLongValue = $valueLength > 127;
94
            $formatParts = [
95
                $isLongName ? 'N' : 'C',
96
                $isLongValue ? 'N' : 'C',
97
                "a{$nameLength}",
98
                "a{$valueLength}",
99
            ];
100
            $format = join('', $formatParts);
101 7
102
            $payload .= pack(
103 7
                $format,
104 7
                $isLongName ? ($nameLength | 0x80000000) : $nameLength,
105 3
                $isLongValue ? ($valueLength | 0x80000000) : $valueLength,
106 3
                $nameData,
107 3
                $valueData
108 3
            );
109
        }
110 3
111 3
        return $payload;
112 3
    }
113 3
114
}
115