BaseConfig   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 118
rs 10
c 0
b 0
f 0
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A hasInterfaceParsing() 0 3 1
A getTemplatesPath() 0 3 1
A hasPrivateParsing() 0 3 1
A hasAuto() 0 3 1
A getPhpDoc() 0 3 1
A validate() 0 19 5
A __construct() 0 5 1
A validatePhpdoc() 0 11 3
1
<?php
2
3
/**
4
 * This file is part of PhpUnitGen.
5
 *
6
 * (c) 2017-2018 Paul Thébaud <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace PhpUnitGen\Configuration;
13
14
use PhpUnitGen\Configuration\ConfigurationInterface\ConfigInterface;
15
use PhpUnitGen\Exception\InvalidConfigException;
16
use Respect\Validation\Validator;
17
18
/**
19
 * Class BaseConfig.
20
 *
21
 * @author     Paul Thébaud <[email protected]>.
22
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
23
 * @license    https://opensource.org/licenses/MIT The MIT license.
24
 * @link       https://github.com/paul-thebaud/phpunit-generator
25
 * @since      Class available since Release 2.0.0.
26
 */
27
class BaseConfig implements ConfigInterface
28
{
29
    /**
30
     * @var mixed[] DEFAULT_CONFIG The default base configuration.
31
     */
32
    protected const DEFAULT_CONFIG = [
33
        'interface' => false,
34
        'private'   => true,
35
        'auto'      => false,
36
        'phpdoc'    => []
37
    ];
38
39
    /**
40
     * @var array $config The configuration as an array.
41
     */
42
    protected $config;
43
44
    /**
45
     * ArrayConfig constructor.
46
     *
47
     * @param mixed $config The config array to use.
48
     *
49
     * @throws InvalidConfigException If the $config is invalid.
50
     */
51
    public function __construct($config = BaseConfig::DEFAULT_CONFIG)
52
    {
53
        $this->config = $config;
54
55
        $this->validate($config);
56
    }
57
58
    /**
59
     * Validate a configuration to know if it can be used to construct an instance.
60
     *
61
     * @param mixed $config The configuration to validate.
62
     *
63
     * @throws InvalidConfigException If the $config is invalid.
64
     */
65
    protected function validate($config): void
66
    {
67
        // Check that $config is an array
68
        if (! Validator::arrayType()->validate($config)) {
69
            throw new InvalidConfigException('The config must be an array.');
70
        }
71
72
        // Check boolean parameters
73
        if (! Validator::key('interface', Validator::boolType())->validate($config)) {
74
            throw new InvalidConfigException('"interface" parameter must be set as a boolean.');
75
        }
76
        if (! Validator::key('private', Validator::boolType())->validate($config)) {
77
            throw new InvalidConfigException('"private" parameter must be set as a boolean.');
78
        }
79
        if (! Validator::key('auto', Validator::boolType())->validate($config)) {
80
            throw new InvalidConfigException('"auto" parameter must be set as a boolean.');
81
        }
82
83
        $this->validatePhpdoc($config);
84
    }
85
86
    /**
87
     * Validate the phpdoc key in the config array.
88
     *
89
     * @param mixed $config The configuration.
90
     *
91
     * @throws InvalidConfigException If the phpdoc is invalid (source or target).
92
     */
93
    private function validatePhpdoc($config): void
94
    {
95
        // Check that dirs key exists
96
        if (! Validator::key('phpdoc', Validator::arrayType())->validate($config)) {
97
            throw new InvalidConfigException('"phpdoc" parameter is not an array.');
98
        }
99
        // Validate each phpdoc
100
        if (! Validator::arrayVal()
101
            ->each(Validator::stringType(), Validator::stringType())->validate($config['phpdoc'])
102
        ) {
103
            throw new InvalidConfigException('Some annotation in "phpdoc" parameter are not strings.');
104
        }
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function hasInterfaceParsing(): bool
111
    {
112
        return $this->config['interface'];
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function hasPrivateParsing(): bool
119
    {
120
        return $this->config['private'];
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function hasAuto(): bool
127
    {
128
        return $this->config['auto'];
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function getTemplatesPath(): string
135
    {
136
        return realpath(__DIR__ . '/../../template');
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function getPhpDoc(): array
143
    {
144
        return $this->config['phpdoc'];
145
    }
146
}
147