Passed
Branch benchmark (b81757)
by Jose
02:27
created

InputValidator::isOptionalInteger()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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
    private $output;
11
12 24
    public function __construct(StyleInterface $output)
13
    {
14 24
        $this->output = $output;
15 24
    }
16
17 24
    public function validate(InputInterface $input)
18
    {
19 24
        $hasValidInput = true;
20
21 24
        $sizes = $input->getOption(BenchmarkCommand::SIZE_OPTION);
22 24
        $iterations = $input->getOption(BenchmarkCommand::ITERATIONS_OPTION);
23 24
        $seed = $input->getOption(BenchmarkCommand::SEED_OPTION);
24
25 24
        foreach ($sizes as $size) {
0 ignored issues
show
Bug introduced by
The expression $sizes of type string|array<integer,string>|boolean|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
26 24
            if (!$this->isPositiveInteger($size)) {
27 7
                $this->output->error('The size must be an integer greater than 0');
28 24
                $hasValidInput = false;
29
            }
30
        }
31
32 24
        if (!$this->isPositiveInteger($iterations)) {
33 8
            $this->output->error('The number of iterations must be an integer greater than 0');
34 8
            $hasValidInput = false;
35
        }
36
37 24
        if (!$this->isOptionalInteger($seed)) {
38 8
            $this->output->error('The seed must be an integer');
39 8
            $hasValidInput = false;
40
        }
41
42 24
        return $hasValidInput;
43
    }
44
45 24
    private function isPositiveInteger($value)
46
    {
47 24
        $positiveInteger = filter_var($value, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1)));
48
49 24
        return $positiveInteger !== false;
50
    }
51
52 24
    private function isOptionalInteger($value)
53
    {
54 24
        $integer = filter_var($value, FILTER_VALIDATE_INT);
55
56 24
        return $integer !== false || $value === null;
57
    }
58
}
59