Passed
Push — master ( 2fbd23...3c6f7d )
by Aleksei
01:50
created

DataBucket::getHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace roxblnfk\SmartStream\Data;
4
5
use Yiisoft\Http\Status;
6
7
class DataBucket
8
{
9
    /** @var mixed */
10
    protected $data;
11
    protected ?int $code = null;
12
    /** @var string[] */
13
    protected array $headers = [];
14
    protected ?string $format = null;
15
    protected array $params = [];
16
17
    protected const IS_CONVERTABLE = true;
18
19
    public function __construct($data, string $format = null, array $params = [])
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

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

19
    public function __construct($data, string $format = null, /** @scrutinizer ignore-unused */ array $params = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
20
    {
21
        $this->data = $data;
22
        $this->format = $format;
23
    }
24
    public function getCode(): ?int
25
    {
26
        return $this->code;
27
    }
28
    public function getHeaders(): array
29
    {
30
        return $this->headers;
31
    }
32
    public function getFormat(): ?string
33
    {
34
        return $this->format;
35
    }
36
    public function getParams(): array
37
    {
38
        return $this->params;
39
    }
40
    public function getData()
41
    {
42
        return $this->data;
43
    }
44
    public function isConvertable(): bool
45
    {
46
        return static::IS_CONVERTABLE;
47
    }
48
    public function hasFormat(): bool
49
    {
50
        return static::IS_CONVERTABLE && $this->format !== null;
51
    }
52
53
    public function withStatus(?int $code = Status::OK): self
54
    {
55
        $clone = clone $this;
56
        $clone->setStatus($code);
57
        return $clone;
58
    }
59
    public function withHeader(string $name, string $value): self
60
    {
61
        $clone = clone $this;
62
        $clone->setHeader($name, $value);
63
        return $clone;
64
    }
65
    public function withFormat(?string $format, array $params = null): self
66
    {
67
        $clone = clone $this;
68
        $clone->setFormat($format, $params);
0 ignored issues
show
Bug introduced by
It seems like $params can also be of type null; however, parameter $params of roxblnfk\SmartStream\Data\DataBucket::setFormat() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

68
        $clone->setFormat($format, /** @scrutinizer ignore-type */ $params);
Loading history...
69
        return $clone;
70
    }
71
    public function withoutHeader(string $name): self
72
    {
73
        $clone = clone $this;
74
        unset($clone->headers[$name]);
75
        return $clone;
76
    }
77
78
79
    protected function setHeader(string $name, string $value): void
80
    {
81
        $this->headers[$name] = $value;
82
    }
83
    protected function setFormat(?string $format, array $params): void
84
    {
85
        $this->format = $format;
86
        if ($params !== null) {
0 ignored issues
show
introduced by
The condition $params !== null is always true.
Loading history...
87
            $this->params = $params;
88
        }
89
    }
90
    protected function setStatus(?int $code): void
91
    {
92
        $this->code = $code;
93
    }
94
}
95