Passed
Push — master ( d2ebdc...958928 )
by Bogdan
05:05
created

AbstractParameterSpec::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 5
crap 1
1
<?php declare(strict_types=1);
2
3
/*
4
 * This file is part of the pinepain/js-sandbox PHP library.
5
 *
6
 * Copyright (c) 2016-2017 Bogdan Padalko <[email protected]>
7
 *
8
 * Licensed under the MIT license: http://opensource.org/licenses/MIT
9
 *
10
 * For the full copyright and license information, please view the
11
 * LICENSE file that was distributed with this source or visit
12
 * http://opensource.org/licenses/MIT
13
 */
14
15
16
namespace Pinepain\JsSandbox\Specs\Parameters;
17
18
19
use Pinepain\JsSandbox\Extractors\Definition\ExtractorDefinitionInterface;
20
use Pinepain\JsSandbox\Specs\SpecException;
21
22
23
abstract class AbstractParameterSpec implements ParameterSpecInterface
24
{
25
    /**
26
     * @var string
27
     */
28
    private $name;
29
    /**
30
     * @var ExtractorDefinitionInterface
31
     */
32
    private $extractor;
33
    /**
34
     * @var bool
35
     */
36
    private $variadic;
37
    /**
38
     * @var bool
39
     */
40
    private $optional;
41
    /**
42
     * @var mixed
43
     */
44
    private $default;
45
46
    /**
47
     * @param string                       $name
48
     * @param ExtractorDefinitionInterface $extractor
49
     * @param bool                         $variadic
50
     * @param bool                         $optional
51
     * @param mixed                        $default
52
     */
53 33
    public function __construct(string $name, ExtractorDefinitionInterface $extractor, bool $variadic = false, bool $optional = false, $default = null)
54
    {
55 33
        $this->name      = $name;
56 33
        $this->extractor = $extractor;
57
58
        // TODO: should we check arguments to not conflict, like variadic && !optional && !default?
59
60 33
        $this->variadic = $variadic;
61
62 33
        $this->optional = $optional;
63 33
        $this->default  = $default;
64 33
    }
65
66 33
    public function getName(): string
67
    {
68 33
        return $this->name;
69
    }
70
71 33
    public function getExtractorDefinition(): ExtractorDefinitionInterface
72
    {
73 33
        return $this->extractor;
74
    }
75
76
    public function isOptional(): bool
77
    {
78
        return $this->optional;
79
    }
80
81 29
    public function getDefaultValue()
82
    {
83 29
        if ($this->variadic) {
84
            throw new SpecException('Variadic parameter cannot have a default value');
85
        }
86
87 29
        if (!$this->optional) {
88
            throw new SpecException('Mandatory parameter cannot have a default value');
89
        }
90
91 29
        return $this->default;
92
    }
93
94
    public function isVariadic(): bool
95
    {
96
        return $this->variadic;
97
    }
98
}
99