Passed
Pull Request — master (#22)
by Aleksei
07:47
created

DirectiveHeader   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 33.33%

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 44
ccs 6
cts 18
cp 0.3333
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A withDirective() 0 7 1
A getDirectives() 0 11 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\DirectivesHeaderValue;
9
use Yiisoft\Http\Header\Value\Unnamed\DirectiveValue;
10
11
final class DirectiveHeader extends Header
12
{
13
    protected const DEFAULT_VALUE_CLASS = DirectiveValue::class;
14
15 6
    public function __construct(string $nameOrClass)
16
    {
17 6
        parent::__construct($nameOrClass);
18 6
        if (!is_a($this->headerClass, DirectivesHeaderValue::class, true)) {
19 1
            throw new InvalidArgumentException(
20 1
                sprintf("%s class is not an instance of %s", $this->headerClass, DirectivesHeaderValue::class)
21
            );
22
        }
23 5
    }
24
25
    /**
26
     * @param string $directive
27
     * @param string|null $argument
28
     *
29
     * @throws InvalidArgumentException
30
     */
31
    public function withDirective(string $directive, string $argument = null): self
32
    {
33
        $clone = clone $this;
34
        /** @var DirectivesHeaderValue $headerValue */
35
        $headerValue = new $this->headerClass();
36
        $clone->addValue($headerValue->withDirective($directive, $argument));
37
        return $clone;
38
    }
39
40
    /**
41
     * @param bool $ignoreIncorrect
42
     * @return string[]|null[] Returns array where keys are directives and values are arguments
43
     */
44
    public function getDirectives(bool $ignoreIncorrect = true): array
45
    {
46
        $result = [];
47
        /** @var DirectivesHeaderValue $header */
48
        foreach ($this->collection as $header) {
49
            if ($ignoreIncorrect && $header->hasError()) {
50
                continue;
51
            }
52
            $result[] = [$header->getDirective() => $header->getArgument()];
53
        }
54
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result returns an array which contains values of type array which are incompatible with the documented value type null|string.
Loading history...
55
    }
56
}
57