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

AbstractParameterSpec   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 71.43%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 76
ccs 15
cts 21
cp 0.7143
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A getName() 0 4 1
A getExtractorDefinition() 0 4 1
A isOptional() 0 4 1
A getDefaultValue() 0 12 3
A isVariadic() 0 4 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