Passed
Pull Request — master (#22)
by Aleksei
02:39
created

SortableHeader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 17
c 1
b 0
f 1
dl 0
loc 33
ccs 18
cts 18
cp 1
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A collect() 0 16 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Http\Header;
6
7
use InvalidArgumentException;
8
use Yiisoft\Http\Header\Internal\BaseHeaderValue;
9
use Yiisoft\Http\Header\Internal\WithParamsHeaderValue;
10
use Yiisoft\Http\Header\Value\Unnamed\SortedValue;
11
12
final class SortableHeader extends Header
13
{
14
    protected const DEFAULT_VALUE_CLASS = SortedValue::class;
15
16 6
    public function __construct(string $nameOrClass)
17
    {
18 6
        parent::__construct($nameOrClass);
19 6
        if (!is_subclass_of($this->headerClass, WithParamsHeaderValue::class, true)) {
20 1
            throw new InvalidArgumentException(
21 1
                sprintf('%s class does not implement %s', $this->headerClass, WithParamsHeaderValue::class)
22
            );
23
        }
24 5
    }
25
26
    /**
27
     * Add value in order
28
     */
29 3
    protected function collect(BaseHeaderValue $value): void
30
    {
31 3
        if (count($this->collection) === 0) {
32 3
            $this->collection[] = $value;
33 3
            return;
34
        }
35 2
        for ($pos = array_key_last($this->collection); $pos >= 0; --$pos) {
36 2
            $item = $this->collection[$pos];
37 2
            $result = (float)$item->getQuality() <=> (float)$value->getQuality();
0 ignored issues
show
Bug introduced by
The method getQuality() does not exist on Yiisoft\Http\Header\Internal\BaseHeaderValue. It seems like you code against a sub-type of Yiisoft\Http\Header\Internal\BaseHeaderValue such as Yiisoft\Http\Header\Internal\WithParamsHeaderValue. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
            $result = (float)$item->getQuality() <=> (float)$value->/** @scrutinizer ignore-call */ getQuality();
Loading history...
38 2
            if ($result >= 0) {
39 2
                $this->collection[$pos + 1] = $value;
40 2
                return;
41
            }
42 2
            $this->collection[$pos + 1] = $item;
43
        }
44 2
        $this->collection[0] = $value;
45 2
    }
46
}
47