Passed
Push — master ( e3a586...14f326 )
by Roman
02:27
created

Table::rotate()   C

Complexity

Conditions 11
Paths 4

Size

Total Lines 39
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 11

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 39
ccs 37
cts 37
cp 1
rs 5.2653
cc 11
eloc 33
nc 4
nop 1
crap 11

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 ToolkitLab\ASCII;
4
5
class Table {
6
7
    private $data = [];
8
    private $dimensionX;
9
    private $dimensionY;
10
    private $columnsMaxLenght = [];
11
12 36
    public function __construct($_data, $_dimensionX = null, $_dimensionY = null) {
13 36
        $this->data = $_data;
14 36
        if (is_null($_dimensionX)) {
15 34
            $this->calculateDimensions();
16 17
        } else {
17 4
            $this->dimensionX = $_dimensionX;
18 4
            $this->dimensionY = $_dimensionY;
19
        }
20 36
    }
21
22 28
    public function getCell($x, $y) {
23 28
        if ($x >= $this->dimensionX || $y >= $this->dimensionY) {
24 2
            throw new \InvalidArgumentException("Index Out of range");
25
        }
26 26
        if (!isset($this->data[$y][$x])) {
27 2
            return "";
28
        }
29 26
        return $this->data[$y][$x];
30
    }
31
32
    /**
33
     * @param int $columnIndex
34
     * @return int
35
     */
36 22
    public function getColumnsMaxLenght($columnIndex) {
37 22
        if (isset($this->columnsMaxLenght[$columnIndex])) {
38 20
                    return $this->columnsMaxLenght[$columnIndex];
39
        }
40 22
        $width = 0;
41 22
        for ($y = 0; $y < $this->dimensionY; $y++) {
42 22
            $len = strlen($this->getCell($columnIndex, $y));
43 22
            if ($len > $width) {
44 22
                            $width = $len;
45 11
            }
46 11
        }
47
48 22
        $this->columnsMaxLenght[$columnIndex] = $width;
49 22
        return $width;
50
    }
51
52
    /**
53
     * @return array
54
     */
55 4
    public function getData() {
56 4
        return $this->data;
57
    }
58
59 34
    private function calculateDimensions() {
60 34
        $this->dimensionY = $this->dimensionX = 0;
61
62 34
        foreach ($this->data as $row) {
63 34
            $cnt = count($row);
64 34
            if ($cnt > $this->dimensionX) {
65 34
                            $this->dimensionX = $cnt;
66 17
            }
67 17
        }
68 34
        $this->dimensionY = count($this->data);
69 34
    }
70
71
    /**
72
     * @param array $data
73
     */
74 6
    public function setData($data) {
75 6
        $this->data = $data;
76 6
        $this->calculateDimensions();
77 6
    }
78
79
    /**
80
     * @return mixed
81
     */
82 26
    public function getDimensionX() {
83 26
        return $this->dimensionX;
84
    }
85
86
    /**
87
     * @return mixed
88
     */
89 24
    public function getDimensionY() {
90 24
        return $this->dimensionY;
91
    }
92
    
93
    /**
94
     * Rotate the table
95
     * @param int $angle 90, 180, 270, -90, -180, -270
96
     * @return void
97
     * @throws \InvalidArgumentException
98
     */
99 4
    public function rotate($angle) {
100 4
        $data = [];
101
        switch($angle) {
102 4
            case 90:
103 2
                for($i = count($this->data) - 1; $i >= 0; $i--) {
104 2
                    foreach($this->data[$i] as $key => $val) {
105 2
                        $data[$key][] = $val;
106 1
                    }
107 1
                }
108 2
                break;
109 4
            case 180:
110 2
                $this->rotate(90);
111 2
                $this->rotate(90);
112 2
                return;
113 4
            case 270:
114 2
                $this->rotate(-90);
115 2
                return;
116 4
            case -90:
117 2
                for ($i = $this->getDimensionX() -1; $i >= 0; $i--) {
118 2
                    $row = [];
119 2
                    foreach ($this->data as $val) {
120 2
                        $row[] = $val[$i];
121 1
                    }
122 2
                    $data[] = $row;
123 1
                }
124 2
                break;
125 4
            case -180:
126 2
                $this->rotate(90);
127 2
                $this->rotate(90);
128 2
                return;
129
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
130 4
            case -270:
131 2
                $this->rotate(90);
132 2
                return;
133 1
            default:
134 2
                throw new \InvalidArgumentException("The angle should be one of the following: 90, 180, 270, -90, -180, -270");
135 1
        }
136 2
        $this->data = $data;
137 2
        $this->calculateDimensions();
138 2
    }
139
140
}
141