Passed
Pull Request — master (#14)
by Jose
04:13
created

AggregatedResult::filterNaturalNumber()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace JMGQ\AStar\Benchmark\Result;
4
5
class AggregatedResult
6
{
7
    private $size;
8
    private $averageDuration;
9
    private $minimumDuration;
10
    private $maximumDuration;
11
    private $numberOfSolutions;
12
    private $numberOfTerrains;
13
14
    /**
15
     * @param int $size
16
     * @param int $averageDuration
17
     * @param int $minimumDuration
18
     * @param int $maximumDuration
19
     * @param int $numberOfSolutions
20
     * @param int $numberOfTerrains
21
     */
22 46
    public function __construct(
23
        $size,
24
        $averageDuration,
25
        $minimumDuration,
26
        $maximumDuration,
27
        $numberOfSolutions,
28
        $numberOfTerrains
29
    ) {
30 46
        $this->size = $this->filterNaturalNumber($size, 'size');
31 40
        $this->averageDuration = $this->filterNonNegativeInteger($averageDuration, 'average duration');
32 33
        $this->minimumDuration = $this->filterNonNegativeInteger($minimumDuration, 'minimum duration');
33 26
        $this->maximumDuration = $this->filterNonNegativeInteger($maximumDuration, 'maximum duration');
34 19
        $this->numberOfSolutions = $this->filterNonNegativeInteger($numberOfSolutions, 'number of solutions');
35 12
        $this->numberOfTerrains = $this->filterNaturalNumber($numberOfTerrains, 'number of terrains');
36 6
    }
37
38 3
    public function getSize()
39
    {
40 3
        return $this->size;
41
    }
42
43 3
    public function getAverageDuration()
44
    {
45 3
        return $this->averageDuration;
46
    }
47
48 3
    public function getMinimumDuration()
49
    {
50 3
        return $this->minimumDuration;
51
    }
52
53 3
    public function getMaximumDuration()
54
    {
55 3
        return $this->maximumDuration;
56
    }
57
58 3
    public function getNumberOfSolutions()
59
    {
60 3
        return $this->numberOfSolutions;
61
    }
62
63 3
    public function getNumberOfTerrains()
64
    {
65 3
        return $this->numberOfTerrains;
66
    }
67
68 46
    private function filterNaturalNumber($value, $parameterName)
69
    {
70 46
        $naturalNumber = filter_var($value, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1)));
71
72 46
        if ($naturalNumber === false) {
73 12
            throw new \InvalidArgumentException('Invalid ' . $parameterName . ': ' . print_r($value, true));
0 ignored issues
show
Bug introduced by
Are you sure print_r($value, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

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

73
            throw new \InvalidArgumentException('Invalid ' . $parameterName . ': ' . /** @scrutinizer ignore-type */ print_r($value, true));
Loading history...
74
        }
75
76 40
        return $naturalNumber;
77
    }
78
79 40
    private function filterNonNegativeInteger($value, $parameterName)
80
    {
81 40
        $nonNegativeInteger = filter_var($value, FILTER_VALIDATE_INT, array('options' => array('min_range' => 0)));
82
83 40
        if ($nonNegativeInteger === false) {
84 28
            throw new \InvalidArgumentException('Invalid ' . $parameterName . ': ' . print_r($value, true));
0 ignored issues
show
Bug introduced by
Are you sure print_r($value, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

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

84
            throw new \InvalidArgumentException('Invalid ' . $parameterName . ': ' . /** @scrutinizer ignore-type */ print_r($value, true));
Loading history...
85
        }
86
87 33
        return $nonNegativeInteger;
88
    }
89
}
90