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

TerrainGenerator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 17.78 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 8
loc 45
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 18 3
A validatePositiveInteger() 0 8 2
A validateOptionalInteger() 8 8 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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