1 | <?php |
||
7 | class Canvas extends \ArrayObject |
||
8 | { |
||
9 | |||
10 | private $pointer = ['X' => 0, 'Y' => 0]; |
||
11 | |||
12 | /** |
||
13 | * @var LoggerInterface |
||
14 | */ |
||
15 | private $logger; |
||
16 | |||
17 | 7 | public function __construct(LoggerInterface $logger = null, array $input = []) |
|
22 | |||
23 | 7 | public function getHeight() |
|
27 | |||
28 | 7 | public function getHighestRow() |
|
34 | |||
35 | 7 | public function getLowestRow() |
|
39 | |||
40 | public function getHighestCol() |
||
41 | { |
||
42 | $highestCol = 0; |
||
43 | foreach ($this as $row) { |
||
44 | foreach ($row as $c => $col) { |
||
45 | $highestCol = max($highestCol, $c); |
||
46 | } |
||
47 | } |
||
48 | |||
49 | return $highestCol; |
||
50 | } |
||
51 | |||
52 | public function setPointer($x, $y = null) |
||
59 | |||
60 | public function inc($direction = 'Y', $increment = 1) |
||
64 | |||
65 | public function write(array $data) |
||
66 | { |
||
67 | $this->logger->debug("Writing array to canvas", ['array' => $data]); |
||
68 | |||
69 | $y = $this->pointer['Y']; |
||
70 | |||
71 | foreach ($data as $row) { |
||
72 | |||
73 | foreach ($row as $x => $col) { |
||
74 | $this->set($x + $this->pointer['X'], $y, $col); |
||
75 | } |
||
76 | |||
77 | $y++; |
||
78 | } |
||
79 | |||
80 | $this->sort(); |
||
81 | } |
||
82 | |||
83 | 7 | private function set($x, $y, $data) |
|
89 | |||
90 | 7 | private function sort() |
|
91 | { |
||
92 | 7 | $this->ksort(); |
|
93 | 7 | foreach ($this as &$row) { |
|
94 | 7 | ksort($row); |
|
95 | } |
||
96 | 7 | } |
|
97 | |||
98 | 7 | public function insert(Canvas $canvas, $y, $x = 0, $h = 0, $w = 0) |
|
99 | { |
||
100 | 7 | $this->logger->debug("Inserting canvas at X:$x, Y:$y. Replacing W:$w, H:$h", ['canvas' => $canvas]); |
|
101 | |||
102 | //remove height |
||
103 | 7 | for ($i = $y; $i < $y + $h; $i++) { |
|
104 | 3 | if (isset($this[$i])) { |
|
105 | 3 | if ($x > 0) { |
|
106 | 1 | foreach ($this[$i] as $ci => $col) { |
|
107 | 1 | if ($ci > $x) { |
|
108 | 1 | unset($this[$i][$x]); |
|
109 | } |
||
110 | } |
||
111 | } else { |
||
112 | 2 | unset($this[$i]); |
|
113 | } |
||
114 | } |
||
115 | } |
||
116 | |||
117 | //make space |
||
118 | 7 | $insertHeight = $canvas->getHeight(); |
|
119 | 7 | foreach ($this->getArrayCopy() as $thisRowNum => $row) { |
|
120 | 6 | if ($thisRowNum >= $y + $h && $x === 0) { |
|
121 | 2 | $this->logger->debug("Shifting row $thisRowNum to " . ($thisRowNum + $insertHeight) . " due to >= Y:$y + H:$h = " . ($y + $h) . " and X === 0. Insertheight is: $insertHeight", ['row' => $row]); |
|
122 | |||
123 | //unset($this[$thisRowNum]); |
||
|
|||
124 | 6 | $this[$thisRowNum + $insertHeight - $h] = $row; |
|
125 | } |
||
126 | } |
||
127 | |||
128 | /*$startRowNum = $canvas->getLowestRow(); |
||
129 | $insertWidth = $canvas->getHighestCol() - $w; |
||
130 | foreach ($this->getArrayCopy() as $thisRowNum => $row) { |
||
131 | if ($thisRowNum == $startRowNum) { |
||
132 | foreach ($row as $thisColNum => $col) { |
||
133 | if ($thisColNum > $x + $w && $col != '') { |
||
134 | $this[$thisRowNum][$thisColNum + $insertWidth] = $col; |
||
135 | } |
||
136 | } |
||
137 | } |
||
138 | }*/ |
||
139 | |||
140 | 7 | $r = $c = 0; |
|
141 | 7 | foreach ($canvas as $row) { |
|
142 | 7 | foreach ($row as $c => $col) { |
|
143 | 7 | $this->set($x + $c, $y + $r, $col); |
|
144 | } |
||
145 | |||
146 | 7 | $r++; |
|
147 | } |
||
148 | |||
149 | 7 | $this->sort(); |
|
150 | 7 | } |
|
151 | |||
152 | public function __toString() |
||
156 | |||
157 | } |
||
158 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.