Attribute   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 42
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getName() 0 4 1
A getDefault() 0 4 1
A withDefault() 0 6 1
A validate() 0 9 3
1
<?php declare(strict_types=1);
2
3
namespace Midnight\Block\Type\Attribute;
4
5
class Attribute implements AttributeInterface
6
{
7
    /** @var string */
8
    private $name;
9
    /** @var string|null */
10
    private $default;
11
12
    public function __construct(string $name)
13
    {
14
        $this->name = $name;
15
    }
16
17
    public function getName(): string
18
    {
19
        return $this->name;
20
    }
21
22
    public function getDefault(): ?string
23
    {
24
        return $this->default;
25
    }
26
27
    public function withDefault(string $value): self
28
    {
29
        $clone = clone $this;
30
        $clone->name = $value;
31
        return $clone;
32
    }
33
34
    /**
35
     * @throws \Midnight\Block\Type\Attribute\Exception\InvalidAttributeValueException
36
     */
37
    public function validate(?string $value): void
38
    {
39
        if ($this->default === null) {
40
            return;
41
        }
42
        if ($value === null) {
43
            throw new Exception\InvalidAttributeValueException(sprintf('Attribute %s is not optional', $this->name));
44
        }
45
    }
46
}
47