Completed
Push — develop ( 34db25...1b1cc6 )
by Paul
02:36
created

ConsoleConfig::getIncludeRegex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace PhpUnitGen\Configuration;
4
5
use PhpUnitGen\Configuration\ConfigurationInterface\ConsoleConfigInterface;
6
use PhpUnitGen\Exception\InvalidConfigException;
7
use Respect\Validation\Validator;
8
9
/**
10
 * Class ConsoleConfig.
11
 *
12
 * @author     Paul Thébaud <[email protected]>.
13
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
14
 * @license    https://opensource.org/licenses/MIT The MIT license.
15
 * @link       https://github.com/paul-thebaud/phpunit-generator
16
 * @since      Class available since Release 2.0.0.
17
 */
18
class ConsoleConfig extends BaseConfig implements ConsoleConfigInterface
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    protected function validate($config): void
24
    {
25
        parent::validate($config);
26
27
        $this->validateBooleans($config);
28
        $this->validateIncludeRegex($config);
29
        $this->validateExcludeRegex($config);
30
        $this->validateDirs($config);
31
        $this->validateFiles($config);
32
    }
33
34
    /**
35
     * Validate all boolean attributes contained in configuration.
36
     *
37
     * @param mixed $config The configuration.
38
     *
39
     * @throws InvalidConfigException If a boolean attribute is invalid.
40
     */
41
    private function validateBooleans($config): void
42
    {
43
        // Check boolean parameters
44
        if (! Validator::key('overwrite', Validator::boolType())->validate($config)) {
45
            throw new InvalidConfigException('"overwrite" parameter must be set as a boolean.');
46
        }
47
        if (! Validator::key('backup', Validator::boolType())->validate($config)) {
48
            throw new InvalidConfigException('"backup" parameter must be set as a boolean.');
49
        }
50
        if (! Validator::key('ignore', Validator::boolType())->validate($config)) {
51
            throw new InvalidConfigException('"ignore" parameter must be set as a boolean.');
52
        }
53
    }
54
55
    /**
56
     * Validate the include regex.
57
     *
58
     * @param mixed $config The configuration.
59
     *
60
     * @throws InvalidConfigException If a string attribute is invalid.
61
     */
62
    private function validateIncludeRegex($config): void
63
    {
64
        if (! Validator::key('include', Validator::stringType())->validate($config)
65
            && ! Validator::key('include', Validator::nullType())->validate($config)
66
        ) {
67
            throw new InvalidConfigException('"include" parameter must be set as a string or a null value.');
68
        }
69
    }
70
71
    /**
72
     * Validate the exclude regex.
73
     *
74
     * @param mixed $config The configuration.
75
     *
76
     * @throws InvalidConfigException If a string attribute is invalid.
77
     */
78
    private function validateExcludeRegex($config): void
79
    {
80
        if (! Validator::key('exclude', Validator::stringType())->validate($config)
81
            && ! Validator::key('exclude', Validator::nullType())->validate($config)
82
        ) {
83
            throw new InvalidConfigException('"exclude" parameter must be set as a string or a null value.');
84
        }
85
    }
86
87
    /**
88
     * Validate directories contained in configuration.
89
     *
90
     * @param mixed $config The configuration.
91
     *
92
     * @throws InvalidConfigException If a directory is invalid (source or target).
93
     */
94
    private function validateDirs($config): void
95
    {
96
        // Check that dirs key exists
97
        if (! Validator::key('dirs', Validator::arrayType())->validate($config)) {
98
            throw new InvalidConfigException('"dirs" parameter is not an array.');
99
        }
100
        // Validate each dirs
101
        if (! Validator::arrayVal()
102
            ->each(Validator::stringType(), Validator::stringType())->validate($config['dirs'])
103
        ) {
104
            throw new InvalidConfigException('Some directories in "dirs" parameter are not strings.');
105
        }
106
    }
107
108
    /**
109
     * Validate files contained in configuration.
110
     *
111
     * @param mixed $config The configuration.
112
     *
113
     * @throws InvalidConfigException If a file is invalid (source or target).
114
     */
115
    private function validateFiles($config): void
116
    {
117
        // Check that files key exists
118
        if (! Validator::key('files', Validator::arrayType())->validate($config)) {
119
            throw new InvalidConfigException('"files" parameter is not an array.');
120
        }
121
        // Validate each files
122
        if (! Validator::arrayVal()
123
            ->each(Validator::stringType(), Validator::stringType())->validate($config['files'])
124
        ) {
125
            throw new InvalidConfigException('Some files in "files" parameter are not strings.');
126
        }
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function hasOverwrite(): bool
133
    {
134
        return $this->config['overwrite'];
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function hasBackup(): bool
141
    {
142
        return $this->config['backup'];
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function hasIgnore(): bool
149
    {
150
        return $this->config['ignore'];
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function getIncludeRegex(): ?string
157
    {
158
        return $this->config['include'];
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164
    public function getExcludeRegex(): ?string
165
    {
166
        return $this->config['exclude'];
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    public function getDirectories(): array
173
    {
174
        return $this->config['dirs'];
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function getFiles(): array
181
    {
182
        return $this->config['files'];
183
    }
184
}
185