Completed
Push — develop ( 523088...d9cee5 )
by Baptiste
01:42
created

Argument::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
crap 2
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
};
10
11
final class Argument
12
{
13
    private $name;
14
    private $type;
15
    private $optional = false;
16
    private $default;
17
18 10
    public function __construct(Name $name, Type $type)
19
    {
20 10
        $this->name = $name;
21 10
        $this->type = $type;
22 10
    }
23
24 9
    public function name(): Name
25
    {
26 9
        return $this->name;
27
    }
28
29 3
    public function makeOptional(): self
30
    {
31 3
        $self = clone $this;
32 3
        $self->optional = true;
33
34 3
        return $self;
35
    }
36
37 3
    public function defaultsTo(Name $name): self
38
    {
39 3
        $self = clone $this;
40 3
        $self->default = $name;
41
42 3
        return $self;
43
    }
44
45 10
    public function optional(): bool
46
    {
47 10
        return $this->optional;
48
    }
49
50 8
    public function hasDefault(): bool
51
    {
52 8
        return $this->default instanceOf Name;
53
    }
54
55 2
    public function default(): Name
1 ignored issue
show
Coding Style introduced by
Possible parse error: non-abstract method defined as abstract
Loading history...
56
    {
57 2
        return $this->default;
1 ignored issue
show
Coding Style introduced by
The visibility should be declared for property $this.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
58
    }
59
60
    /**
61
     * @param mixed $value
62
     *
63
     * @throws InvalidArgument
64
     */
65 6
    public function validate($value): void
66
    {
67 6
        if (!$this->type->accepts($value)) {
68 2
            throw new InvalidArgument((string) $this->name);
69
        }
70 5
    }
71
}
72