Argument   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 69
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0
wmc 11

10 Methods

Rating   Name   Duplication   Size   Complexity  
A optional() 0 3 1
A __construct() 0 4 1
A hasDefault() 0 3 1
A type() 0 3 1
A default() 0 3 1
A name() 0 3 1
A compile() 0 3 1
A validate() 0 4 2
A defaultsTo() 0 6 1
A makeOptional() 0 6 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose\Definition;
5
6
use Innmind\Compose\{
7
    Definition\Argument\Type,
8
    Exception\InvalidArgument,
9
    Compilation\Argument as CompiledArgument
10
};
11
12
final class Argument
13
{
14
    private $name;
15
    private $type;
16
    private $optional = false;
17
    private $default;
18
19 71
    public function __construct(Name $name, Type $type)
20
    {
21 71
        $this->name = $name;
22 71
        $this->type = $type;
23 71
    }
24
25 69
    public function name(): Name
26
    {
27 69
        return $this->name;
28
    }
29
30 15
    public function type(): Type
31
    {
32 15
        return $this->type;
33
    }
34
35 15
    public function makeOptional(): self
36
    {
37 15
        $self = clone $this;
38 15
        $self->optional = true;
39
40 15
        return $self;
41
    }
42
43 15
    public function defaultsTo(Name $name): self
44
    {
45 15
        $self = clone $this;
46 15
        $self->default = $name;
47
48 15
        return $self;
49
    }
50
51 53
    public function optional(): bool
52
    {
53 53
        return $this->optional;
54
    }
55
56 53
    public function hasDefault(): bool
57
    {
58 53
        return $this->default instanceOf Name;
59
    }
60
61 13
    public function default(): Name
62
    {
63 13
        return $this->default;
64
    }
65
66
    /**
67
     * @param mixed $value
68
     *
69
     * @throws InvalidArgument
70
     */
71 36
    public function validate($value): void
72
    {
73 36
        if (!$this->type->accepts($value)) {
74 2
            throw new InvalidArgument((string) $this->name);
75
        }
76 35
    }
77
78 6
    public function compile(): CompiledArgument
79
    {
80 6
        return new CompiledArgument($this);
81
    }
82
}
83