SurrogateKeys::setSurrogateHeader()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\QueryRepository;
6
7
use BEAR\Resource\AbstractUri;
8
use BEAR\Resource\ResourceObject;
9
10
use function array_key_exists;
11
use function array_merge;
12
use function array_unique;
13
use function explode;
14
use function implode;
15
16
final class SurrogateKeys
17
{
18
    /** @var list<string> */
0 ignored issues
show
Bug introduced by
The type BEAR\QueryRepository\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
    private array $surrogateKeys;
20
    private readonly UriTagInterface $uriTag;
21
22
    public function __construct(AbstractUri $uri)
23
    {
24
        $this->uriTag = new UriTag();
0 ignored issues
show
Bug introduced by
The property uriTag is declared read-only in BEAR\QueryRepository\SurrogateKeys.
Loading history...
25
        $this->surrogateKeys = [($this->uriTag)($uri)];
0 ignored issues
show
Documentation Bug introduced by
It seems like array($this->uriTag($uri)) of type array is incompatible with the declared type BEAR\QueryRepository\list of property $surrogateKeys.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
26
    }
27
28
    /**
29
     * Add etag of embedded resource
30
     */
31
    public function addTag(ResourceObject $ro): void
32
    {
33
        $this->surrogateKeys[] = ($this->uriTag)($ro->uri);
34
        if (array_key_exists(Header::SURROGATE_KEY, $ro->headers)) {
35
            $this->surrogateKeys = array_merge($this->surrogateKeys, explode(' ', $ro->headers[Header::SURROGATE_KEY]));
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->surro...eader::SURROGATE_KEY])) of type array is incompatible with the declared type BEAR\QueryRepository\list of property $surrogateKeys.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
36
        }
37
    }
38
39
    public function setSurrogateHeader(ResourceObject $ro): void
40
    {
41
        $key = implode(' ', array_unique($this->surrogateKeys));
42
        $wasSetManually = isset($ro->headers[Header::SURROGATE_KEY]);
43
        if ($wasSetManually) {
44
            $ro->headers[Header::SURROGATE_KEY] .= ' ' . $key;
45
46
            return;
47
        }
48
49
        $ro->headers[Header::SURROGATE_KEY] = $key;
50
    }
51
}
52