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

AcceptHeader::collect()   C

Complexity

Conditions 12
Paths 9

Size

Total Lines 42
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 12

Importance

Changes 0
Metric Value
cc 12
eloc 31
c 0
b 0
f 0
nc 9
nop 1
dl 0
loc 42
ccs 30
cts 30
cp 1
crap 12
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Http\Header;
6
7
use InvalidArgumentException;
8
use Yiisoft\Http\Header\Value\Accept\Accept;
9
use Yiisoft\Http\Header\Internal\BaseHeaderValue;
10
11
final class AcceptHeader extends Header
12
{
13
    // todo: comparing
14
    protected const DEFAULT_VALUE_CLASS = Accept::class;
15
16 14
    public function __construct(string $nameOrClass)
17
    {
18 14
        parent::__construct($nameOrClass);
19 14
        if (!is_a($this->headerClass, Accept::class, true)) {
20 1
            throw new InvalidArgumentException(
21 1
                sprintf("%s class is not an instance of %s", $this->headerClass, Accept::class)
22
            );
23
        }
24 13
    }
25
26
    /**
27
     * Add value in order
28
     */
29 12
    protected function collect(BaseHeaderValue $value): void
30
    {
31 12
        if (count($this->collection) === 0) {
32 12
            $this->collection[] = $value;
33 12
            return;
34
        }
35 11
        for ($pos = array_key_last($this->collection); $pos >= 0; --$pos) {
36 11
            $item = $this->collection[$pos];
37 11
            $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 11
            if ($result > 0) {
39 5
                break;
40 11
            } elseif ($result === 0) {
41 10
                $separator = $this->headerClass::VALUE_SEPARATOR;
42 10
                if ($separator !== '') {
43 6
                    $itemTypes = array_reverse(explode($separator, $item->getValue()));
44 6
                    $valueTypes = array_reverse(explode($separator, $value->getValue()));
45
                } else {
46 4
                    $itemTypes = [$item->getValue()];
47 4
                    $valueTypes = [$value->getValue()];
48
                }
49 10
                $result = count($itemTypes) <=> count($valueTypes);
50 10
                if ($result > 0) {
51 3
                    break;
52 10
                } elseif ($result === 0) {
53 9
                    foreach ($itemTypes as $part => $itemType) {
54 9
                        if ($itemType === '*' xor $valueTypes[$part] === '*') {
55 7
                            if ($itemType !== '*') {
56 4
                                break 2;
57
                            } else {
58 7
                                $this->collection[$pos + 1] = $item;
59 7
                                continue 2;
60
                            }
61
                        }
62
                    }
63 8
                    if (count($item->getParams()) >= count($value->getParams())) {
0 ignored issues
show
Bug introduced by
The method getParams() 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

63
                    if (count($item->getParams()) >= count($value->/** @scrutinizer ignore-call */ getParams())) {
Loading history...
64 7
                        break;
65
                    }
66
                }
67
            }
68 8
            $this->collection[$pos + 1] = $item;
69
        }
70 11
        $this->collection[$pos + 1] = $value;
71 11
    }
72
}
73