Conditions | 31 |
Paths | 29 |
Total Lines | 183 |
Code Lines | 139 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php declare(strict_types=1); |
||
151 | private function parse(string $value): void |
||
152 | { |
||
153 | // Check for castling: |
||
154 | if (preg_match('/^(O-O|O-O-O)(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
155 | $this->castling = $matches[1]; |
||
156 | $this->check = $matches[2] === '+'; |
||
157 | $this->checkmate = $matches[2] === '#'; |
||
158 | $this->annotation = isset($matches[3]) ? $matches[3] : null; |
||
159 | return; |
||
160 | } |
||
161 | |||
162 | // Pawn movement: |
||
163 | if (preg_match('/^([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
164 | $this->targetColumn = $matches[1]; |
||
165 | $this->targetRow = (int)$matches[2]; |
||
166 | $this->check = $matches[3] === '+'; |
||
167 | $this->checkmate = $matches[3] === '#'; |
||
168 | $this->movedPiece = self::PIECE_PAWN; |
||
169 | $this->annotation = isset($matches[4]) ? $matches[4] : null; |
||
170 | return; |
||
171 | } |
||
172 | |||
173 | // Pawn movement (long san): |
||
174 | if (preg_match('/^([a-h])([1-8])([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
175 | $this->movedPieceDisambiguationColumn = $matches[1]; |
||
176 | $this->movedPieceDisambiguationRow = (int)$matches[2]; |
||
177 | $this->targetColumn = $matches[3]; |
||
178 | $this->targetRow = (int)$matches[4]; |
||
179 | $this->check = $matches[5] === '+'; |
||
180 | $this->checkmate = $matches[5] === '#'; |
||
181 | $this->movedPiece = self::PIECE_PAWN; |
||
182 | $this->annotation = isset($matches[6]) ? $matches[6] : null; |
||
183 | $this->longSan = true; |
||
184 | return; |
||
185 | } |
||
186 | |||
187 | // Piece movement: |
||
188 | if (preg_match('/^([KQBNR])([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
189 | $this->movedPiece = $matches[1]; |
||
190 | $this->targetColumn = $matches[2]; |
||
191 | $this->targetRow = (int)$matches[3]; |
||
192 | $this->check = $matches[4] === '+'; |
||
193 | $this->checkmate = $matches[4] === '#'; |
||
194 | $this->annotation = isset($matches[5]) ? $matches[5] : null; |
||
195 | return; |
||
196 | } |
||
197 | |||
198 | // Piece movement from a specific column: |
||
199 | if (preg_match('/^([KQBNR])([a-h])([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
200 | $this->movedPiece = $matches[1]; |
||
201 | $this->movedPieceDisambiguationColumn = $matches[2]; |
||
202 | $this->targetColumn = $matches[3]; |
||
203 | $this->targetRow = (int)$matches[4]; |
||
204 | $this->check = $matches[5] === '+'; |
||
205 | $this->checkmate = $matches[5] === '#'; |
||
206 | $this->annotation = isset($matches[6]) ? $matches[6] : null; |
||
207 | return; |
||
208 | } |
||
209 | |||
210 | // Piece movement from a specific row: |
||
211 | if (preg_match('/^([KQBNR])([0-9])([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
212 | $this->movedPiece = $matches[1]; |
||
213 | $this->movedPieceDisambiguationRow = (int)$matches[2]; |
||
214 | $this->targetColumn = $matches[3]; |
||
215 | $this->targetRow = (int)$matches[4]; |
||
216 | $this->check = $matches[5] === '+'; |
||
217 | $this->checkmate = $matches[5] === '#'; |
||
218 | $this->annotation = isset($matches[6]) ? $matches[6] : null; |
||
219 | return; |
||
220 | } |
||
221 | |||
222 | // Piece movement from a specific column and row (long san): |
||
223 | if (preg_match('/^([KQBNR])([a-h])([0-9])([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
224 | $this->movedPiece = $matches[1]; |
||
225 | $this->movedPieceDisambiguationColumn = $matches[2]; |
||
226 | $this->movedPieceDisambiguationRow = (int)$matches[3]; |
||
227 | $this->targetColumn = $matches[4]; |
||
228 | $this->targetRow = (int)$matches[5]; |
||
229 | $this->check = $matches[6] === '+'; |
||
230 | $this->checkmate = $matches[6] === '#'; |
||
231 | $this->annotation = isset($matches[7]) ? $matches[7] : null; |
||
232 | $this->longSan = true; |
||
233 | return; |
||
234 | } |
||
235 | |||
236 | // Pawn capture: |
||
237 | if (preg_match('/^([a-h])x([a-h])([1-8])(?:=([KQBNR]))?(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
238 | $this->targetColumn = $matches[2]; |
||
239 | $this->targetRow = (int)$matches[3]; |
||
240 | $this->movedPiece = self::PIECE_PAWN; |
||
241 | $this->movedPieceDisambiguationColumn = $matches[1]; |
||
242 | $this->capture = true; |
||
243 | $this->promotedPiece = $matches[4] ?: null; |
||
244 | $this->check = $matches[5] === '+'; |
||
245 | $this->checkmate = $matches[5] === '#'; |
||
246 | $this->annotation = isset($matches[6]) ? $matches[6] : null; |
||
247 | return; |
||
248 | } |
||
249 | |||
250 | // Pawn capture (long san): |
||
251 | if (preg_match('/^([a-h])([1-8])x([a-h])([1-8])(?:=([KQBNR]))?(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
252 | $this->targetColumn = $matches[3]; |
||
253 | $this->targetRow = (int)$matches[4]; |
||
254 | $this->movedPiece = self::PIECE_PAWN; |
||
255 | $this->movedPieceDisambiguationColumn = $matches[1]; |
||
256 | $this->movedPieceDisambiguationRow = (int)$matches[2]; |
||
257 | $this->capture = true; |
||
258 | $this->promotedPiece = $matches[5] ?: null; |
||
259 | $this->check = $matches[6] === '+'; |
||
260 | $this->checkmate = $matches[6] === '#'; |
||
261 | $this->annotation = isset($matches[7]) ? $matches[7] : null; |
||
262 | $this->longSan = true; |
||
263 | return; |
||
264 | } |
||
265 | |||
266 | // Piece capture: |
||
267 | if (preg_match('/^([KQBNR])x([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
268 | $this->movedPiece = $matches[1]; |
||
269 | $this->targetColumn = $matches[2]; |
||
270 | $this->targetRow = (int)$matches[3]; |
||
271 | $this->check = $matches[4] === '+'; |
||
272 | $this->checkmate = $matches[4] === '#'; |
||
273 | $this->capture = true; |
||
274 | $this->annotation = isset($matches[5]) ? $matches[5] : null; |
||
275 | return; |
||
276 | } |
||
277 | |||
278 | // Piece capture from a specific column: |
||
279 | if (preg_match('/^([KQBNR])([a-h])x([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
280 | $this->movedPiece = $matches[1]; |
||
281 | $this->movedPieceDisambiguationColumn = $matches[2]; |
||
282 | $this->targetColumn = $matches[3]; |
||
283 | $this->targetRow = (int)$matches[4]; |
||
284 | $this->check = $matches[5] === '+'; |
||
285 | $this->checkmate = $matches[5] === '#'; |
||
286 | $this->capture = true; |
||
287 | $this->annotation = isset($matches[6]) ? $matches[6] : null; |
||
288 | return; |
||
289 | } |
||
290 | |||
291 | // Piece capture from a specific row: |
||
292 | if (preg_match('/^([KQBNR])([0-9])x([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
293 | $this->movedPiece = $matches[1]; |
||
294 | $this->movedPieceDisambiguationRow = (int)$matches[2]; |
||
295 | $this->targetColumn = $matches[3]; |
||
296 | $this->targetRow = (int)$matches[4]; |
||
297 | $this->check = $matches[5] === '+'; |
||
298 | $this->checkmate = $matches[5] === '#'; |
||
299 | $this->capture = true; |
||
300 | $this->annotation = isset($matches[6]) ? $matches[6] : null; |
||
301 | return; |
||
302 | } |
||
303 | |||
304 | // Piece capture from a specific column and row (long san): |
||
305 | if (preg_match('/^([KQBNR])([a-h])([0-9])x([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
306 | $this->movedPiece = $matches[1]; |
||
307 | $this->movedPieceDisambiguationColumn = $matches[2]; |
||
308 | $this->movedPieceDisambiguationRow = (int)$matches[3]; |
||
309 | $this->targetColumn = $matches[4]; |
||
310 | $this->targetRow = (int)$matches[5]; |
||
311 | $this->check = $matches[6] === '+'; |
||
312 | $this->checkmate = $matches[6] === '#'; |
||
313 | $this->capture = true; |
||
314 | $this->annotation = isset($matches[7]) ? $matches[7] : null; |
||
315 | $this->longSan = true; |
||
316 | return; |
||
317 | } |
||
318 | |||
319 | // Check for pawn promotion: |
||
320 | if (preg_match('/^([a-h])([1-8])=?([KQBNR])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
321 | $this->movedPiece = self::PIECE_PAWN; |
||
322 | $this->targetColumn = $matches[1]; |
||
323 | $this->targetRow = (int)$matches[2]; |
||
324 | $this->promotedPiece = $matches[3]; |
||
325 | $this->check = $matches[4] === '+'; |
||
326 | $this->checkmate = $matches[4] === '#'; |
||
327 | $this->annotation = isset($matches[5]) ? $matches[5] : null; |
||
328 | return; |
||
329 | } |
||
330 | |||
331 | throw new InvalidArgumentException(sprintf( |
||
332 | 'The value "%s" could not be parsed.', |
||
333 | $value |
||
334 | )); |
||
594 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: