Completed
Push — dev ( 27ea21...40d27c )
by James Ekow Abaka
01:33
created

Validator::validateOption()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 4
nc 3
nop 2
crap 4
1
<?php
2
3
namespace clearice\argparser;
4
5
6
class Validator
7
{
8
    /**
9
     * @param $options
10
     * @throws InvalidArgumentException
11
     */
12 5
    public function validateArguments($options)
13
    {
14 5
        $required = [];
15 5
        foreach($options as $option) {
16 5
            if(isset($option['required']) && $option['required'] && !isset($parsed[$option['name']])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $parsed seems to never exist and therefore isset should always be false.
Loading history...
17 5
                $required[] = $option['name'];
18
            }
19
        }
20
21 5
        if(!empty(($required))) {
22
            throw new InvalidArgumentException(
23
                sprintf("The following options are required: %s. Pass the --help option for more information about possible options.", implode(", $required"))
0 ignored issues
show
Bug introduced by
', '.$required of type string is incompatible with the type array expected by parameter $pieces of implode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

23
                sprintf("The following options are required: %s. Pass the --help option for more information about possible options.", implode(/** @scrutinizer ignore-type */ ", $required"))
Loading history...
24
            );
25
        }
26 5
    }
27
28
    /**
29
     * @throws InvalidArgumentDescriptionException
30
     * @throws UnknownCommandException
31
     */
32 14
    public function validateOption($option, $commands)
33
    {
34 14
        if (!isset($option['name'])) {
35 1
            throw new InvalidArgumentDescriptionException("Argument must have a name");
36
        }
37 13
        if (isset($option['command']) && !isset($commands[$option['command']])) {
38 1
            throw new UnknownCommandException("The command {$option['command']} is unknown");
39
        }
40 13
    }
41
42
    /**
43
     * @param $command
44
     * @param $commands
45
     * @throws CommandExistsException
46
     * @throws InvalidArgumentDescriptionException
47
     */
48 5
    public function validateCommand($command, $commands)
49
    {
50 5
        if (!isset($command['name'])) {
51 1
            throw new InvalidArgumentDescriptionException("Command description must contain a name");
52
        }
53 5
        if (isset($commands[$command['name']])) {
54 1
            throw new CommandExistsException("Command ${command['name']} already exists.");
55
        }
56
    }
57
}