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

TerrainGenerator::validatePositiveInteger()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
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 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);
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));
40
        }
41 16
    }
42
43 10 View Code Duplication
    private function validateOptionalInteger($number)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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));
49
        }
50 3
    }
51
}
52