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

Life::printMatrixCli()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 33
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 26
nc 4
nop 1
1
<?php
2
3
namespace MidoriKocak\GameOfLife;
4
5
6
class Life
7
{
8
    private $world;
9
    private $organisms;
10
    private $generations = 0;
11
12
    public function __construct(World $world, Organisms $organisms)
13
    {
14
        $this->world = $world;
15
        $this->organisms = $organisms;
16
    }
17
18
    public function start($verbose = true)
19
    {
20
        if ($this->generations == 0) {
21
            while ($this->generations < $this->world->getIterations()) {
22
                $this->organisms->iterate();
23
                if ($verbose) {
24
                    self::printMatrixCli($this->organisms->getCells());
25
                }
26
                $this->generations++;
27
            }
28
        }
29
    }
30
31
    public function isEnded()
32
    {
33
        return $this->generations == $this->world->getIterations();
34
    }
35
36
    private static function printMatrixCli($matrix)
37
    {
38
        system('clear');
39
        $colors = [
40
            "0;34",
41
            "0;32",
42
            "0;36",
43
            "0;31",
44
            "0;35",
45
            "0;33",
46
            "0;37",
47
            "1;30",
48
            "1;34",
49
            "1;32",
50
            "1;36",
51
            "1;31",
52
            "1;35",
53
            "1;33",
54
            "1;37"
55
        ];
56
57
        for ($i = 0; $i < sizeof($matrix); $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...
58
            $out = implode($matrix[$i]) . "\n";
0 ignored issues
show
Bug introduced by
The call to implode() has too few arguments starting with pieces. ( Ignorable by Annotation )

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

58
            $out = /** @scrutinizer ignore-call */ implode($matrix[$i]) . "\n";

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
59
            for ($k = 0; $k < strlen($out); $k++) {
60
                if ($out[$k] > 0) {
61
                    $colorNumber = $out[$k] % 16;
62
                    echo "\033[" . $colors[$colorNumber] . "m" . $out[$k] . "\033[0m";
63
                } else {
64
                    echo $out[$k];
65
                }
66
            }
67
        }
68
        usleep(100000);
69
    }
70
71
}