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

TerrainGenerator::validatePositiveInteger()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace JMGQ\AStar\Benchmark;
4
5
use JMGQ\AStar\Example\Terrain\TerrainCost;
6
7
class TerrainGenerator
8
{
9
    /**
10
     * @param int $rows
11
     * @param int $columns
12
     * @param int | null $seed
13
     * @return TerrainCost
14
     */
15 22
    public function generate($rows, $columns, $seed = null)
16
    {
17 22
        $this->validatePositiveInteger($rows);
18 16
        $this->validatePositiveInteger($columns);
19 10
        $this->validateOptionalInteger($seed);
20
21 3
        mt_srand($seed);
0 ignored issues
show
Bug introduced by
It seems like $seed can also be of type null; however, parameter $seed of mt_srand() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

21
        mt_srand(/** @scrutinizer ignore-type */ $seed);
Loading history...
22
23 3
        $terrainCost = array();
24
25 3
        foreach (range(0, $rows - 1) as $row) {
26 3
            foreach (range(0, $columns - 1) as $column) {
27 3
                $terrainCost[$row][$column] = mt_rand(1, 10);
28
            }
29
        }
30
31 3
        return new TerrainCost($terrainCost);
32
    }
33
34 22
    private function validatePositiveInteger($number)
35
    {
36 22
        $positiveInteger = filter_var($number, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1)));
37
38 22
        if ($positiveInteger === false) {
39 12
            throw new \InvalidArgumentException('Invalid positive integer: ' . print_r($number, true));
0 ignored issues
show
Bug introduced by
Are you sure print_r($number, 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

39
            throw new \InvalidArgumentException('Invalid positive integer: ' . /** @scrutinizer ignore-type */ print_r($number, true));
Loading history...
40
        }
41 16
    }
42
43 10
    private function validateOptionalInteger($number)
44
    {
45 10
        $integer = filter_var($number, FILTER_VALIDATE_INT);
46
47 10
        if ($integer === false && $number !== null) {
48 7
            throw new \InvalidArgumentException('Invalid integer: ' . print_r($number, true));
0 ignored issues
show
Bug introduced by
Are you sure print_r($number, 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

48
            throw new \InvalidArgumentException('Invalid integer: ' . /** @scrutinizer ignore-type */ print_r($number, true));
Loading history...
49
        }
50 3
    }
51
}
52