Passed
Push — master ( d49609...ad7aab )
by James Ekow Abaka
01:57 queued 13s
created

Validator   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 20
eloc 27
c 3
b 1
f 0
dl 0
loc 72
ccs 21
cts 28
cp 0.75
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validateCommand() 0 7 3
B validateOption() 0 16 9
B validateArguments() 0 14 8
1
<?php
2
3
namespace clearice\argparser;
4
5
6
class Validator implements ValidatorInterface
7
{
8
    /**
9
     * Supported keys expected in the option array. This is used for validation./
10
     */
11
    const VALID_OPTION_KEYS = [
12
        'name', 'short_name', 'type', 'help', 'repeats',
13
        'default', 'value', 'required', 'command'
14
    ];
15
16
    /**
17
     * @param $options
18
     * @param $parsed
19
     * @throws InvalidArgumentException
20
     */
21 5
    public function validateArguments($options, $parsed)
22
    {
23 5
        $required = [];
24 5
        foreach($options as $option) {
25 5
            if(isset($option['required']) && $option['required'] && !isset($parsed[$option['name']]) && $option['command'] == ($parsed['__command'] ?? '')) {
26
                $required[] = $option['name'];
27
            }
28
        }
29
30 5
        if(!empty(($required))) {
31
            throw new InvalidArgumentException(
32
                sprintf(
33
                    "Values for the following options are required%s: %s.\nPass the --help option for more information about possible options.",
34
                    isset($parsed['__command']) ? " for the {$parsed['__command']} command" : "", implode(",", $required)
35
                )
36
            );
37
        }
38 5
    }
39
40
    /**
41
     * @param $option
42
     * @param $commands
43
     * @throws InvalidArgumentDescriptionException
44
     * @throws UnknownCommandException
45
     */
46 15
    public function validateOption($option, $commands)
47
    {
48 15
        if (!(isset($option['name']) || isset($option['short_name']))) {
49 1
            throw new InvalidArgumentDescriptionException("An option must have either a name, a short_name or both.");
50
        }
51 14
        $name = $option['name'] ?? $option['short_name'];
52 14
        if(isset($option['default']) && isset($option['required'])) {
53
            throw new InvalidArgumentDescriptionException("A required option, {$name} cannot have a default value.");
54
        }
55 14
        foreach($option as $key => $value) {
56 14
            if(!in_array($key, self::VALID_OPTION_KEYS)) {
57
                throw new InvalidArgumentDescriptionException("Invalid key [$key] in option description for [{$name}]");
58
            }
59
        }
60 14
        if (isset($option['command']) && !isset($commands[$option['command']])) {
61 1
            throw new UnknownCommandException("The command '{$option['command']}' has not been added to this parser.");
62
        }
63 14
    }
64
65
    /**
66
     * @param $command
67
     * @param $commands
68
     * @throws CommandExistsException
69
     * @throws InvalidArgumentDescriptionException
70
     */
71 6
    public function validateCommand($command, $commands)
72
    {
73 6
        if (!isset($command['name'])) {
74 1
            throw new InvalidArgumentDescriptionException("Command description must contain a name");
75
        }
76 6
        if (isset($commands[$command['name']])) {
77 1
            throw new CommandExistsException("Command ${command['name']} already exists.");
78
        }
79
    }
80
}