Passed
Push — develop ( 31e284...390d3f )
by Paul
02:12
created

ConsoleConfig::validateStrings()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 1
1
<?php
2
3
namespace PhpUnitGen\Configuration;
4
5
use PhpUnitGen\Exception\InvalidConfigException;
6
use Respect\Validation\Validator;
7
8
/**
9
 * Class ConsoleConfig.
10
 *
11
 * @author     Paul Thébaud <[email protected]>.
12
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
13
 * @license    https://opensource.org/licenses/MIT The MIT license.
14
 * @link       https://github.com/paul-thebaud/phpunit-generator
15
 * @since      Class available since Release 2.0.0.
16
 */
17
class ConsoleConfig extends BaseConfig implements ConsoleConfigInterface
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    protected function validate($config): void
23
    {
24
        parent::validate($config);
25
26
        $this->validateBooleans($config);
27
        $this->validateStrings($config);
28
        $this->validateDirs($config);
29
    }
30
31
    /**
32
     * Validate all boolean attributes contained in configuration.
33
     *
34
     * @param mixed $config The configuration.
35
     *
36
     * @throws InvalidConfigException If a boolean attribute is invalid.
37
     */
38
    private function validateBooleans($config):void
39
    {
40
        // Check boolean parameters
41
        if (! Validator::key('overwrite', Validator::boolType())->validate($config)) {
42
            throw new InvalidConfigException('"overwrite" parameter must be set as a boolean.');
43
        }
44
        if (! Validator::key('auto', Validator::boolType())->validate($config)) {
45
            throw new InvalidConfigException('"auto" parameter must be set as a boolean.');
46
        }
47
        if (! Validator::key('ignore', Validator::boolType())->validate($config)) {
48
            throw new InvalidConfigException('"ignore" parameter must be set as a boolean.');
49
        }
50
    }
51
52
    /**
53
     * Validate all string attributes contained in configuration.
54
     *
55
     * @param mixed $config The configuration.
56
     *
57
     * @throws InvalidConfigException If a string attribute is invalid.
58
     */
59
    private function validateStrings($config): void
60
    {
61
        // Check string parameters
62
        if (! Validator::key('include', Validator::stringType())->validate($config)) {
63
            throw new InvalidConfigException('"include" parameter must be set as a string.');
64
        }
65
        if (! Validator::key('exclude', Validator::stringType())->validate($config)) {
66
            throw new InvalidConfigException('"exclude" parameter must be set as a string.');
67
        }
68
    }
69
70
    /**
71
     * Validate directories contained in configuration.
72
     *
73
     * @param mixed $config The configuration.
74
     *
75
     * @throws InvalidConfigException If a directory is invalid (source or target).
76
     */
77
    private function validateDirs($config): void
78
    {
79
        // Check that dirs exists
80
        if (! Validator::key('dirs', Validator::arrayType()->length(1, null))->validate($config)) {
81
            throw new InvalidConfigException('"dirs" parameter is not an array or does not contains elements.');
82
        }
83
        // Validate each dirs
84
        if (! Validator::arrayVal()
85
            ->each(Validator::stringType(), Validator::stringType())->validate($config['dirs'])
86
        ) {
87
            throw new InvalidConfigException('Some directories in "dirs" parameter are not strings.');
88
        }
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function hasOverwrite(): bool
95
    {
96
        return $this->config['overwrite'];
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function hasIgnore(): bool
103
    {
104
        return $this->config['ignore'];
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function getIncludeRegex(): string
111
    {
112
        return $this->config['include'];
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function getExcludeRegex(): string
119
    {
120
        return $this->config['exclude'];
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function getDirectories(): array
127
    {
128
        return $this->config['dirs'];
129
    }
130
}
131