SudokuValidator   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 97
Duplicated Lines 24.74 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 0
dl 24
loc 97
ccs 59
cts 59
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isValidSudoku() 0 23 5
A getLocationHaveContents() 17 17 4
A isValidVertical() 0 9 2
A isValidHorisontal() 0 16 5
A isValidSquere() 7 25 5

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 Chipulaja\Algo\Backtrack\Sudoku;
4
5
class SudokuValidator
6
{
7 1
    public function isValidSudoku($data)
8
    {
9 1
        $isValid = true;
0 ignored issues
show
Unused Code introduced by
$isValid is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
10 1
        $locationHaveContents = $this->getLocationHaveContents($data);
11 1
        foreach ($locationHaveContents as $location) {
12 1
            $x = $location['x'];
13 1
            $y = $location['y'];
14 1
            $value = $data[$x][$y];
15 1
            $vertical = $this->isValidVertical($location, $value, $data);
16 1
            if ($vertical === false) {
17 1
                return false;
18
            }
19 1
            $horisontal = $this->isValidHorisontal($location, $value, $data);
20 1
            if ($horisontal === false) {
21 1
                return false;
22
            }
23 1
            $squere = $this->isValidSquere($location, $value, $data);
24 1
            if ($squere === false) {
25 1
                return false;
26
            }
27
        }
28 1
        return true;
29
    }
30
31 1 View Code Duplication
    public function getLocationHaveContents($data)
32
    {
33 1
        $location = [];
34 1
        foreach ($data as $x => $cells) {
35 1
            foreach ($cells as $y => $value) {
36 1
                if (!empty($value)) {
37 1
                    $position = (int)($x."".$y);
38 1
                    $location[$position] = [
39 1
                        'x' => $x,
40 1
                        'y' => $y
41
                    ];
42
                }
43
            }
44
        }
45
46 1
        return $location;
47
    }
48
49 1
    protected function isValidVertical($location, $value, $data)
50
    {
51 1
        $x = $location["x"];
52 1
        $count = array_count_values($data[$x]);
53 1
        if ($count[$value] > 1) {
54 1
            return false;
55
        }
56 1
        return true;
57
    }
58
59 1
    protected function isValidHorisontal($location, $value, $data)
60
    {
61 1
        $dataY = [];
62 1
        foreach ($data as $x => $cells) {
63 1
            foreach ($cells as $y => $cellValue) {
64 1
                if ($y == $location["y"]) {
65 1
                    $dataY[] = $cellValue;
66
                }
67
            }
68
        }
69 1
        $count = array_count_values($dataY);
70 1
        if ($count[$value] > 1) {
71 1
            return false;
72
        }
73 1
        return true;
74
    }
75
76 1
    protected function isValidSquere($location, $value, $data)
77
    {
78 1
        $squereData = [];
79 1
        $x = $location['x'];
80 1
        $y = $location['y'];
81
82 1
        $iStart = 3 * floor($x/3);
83 1
        $iEnd   = $iStart + 2;
84 1
        $jStart = 3 * floor($y/3);
85 1
        $jEnd   = $jStart + 2;
86
87 1 View Code Duplication
        for ($i=$iStart; $i<=$iEnd; $i++) {
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...
88 1
            for ($j=$jStart; $j<=$jEnd; $j++) {
89 1
                if (!empty($data[$i][$j])) {
90 1
                    $squereData[] = $data[$i][$j];
91
                }
92
            }
93
        }
94
95 1
        $count = array_count_values($squereData);
96 1
        if ($count[$value] > 1) {
97 1
            return false;
98
        }
99 1
        return true;
100
    }
101
}
102