Total Complexity | 49 |
Total Lines | 290 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 1 | Features | 1 |
Complex classes like Grid often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Grid, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Grid |
||
13 | { |
||
14 | |||
15 | use HasWordCollection; |
||
16 | |||
17 | |||
18 | /** |
||
19 | * minimum word length for the puzzle |
||
20 | */ |
||
21 | protected int $minWordLen; |
||
22 | |||
23 | /** |
||
24 | * maximium word length for the puzzle |
||
25 | */ |
||
26 | protected int $maxWordLen; |
||
27 | |||
28 | /** |
||
29 | * grid side length |
||
30 | */ |
||
31 | protected int $gridSize; |
||
32 | |||
33 | /** |
||
34 | * cells holding the collection of lettes |
||
35 | */ |
||
36 | protected array $cells = []; |
||
37 | |||
38 | /** |
||
39 | * Words used within the puzzle |
||
40 | */ |
||
41 | protected array $wordsList = []; |
||
42 | |||
43 | /** |
||
44 | * column mapping |
||
45 | */ |
||
46 | protected array $columnArray = []; |
||
47 | |||
48 | public function __construct(int $gridSize, int $minWordLen, int $maxWordLen, Collection $wordsCollection) |
||
49 | { |
||
50 | $this->minWordLen = $minWordLen; |
||
51 | $this->maxWordLen = $maxWordLen; |
||
52 | |||
53 | $this->setGridsize($gridSize) |
||
54 | ->setWordsCollection($wordsCollection) |
||
55 | ->initGrid(); |
||
56 | } |
||
57 | |||
58 | protected function setGridSize(int $gridSize): self |
||
69 | } |
||
70 | |||
71 | protected function initGrid(): self |
||
72 | { |
||
73 | $this->cells = array_fill(0, $this->gridSize * $this->gridSize, null); |
||
74 | |||
75 | for ($i = 0; $i < (2 * $this->gridSize * $this->gridSize); $i++) { |
||
76 | $this->columnArray[$i]=$this->getColumnDefault($i); |
||
77 | } |
||
78 | |||
79 | return $this; |
||
80 | } |
||
81 | |||
82 | public function generate(): self |
||
83 | { |
||
84 | $blocks = $this->gridSize * $this->gridSize; //36 |
||
85 | $i=rand(0, $blocks-1); |
||
86 | |||
87 | $complete=0; //0 |
||
88 | while ($complete < $blocks) { |
||
89 | $this->placeWord($i); |
||
90 | $complete++; |
||
91 | $i++; |
||
92 | if ($i==$blocks) { |
||
93 | $i=0; |
||
94 | } |
||
95 | } |
||
96 | |||
97 | return $this; |
||
98 | } |
||
99 | |||
100 | protected function addPlacedWord(Word $word, int $increment, int $len): void |
||
101 | { |
||
102 | $string = ''; |
||
103 | $flag=false; |
||
104 | |||
105 | for ($i=$word->getStart(); $i<=$word->getEnd(); $i+=$increment) { |
||
106 | if ($this->cells[$i] === null) { |
||
107 | $string .= '_'; |
||
108 | } else { |
||
109 | $string .= $this->cells[$i]; |
||
110 | $flag=true; |
||
111 | } |
||
112 | } |
||
113 | |||
114 | if (! $flag) { |
||
|
|||
115 | $randomWord = $this->getRandomWord($len); |
||
116 | $word->setLabel($randomWord); |
||
117 | $this->addWord($word); |
||
118 | return; |
||
119 | } |
||
120 | |||
121 | if (Str::contains($string, '_')===false) { |
||
122 | return; |
||
123 | } |
||
124 | |||
125 | $word->setInversed(false)->setLabel($this->getWordLike($string)); |
||
126 | $this->addWord($word); |
||
127 | } |
||
128 | |||
129 | protected function placeWordHorizontally(Word $word, int $len): void |
||
139 | } |
||
140 | |||
141 | protected function placeWordVertical(Word $word, int $len): void |
||
142 | { |
||
143 | $inc=$this->gridSize; |
||
144 | $word->setEnd($word->getStart()+($len*$this->gridSize)-$this->gridSize); |
||
145 | while ($word->getEnd()>($this->gridSize*$this->gridSize)-1) { |
||
146 | $word->setStart($word->getStart()-$this->gridSize); |
||
147 | $word->setEnd($word->getStart()+($len*$this->gridSize)-$this->gridSize); |
||
148 | } |
||
149 | |||
150 | $this->addPlacedWord($word, $inc, $len); |
||
151 | } |
||
152 | |||
153 | protected function placeWordDiagonallyLtr(Word $word, int $len): void |
||
154 | { |
||
155 | $inc=$this->gridSize+1; |
||
156 | $word->setEnd($word->getStart()+($len*($this->gridSize+1))-($this->gridSize+1)); |
||
157 | while ($this->columnArray[$word->getEnd()] < $this->columnArray[$word->getStart()]) { |
||
158 | $word->setStart($word->getStart()-1); |
||
159 | $word->setEnd($word->getStart()+($len*($this->gridSize+1))-($this->gridSize+1)); |
||
160 | } |
||
161 | while ($word->getEnd()>($this->gridSize*$this->gridSize)-1) { |
||
162 | $word->setStart($word->getStart()-$this->gridSize); |
||
163 | $word->setEnd($word->getStart()+($len*($this->gridSize+1))-($this->gridSize+1)); |
||
164 | } |
||
165 | $this->addPlacedWord($word, $inc, $len); |
||
166 | } |
||
167 | |||
168 | protected function placeWordDiagonallyRtl(Word $word, int $len): void |
||
169 | { |
||
170 | $inc=$this->gridSize-1; |
||
171 | $word->setEnd($word->getStart()+(($len-1)*($this->gridSize-1))); |
||
172 | while ($this->columnArray[$word->getEnd()] > $this->columnArray[$word->getStart()]) { |
||
173 | $word->setStart($word->getStart()+1); |
||
174 | $word->setEnd($word->getStart()+(($len-1)*($this->gridSize-1))); |
||
175 | } |
||
176 | while ($word->getEnd()>($this->gridSize*$this->gridSize)-1) { |
||
177 | $word->setStart($word->getStart()-$this->gridSize); |
||
178 | $word->setEnd($word->getStart()+(($len-1)*($this->gridSize-1))); |
||
179 | } |
||
180 | $this->addPlacedWord($word, $inc, $len); |
||
181 | } |
||
182 | |||
183 | protected function placeWord($start): void |
||
207 | } |
||
208 | } |
||
209 | |||
210 | protected function getColumnDefault(int $x): int |
||
211 | { |
||
213 | } |
||
214 | |||
215 | protected function addWord(Word $word): void |
||
216 | { |
||
217 | if ($word->getLabel() === null) { |
||
218 | return; |
||
219 | } |
||
220 | |||
221 | $j=0; |
||
222 | $incrementBy = 1; |
||
223 | switch ($word->getOrientation()) { |
||
224 | case Word::HORIZONTAL: |
||
225 | $incrementBy=1; |
||
226 | break; |
||
227 | |||
228 | case Word::VERTICAL: |
||
229 | $incrementBy=$this->gridSize; |
||
230 | break; |
||
231 | |||
232 | case Word::DIAGONAL_LEFT_TO_RIGHT: |
||
233 | $incrementBy=$this->gridSize+1; |
||
234 | break; |
||
235 | |||
236 | case Word::DIAGONAL_RIGHT_TO_LEFT: |
||
237 | $incrementBy=$this->gridSize-1; |
||
238 | break; |
||
239 | } |
||
240 | |||
241 | for ($i = $word->getStart(); $j < Str::length($word->getLabel()); $i += $incrementBy) { |
||
242 | $nchar = Str::substr($word->getLabel(), $j, 1); |
||
243 | if ($this->cells[$i] !== $nchar |
||
244 | && ! is_null($this->cells[$i])) { |
||
245 | throw new RuntimeException("Null or Char required: {$this->cells[$i]} - {$nchar}"); |
||
246 | } |
||
247 | $this->cells[$i] = Str::substr($word->getLabel(), $j, 1); |
||
248 | $j++; |
||
249 | } |
||
250 | |||
251 | $this->wordsList[]=$word; |
||
252 | } |
||
253 | |||
254 | public function getTextGrid() |
||
255 | { |
||
256 | $r = ''; |
||
257 | foreach ($this->getGrid() as $idx => $row) { |
||
258 | if ($idx > 0) { |
||
259 | $r .= "\n"; |
||
260 | } |
||
261 | $r .= implode(" ", $row); |
||
262 | } |
||
263 | return $r; |
||
264 | } |
||
265 | |||
266 | public function getGrid() |
||
287 | } |
||
288 | |||
289 | public function getPuzzleWords() |
||
290 | { |
||
291 | return collect($this->wordsList)->map(function (Word $word) { |
||
292 | $str = $word->getLabel(true); |
||
293 | $charClass = resolve(config('word-finder.character_map')); |
||
294 | |||
295 | return method_exists($charClass, 'unmapChars') ? $charClass->unmapChars($str) : $str; |
||
296 | }); |
||
297 | } |
||
298 | |||
299 | public function getGridSize() |
||
302 | } |
||
303 | } |
||
304 |