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

SortableHeader::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 2
rs 10
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