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

SortableHeader::collect()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 11
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 16
ccs 12
cts 12
cp 1
crap 4
rs 9.9
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