GeneratorHelper::isValidSudoku()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 17
cts 17
cp 1
rs 8.8657
c 0
b 0
f 0
cc 6
nc 6
nop 1
crap 6
1
<?php
2
3
namespace Chipulaja\Algo\Sudoku;
4
5
class GeneratorHelper
6
{
7 2 View Code Duplication
    public function getAllHorisontalValue($location, $data)
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...
8
    {
9 2
        $horisontalValue = [];
10 2
        $x = $location['x'];
11 2
        for ($i = $x; $i <= $x; $i++) {
12 2
            for ($j = 0; $j < 9; $j++) {
13 2
                if (!empty($data[$i][$j])) {
14 2
                    $horisontalValue[] = $data[$i][$j];
15
                }
16
            }
17
        }
18 2
        return $horisontalValue;
19
    }
20
21 2 View Code Duplication
    public function getAllVerticalValue($location, $data)
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...
22
    {
23 2
        $verticalValue = [];
24 2
        $y = $location['y'];
25 2
        for ($i = 0; $i < 9; $i++) {
26 2
            for ($j = $y; $j <= $y; $j++) {
27 2
                if (!empty($data[$i][$j])) {
28 2
                    $verticalValue[] = $data[$i][$j];
29
                }
30
            }
31
        }
32 2
        return $verticalValue;
33
    }
34
35 1
    public function getAllSquereValue($location, $data)
36
    {
37 1
        $squereValue = [];
38 1
        $x = $location['x'];
39 1
        $y = $location['y'];
40
41 1
        $iStart = 3 * floor($x / 3);
42 1
        $iEnd   = $iStart + 2;
43 1
        $jStart = 3 * floor($y / 3);
44 1
        $jEnd   = $jStart + 2;
45
46 1
        for ($i = $iStart; $i <= $iEnd; $i++) {
47 1
            for ($j = $jStart; $j <= $jEnd; $j++) {
48 1
                if (!empty($data[$i][$j])) {
49 1
                    $squereValue[] = $data[$i][$j];
50
                }
51
            }
52
        }
53 1
        return $squereValue;
54
    }
55
56 2
    public function isValidSudoku($data)
57
    {
58 2
        $isValidSudoku = true;
59 2
        for ($x = 0; $x <= 8; $x++) {
60 2
            for ($y = 0; $y <= 8; $y++) {
61 2
                $currentValue = @$data[$x][$y];
62 2
                $dataTemp = $data;
63 2
                $dataTemp[$x][$y] = 0;
64 2
                $location = ["x" => $x, "y" => $y];
65
66 2
                if (empty($currentValue)) {
67 1
                    return false;
68
                }
69
70 2
                $horisontalValue = $this->getAllHorisontalValue($location, $dataTemp);
71 2
                if (in_array($currentValue, $horisontalValue)) {
72 1
                    return false;
73
                }
74
75 2
                $verticalValue = $this->getAllVerticalValue($location, $dataTemp);
76 2
                if (in_array($currentValue, $verticalValue)) {
77 1
                    return false;
78
                }
79
            }
80
        }
81 2
        return $isValidSudoku;
82
    }
83
84 1
    public function getBoard($data)
85
    {
86 1
        $board = "";
87 1
        for ($x = 0; $x <= 8; $x++) {
88 1
            if ($x % 3 === 0) {
89 1
                $board .= "  " . str_repeat("-", 23) . PHP_EOL;
90
            }
91 1
            for ($y = 0; $y <= 8; $y++) {
92 1
                if ($y % 3 === 0) {
93 1
                    $board .= " |";
94
                }
95 1
                if (!empty(@$data[$x][$y])) {
96 1
                    $board .= " " . $data[$x][$y];
97
                } else {
98 1
                    $board .= " \033[31m0\033[0m";
99
                }
100 1
                if ($y === 8) {
101 1
                    $board .= " | " . PHP_EOL;
102
                }
103
            }
104 1
            if ($x === 8) {
105 1
                $board .= "  " . str_repeat("-", 23) . PHP_EOL;
106
            }
107
        }
108
109 1
        return $board;
110
    }
111
}
112