Test Failed
Push — master ( 0d2523...299604 )
by Midori
01:39
created

Organisms::getNeighborIndexes()   C

Complexity

Conditions 13
Paths 256

Size

Total Lines 15
Code Lines 10

Duplication

Lines 3
Ratio 20 %

Importance

Changes 0
Metric Value
dl 3
loc 15
rs 5.6601
c 0
b 0
f 0
cc 13
eloc 10
nc 256
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace MidoriKocak\GameOfLife;
4
5
6
class Organisms
7
{
8
    /**
9
     * @var int[][]
10
     */
11
    private $cells;
12
13
    /**
14
     * Organisms constructor.
15
     * @param int[][] $cells
16
     */
17
    public function __construct(array $cells)
18
    {
19
        $this->cells = $cells;
20
    }
21
22
    public function iterate()
23
    {
24
        $next = $this->cells;
25
26
        for ($i = 0; $i < sizeof($this->cells); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
27
            for ($j = 0; $j < sizeof($this->cells[0]); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
28
                $next[$i][$j] = $this->checkCell($i, $j);
29
            }
30
        }
31
32
        $this->cells = $next;
33
    }
34
35
    public function getCells()
36
    {
37
        return $this->cells;
38
    }
39
40
    private function getNeighborCount($i, $j)
41
    {
42
        $neighbors = [];
43
        $neighbors[$this->cells[$i][$j]] = 0;
44
        $indexes = self::getNeighborIndexes($this->cells, $i, $j);
45
        foreach ($indexes as $coordinate) {
46
            $y = $coordinate[0];
47
            $x = $coordinate[1];
48
            $type = $this->cells[$y][$x];
49
50
            if ($this->cells[$y][$x] > 0) {
51
                $neighbors[$type] = $neighbors[$type] ?? 0;
52
                $neighbors[$type]++;
53
            }
54
        }
55
        return $neighbors;
56
    }
57
58
    private function checkCell($i, $j)
59
    {
60
        $neighbors = $this->getNeighborCount($i, $j);
61
        if ($this->cells[$i][$j] > 0) {
62
            if ($neighbors[$this->cells[$i][$j]] == 2 || $neighbors[$this->cells[$i][$j]] == 3) {
63
                return $this->cells[$i][$j];
64
            } elseif ($neighbors[$this->cells[$i][$j]] < 2) {
65
                return 0;
66
            } elseif ($neighbors[$this->cells[$i][$j]] >= 4) {
67
                return 0;
68
            }
69
        } else {
70
            $speciesEqualToTree = [];
71
            foreach ($neighbors as $type => $count) {
72
                if ($count == 3) {
73
                    array_push($speciesEqualToTree, $type);
74
                }
75
            }
76
77
            if (!empty($speciesEqualToTree)) {
78
                return $speciesEqualToTree[rand(0, sizeof($speciesEqualToTree) - 1)];
79
            } else {
80
                return 0;
81
            }
82
        }
83
        return 0;
84
    }
85
86
    private static function getNeighborIndexes($matrix, $i, $j)
87
    {
88
89
        $indexes = [];
90
91
        if ($i > 0 && $j > 0) array_push($indexes, [$i - 1, $j - 1]);
92
        if ($i > 0) array_push($indexes, [$i - 1, $j]);
93 View Code Duplication
        if ($i > 0 && $j < sizeof($matrix[0]) - 1) array_push($indexes, [$i - 1, $j + 1]);
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
94
        if ($j > 0) array_push($indexes, [$i, $j - 1]);
95
        if ($j < sizeof($matrix[0]) - 1) array_push($indexes, [$i, $j + 1]);
96 View Code Duplication
        if ($i < sizeof($matrix) - 1 && $j > 0) array_push($indexes, [$i + 1, $j - 1]);
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
97
        if ($i < sizeof($matrix) - 1) array_push($indexes, [$i + 1, $j]);
98 View Code Duplication
        if ($i < sizeof($matrix) - 1 && $j < sizeof($matrix[0]) - 1) array_push($indexes, [$i + 1, $j + 1]);
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
99
100
        return $indexes;
101
    }
102
103
104
}