InputValidator   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 20
c 1
b 0
f 0
dl 0
loc 46
ccs 24
cts 24
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isOptionalInteger() 0 5 2
A isPositiveInteger() 0 5 1
A validate() 0 26 5
A __construct() 0 2 1
1
<?php
2
3
namespace JMGQ\AStar\Benchmark;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Style\StyleInterface;
7
8
class InputValidator
9
{
10 24
    public function __construct(private StyleInterface $output)
11
    {
12 24
    }
13
14 24
    public function validate(InputInterface $input): bool
15
    {
16 24
        $hasValidInput = true;
17
18 24
        $sizes = (array) $input->getOption(BenchmarkCommand::SIZE_OPTION);
19 24
        $iterations = $input->getOption(BenchmarkCommand::ITERATIONS_OPTION);
20 24
        $seed = $input->getOption(BenchmarkCommand::SEED_OPTION);
21
22 24
        foreach ($sizes as $size) {
23 24
            if (!$this->isPositiveInteger($size)) {
24 7
                $this->output->error('The size must be an integer greater than 0');
25 7
                $hasValidInput = false;
26
            }
27
        }
28
29 24
        if (!$this->isPositiveInteger($iterations)) {
30 8
            $this->output->error('The number of iterations must be an integer greater than 0');
31 8
            $hasValidInput = false;
32
        }
33
34 24
        if (!$this->isOptionalInteger($seed)) {
35 8
            $this->output->error('The seed must be an integer');
36 8
            $hasValidInput = false;
37
        }
38
39 24
        return $hasValidInput;
40
    }
41
42 24
    private function isPositiveInteger(mixed $value): bool
43
    {
44 24
        $positiveInteger = filter_var($value, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
45
46 24
        return $positiveInteger !== false;
47
    }
48
49 24
    private function isOptionalInteger(mixed $value): bool
50
    {
51 24
        $integer = filter_var($value, FILTER_VALIDATE_INT);
52
53 24
        return $integer !== false || $value === null;
54
    }
55
}
56