Passed
Push — master ( 2a68f1...702eef )
by Craig
02:58
created
src/Grid.php 1 patch
Spacing   +50 added lines, -50 removed lines patch added patch discarded remove patch
@@ -60,7 +60,7 @@  discard block
 block discarded – undo
60 60
 
61 61
     protected function setWordsCollection(Collection $wordsCollection): self
62 62
     {
63
-        $this->wordsCollection = $wordsCollection->filter(function ($word) {
63
+        $this->wordsCollection = $wordsCollection->filter(function($word) {
64 64
             return strlen($word) >= $this->minWordLen && strlen($word) <= $this->maxWordLen;
65 65
         })->map(fn($word) => strtoupper($word));
66 66
 
@@ -72,7 +72,7 @@  discard block
 block discarded – undo
72 72
         $this->cells = array_fill(0, $this->gridSize * $this->gridSize, null);
73 73
 
74 74
         for ($i = 0; $i < (2 * $this->gridSize * $this->gridSize); $i++) {
75
-            $this->columnArray[$i]=$this->getColumnDefault($i);
75
+            $this->columnArray[$i] = $this->getColumnDefault($i);
76 76
         }
77 77
 
78 78
         return $this;
@@ -81,15 +81,15 @@  discard block
 block discarded – undo
81 81
     public function generate(): self
82 82
     {
83 83
         $blocks = $this->gridSize * $this->gridSize;
84
-        $i=rand(0, $blocks-1);
84
+        $i = rand(0, $blocks - 1);
85 85
 
86
-        $complete=0;
86
+        $complete = 0;
87 87
         while ($complete < $blocks) {
88 88
             $this->placeWord($i);
89 89
             $complete++;
90 90
             $i++;
91
-            if ($i==$blocks) {
92
-                $i=0;
91
+            if ($i == $blocks) {
92
+                $i = 0;
93 93
             }
94 94
         }
95 95
 
@@ -106,7 +106,7 @@  discard block
 block discarded – undo
106 106
             $len = rand($this->minWordLen, $this->gridSize);
107 107
         } while (in_array($len, $exclude));
108 108
 
109
-        $available = $this->wordsCollection->filter(function ($word) use ($len) {
109
+        $available = $this->wordsCollection->filter(function($word) use ($len) {
110 110
             return strlen($word) === $len;
111 111
         })->count();
112 112
 
@@ -118,25 +118,25 @@  discard block
 block discarded – undo
118 118
     protected function addPlacedWord(Word $word, int $increment, int $len): void
119 119
     {
120 120
         $string = '';
121
-        $flag=false;
121
+        $flag = false;
122 122
 
123
-        for ($i=$word->getStart(); $i<=$word->getEnd(); $i+=$increment) {
124
-            if ($this->cells[$i]=='') {
123
+        for ($i = $word->getStart(); $i <= $word->getEnd(); $i += $increment) {
124
+            if ($this->cells[$i] == '') {
125 125
                 $string .= '_';
126 126
             } else {
127 127
                 $string .= $this->cells[$i];
128
-                $flag=true;
128
+                $flag = true;
129 129
             }
130 130
         }
131 131
 
132
-        if (! $flag) {
132
+        if (!$flag) {
133 133
             $randomWord = $this->getRandomWord($len);
134 134
             $word->setLabel($word->getInversed() ? strrev($randomWord) : $randomWord);
135 135
             $this->addWord($word);
136 136
             return;
137 137
         }
138 138
 
139
-        if (strpos($string, '_')===false) {
139
+        if (strpos($string, '_') === false) {
140 140
             return;
141 141
         }
142 142
 
@@ -148,11 +148,11 @@  discard block
 block discarded – undo
148 148
     protected function placeWordHorizontally(Word $word, int $len): void
149 149
     {
150 150
         $inc = 1;
151
-        $word->setEnd($word->getStart()+$len-1);
151
+        $word->setEnd($word->getStart() + $len - 1);
152 152
                  // si mot placé sur 2 lignes on décale à gauche
153 153
         while ($this->columnArray[$word->getEnd()] < $this->columnArray[$word->getStart()]) {
154
-            $word->setStart($word->getStart()-1);
155
-            $word->setEnd($word->getStart()+$len-1);
154
+            $word->setStart($word->getStart() - 1);
155
+            $word->setEnd($word->getStart() + $len - 1);
156 156
         }
157 157
 
158 158
         $this->addPlacedWord($word, $inc, $len);
@@ -160,12 +160,12 @@  discard block
 block discarded – undo
160 160
 
161 161
     protected function placeWordVertical(Word $word, int $len): void
162 162
     {
163
-        $inc=$this->gridSize;
164
-        $word->setEnd($word->getStart()+($len*$this->gridSize)-$this->gridSize);
163
+        $inc = $this->gridSize;
164
+        $word->setEnd($word->getStart() + ($len * $this->gridSize) - $this->gridSize);
165 165
                 // si le mot dépasse la grille en bas, on décale vers le haut
166
-        while ($word->getEnd()>($this->gridSize*$this->gridSize)-1) {
167
-            $word->setStart($word->getStart()-$this->gridSize);
168
-            $word->setEnd($word->getStart()+($len*$this->gridSize)-$this->gridSize);
166
+        while ($word->getEnd() > ($this->gridSize * $this->gridSize) - 1) {
167
+            $word->setStart($word->getStart() - $this->gridSize);
168
+            $word->setEnd($word->getStart() + ($len * $this->gridSize) - $this->gridSize);
169 169
         }
170 170
 
171 171
         $this->addPlacedWord($word, $inc, $len);
@@ -173,34 +173,34 @@  discard block
 block discarded – undo
173 173
 
174 174
     protected function placeWordDiagonallyLtr(Word $word, int $len): void
175 175
     {
176
-        $inc=$this->gridSize+1;
177
-        $word->setEnd($word->getStart()+($len*($this->gridSize+1))-($this->gridSize+1));
176
+        $inc = $this->gridSize + 1;
177
+        $word->setEnd($word->getStart() + ($len * ($this->gridSize + 1)) - ($this->gridSize + 1));
178 178
                 // si le mot dépasse la grille à droite, on décale à gauche
179 179
         while ($this->columnArray[$word->getEnd()] < $this->columnArray[$word->getStart()]) {
180
-            $word->setStart($word->getStart()-1);
181
-            $word->setEnd($word->getStart()+($len*($this->gridSize+1))-($this->gridSize+1));
180
+            $word->setStart($word->getStart() - 1);
181
+            $word->setEnd($word->getStart() + ($len * ($this->gridSize + 1)) - ($this->gridSize + 1));
182 182
         }
183 183
                 // si le mot dépasse la grille en bas, on décale vers le haut
184
-        while ($word->getEnd()>($this->gridSize*$this->gridSize)-1) {
185
-            $word->setStart($word->getStart()-$this->gridSize);
186
-            $word->setEnd($word->getStart()+($len*($this->gridSize+1))-($this->gridSize+1));
184
+        while ($word->getEnd() > ($this->gridSize * $this->gridSize) - 1) {
185
+            $word->setStart($word->getStart() - $this->gridSize);
186
+            $word->setEnd($word->getStart() + ($len * ($this->gridSize + 1)) - ($this->gridSize + 1));
187 187
         }
188 188
         $this->addPlacedWord($word, $inc, $len);
189 189
     }
190 190
 
191 191
     protected function placeWordDiagonallyRtl(Word $word, int $len): void
192 192
     {
193
-        $inc=$this->gridSize-1;
194
-        $word->setEnd($word->getStart()+(($len-1)*($this->gridSize-1)));
193
+        $inc = $this->gridSize - 1;
194
+        $word->setEnd($word->getStart() + (($len - 1) * ($this->gridSize - 1)));
195 195
                 // si le mot sort de la grille à gauche, on décale à droite
196 196
         while ($this->columnArray[$word->getEnd()] > $this->columnArray[$word->getStart()]) {
197
-            $word->setStart($word->getStart()+1);
198
-            $word->setEnd($word->getStart()+(($len-1)*($this->gridSize-1)));
197
+            $word->setStart($word->getStart() + 1);
198
+            $word->setEnd($word->getStart() + (($len - 1) * ($this->gridSize - 1)));
199 199
         }
200 200
                 // si le mot dépasse la grille en bas, on décale vers le haut
201
-        while ($word->getEnd()>($this->gridSize*$this->gridSize)-1) {
202
-            $word->setStart($word->getStart()-$this->gridSize);
203
-            $word->setEnd($word->getStart()+(($len-1)*($this->gridSize-1)));
201
+        while ($word->getEnd() > ($this->gridSize * $this->gridSize) - 1) {
202
+            $word->setStart($word->getStart() - $this->gridSize);
203
+            $word->setEnd($word->getStart() + (($len - 1) * ($this->gridSize - 1)));
204 204
         }
205 205
         $this->addPlacedWord($word, $inc, $len);
206 206
     }
@@ -231,19 +231,19 @@  discard block
 block discarded – undo
231 231
 
232 232
     protected function getColumnDefault(int $x): int
233 233
     {
234
-        return ($x % $this->gridSize)+1;
234
+        return ($x % $this->gridSize) + 1;
235 235
     }
236 236
 
237 237
     protected function markWordUsed($word): void
238 238
     {
239
-        $this->wordsCollection = $this->wordsCollection->reject(function ($current) use ($word) {
239
+        $this->wordsCollection = $this->wordsCollection->reject(function($current) use ($word) {
240 240
             return $current === $word;
241 241
         });
242 242
     }
243 243
 
244 244
     protected function getRandomWord(int $len): string
245 245
     {
246
-        $word = $this->wordsCollection->filter(function ($word) use ($len) {
246
+        $word = $this->wordsCollection->filter(function($word) use ($len) {
247 247
             return strlen($word) === $len;
248 248
         })->random(1)->first();
249 249
 
@@ -255,7 +255,7 @@  discard block
 block discarded – undo
255 255
     protected function getWordLike(string $pattern): ?string
256 256
     {
257 257
         $pattern = str_replace("_", ".", $pattern);
258
-        $words = $this->wordsCollection->filter(function ($word) use ($pattern) {
258
+        $words = $this->wordsCollection->filter(function($word) use ($pattern) {
259 259
             return preg_match("/^$pattern\$/i", $word);
260 260
         });
261 261
 
@@ -276,37 +276,37 @@  discard block
 block discarded – undo
276 276
             return;
277 277
         }
278 278
 
279
-        $j=0;
279
+        $j = 0;
280 280
         switch ($word->getOrientation()) {
281 281
             case Word::HORIZONTAL:
282
-                for ($i=$word->getStart(); $j<strlen($word->getLabel()); $i++) {
283
-                    $this->cells[$i]=substr($word->getLabel(), $j, 1);
282
+                for ($i = $word->getStart(); $j < strlen($word->getLabel()); $i++) {
283
+                    $this->cells[$i] = substr($word->getLabel(), $j, 1);
284 284
                     $j++;
285 285
                 }
286 286
                 break;
287 287
 
288 288
             case Word::VERTICAL:
289
-                for ($i=$word->getStart(); $j<strlen($word->getLabel()); $i+=$this->gridSize) {
290
-                    $this->cells[$i]=substr($word->getLabel(), $j, 1);
289
+                for ($i = $word->getStart(); $j < strlen($word->getLabel()); $i += $this->gridSize) {
290
+                    $this->cells[$i] = substr($word->getLabel(), $j, 1);
291 291
                     $j++;
292 292
                 }
293 293
                 break;
294 294
 
295 295
             case Word::DIAGONAL_LEFT_TO_RIGHT:
296
-                for ($i=$word->getStart(); $j<strlen($word->getLabel()); $i+=$this->gridSize+1) {
297
-                    $this->cells[$i]=substr($word->getLabel(), $j, 1);
296
+                for ($i = $word->getStart(); $j < strlen($word->getLabel()); $i += $this->gridSize + 1) {
297
+                    $this->cells[$i] = substr($word->getLabel(), $j, 1);
298 298
                     $j++;
299 299
                 }
300 300
                 break;
301 301
 
302 302
             case Word::DIAGONAL_RIGHT_TO_LEFT:
303
-                for ($i=$word->getStart(); $j<strlen($word->getLabel()); $i+=$this->gridSize-1) {
304
-                    $this->cells[$i]=substr($word->getLabel(), $j, 1);
303
+                for ($i = $word->getStart(); $j < strlen($word->getLabel()); $i += $this->gridSize - 1) {
304
+                    $this->cells[$i] = substr($word->getLabel(), $j, 1);
305 305
                     $j++;
306 306
                 }
307 307
                 break;
308 308
         }
309
-        $this->wordsList[]=$word;
309
+        $this->wordsList[] = $word;
310 310
     }
311 311
 
312 312
     public function getTextGrid()
@@ -340,7 +340,7 @@  discard block
 block discarded – undo
340 340
 
341 341
     public function getPuzzleWords()
342 342
     {
343
-        return collect($this->wordsList)->map(function (Word $word) {
343
+        return collect($this->wordsList)->map(function(Word $word) {
344 344
             $label = $word->getLabel();
345 345
             if ($word->getInversed()) {
346 346
                 $label = strrev(/** @scrutinizer ignore-type */$label);
Please login to merge, or discard this patch.