Passed
Pull Request — master (#1104)
by Aleksei
26:20
created

AttributedTrait::withAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Interceptors\Context;
6
7
/**
8
 * Provides a basic implementation of the {@see AttributedInterface}.
9
 */
10
trait AttributedTrait
11
{
12
    /** @var array<non-empty-string, mixed> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, mixed> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, mixed>.
Loading history...
13
    private array $attributes = [];
14
15
    /**
16
     * @return array<non-empty-string, mixed> Attributes derived from the context.
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, mixed> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, mixed>.
Loading history...
17
     */
18 4
    public function getAttributes(): array
19
    {
20 4
        return $this->attributes;
21
    }
22
23
    /**
24
     * @param non-empty-string $name
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
25
     */
26 3
    public function getAttribute(string $name, mixed $default = null): mixed
27
    {
28 3
        return $this->attributes[$name] ?? $default;
29
    }
30
31
    /**
32
     * @param non-empty-string $name
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
33
     */
34 6
    public function withAttribute(string $name, mixed $value): static
35
    {
36 6
        $clone = clone $this;
37 6
        $clone->attributes[$name] = $value;
38 6
        return $clone;
39
    }
40
41
    /**
42
     * @param non-empty-string $name
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
43
     */
44 1
    public function withoutAttribute(string $name): static
45
    {
46 1
        $clone = clone $this;
47 1
        unset($clone->attributes[$name]);
48 1
        return $clone;
49
    }
50
}
51