| Conditions | 33 |
| Paths | 31 |
| Total Lines | 192 |
| Code Lines | 145 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 | // Check for castling: |
||
| 163 | if (preg_match('/^(0-0|0-0-0)(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
| 164 | $this->castling = $matches[1]; |
||
| 165 | $this->check = $matches[2] === '+'; |
||
| 166 | $this->checkmate = $matches[2] === '#'; |
||
| 167 | $this->annotation = isset($matches[3]) ? $matches[3] : null; |
||
| 168 | return; |
||
| 169 | } |
||
| 170 | |||
| 171 | // Pawn movement: |
||
| 172 | if (preg_match('/^([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
| 173 | $this->targetColumn = $matches[1]; |
||
| 174 | $this->targetRow = (int)$matches[2]; |
||
| 175 | $this->check = $matches[3] === '+'; |
||
| 176 | $this->checkmate = $matches[3] === '#'; |
||
| 177 | $this->movedPiece = self::PIECE_PAWN; |
||
| 178 | $this->annotation = isset($matches[4]) ? $matches[4] : null; |
||
| 179 | return; |
||
| 180 | } |
||
| 181 | |||
| 182 | // Pawn movement (long san): |
||
| 183 | if (preg_match('/^([a-h])([1-8])([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
| 184 | $this->movedPieceDisambiguationColumn = $matches[1]; |
||
| 185 | $this->movedPieceDisambiguationRow = (int)$matches[2]; |
||
| 186 | $this->targetColumn = $matches[3]; |
||
| 187 | $this->targetRow = (int)$matches[4]; |
||
| 188 | $this->check = $matches[5] === '+'; |
||
| 189 | $this->checkmate = $matches[5] === '#'; |
||
| 190 | $this->movedPiece = self::PIECE_PAWN; |
||
| 191 | $this->annotation = isset($matches[6]) ? $matches[6] : null; |
||
| 192 | $this->longSan = true; |
||
| 193 | return; |
||
| 194 | } |
||
| 195 | |||
| 196 | // Piece movement: |
||
| 197 | if (preg_match('/^([KQBNR])([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
| 198 | $this->movedPiece = $matches[1]; |
||
| 199 | $this->targetColumn = $matches[2]; |
||
| 200 | $this->targetRow = (int)$matches[3]; |
||
| 201 | $this->check = $matches[4] === '+'; |
||
| 202 | $this->checkmate = $matches[4] === '#'; |
||
| 203 | $this->annotation = isset($matches[5]) ? $matches[5] : null; |
||
| 204 | return; |
||
| 205 | } |
||
| 206 | |||
| 207 | // Piece movement from a specific column: |
||
| 208 | if (preg_match('/^([KQBNR])([a-h])([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
| 209 | $this->movedPiece = $matches[1]; |
||
| 210 | $this->movedPieceDisambiguationColumn = $matches[2]; |
||
| 211 | $this->targetColumn = $matches[3]; |
||
| 212 | $this->targetRow = (int)$matches[4]; |
||
| 213 | $this->check = $matches[5] === '+'; |
||
| 214 | $this->checkmate = $matches[5] === '#'; |
||
| 215 | $this->annotation = isset($matches[6]) ? $matches[6] : null; |
||
| 216 | return; |
||
| 217 | } |
||
| 218 | |||
| 219 | // Piece movement from a specific row: |
||
| 220 | if (preg_match('/^([KQBNR])([0-9])([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
| 221 | $this->movedPiece = $matches[1]; |
||
| 222 | $this->movedPieceDisambiguationRow = (int)$matches[2]; |
||
| 223 | $this->targetColumn = $matches[3]; |
||
| 224 | $this->targetRow = (int)$matches[4]; |
||
| 225 | $this->check = $matches[5] === '+'; |
||
| 226 | $this->checkmate = $matches[5] === '#'; |
||
| 227 | $this->annotation = isset($matches[6]) ? $matches[6] : null; |
||
| 228 | return; |
||
| 229 | } |
||
| 230 | |||
| 231 | // Piece movement from a specific column and row (long san): |
||
| 232 | if (preg_match('/^([KQBNR])([a-h])([0-9])([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
| 233 | $this->movedPiece = $matches[1]; |
||
| 234 | $this->movedPieceDisambiguationColumn = $matches[2]; |
||
| 235 | $this->movedPieceDisambiguationRow = (int)$matches[3]; |
||
| 236 | $this->targetColumn = $matches[4]; |
||
| 237 | $this->targetRow = (int)$matches[5]; |
||
| 238 | $this->check = $matches[6] === '+'; |
||
| 239 | $this->checkmate = $matches[6] === '#'; |
||
| 240 | $this->annotation = isset($matches[7]) ? $matches[7] : null; |
||
| 241 | $this->longSan = true; |
||
| 242 | return; |
||
| 243 | } |
||
| 244 | |||
| 245 | // Pawn capture: |
||
| 246 | if (preg_match('/^([a-h])x([a-h])([1-8])(?:=?([KQBNR]))?(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
| 247 | $this->targetColumn = $matches[2]; |
||
| 248 | $this->targetRow = (int)$matches[3]; |
||
| 249 | $this->movedPiece = self::PIECE_PAWN; |
||
| 250 | $this->movedPieceDisambiguationColumn = $matches[1]; |
||
| 251 | $this->capture = true; |
||
| 252 | $this->promotedPiece = $matches[4] ?: null; |
||
| 253 | $this->check = $matches[5] === '+'; |
||
| 254 | $this->checkmate = $matches[5] === '#'; |
||
| 255 | $this->annotation = isset($matches[6]) ? $matches[6] : null; |
||
| 256 | return; |
||
| 257 | } |
||
| 258 | |||
| 259 | // Pawn capture (long san): |
||
| 260 | if (preg_match('/^([a-h])([1-8])x([a-h])([1-8])(?:=?([KQBNR]))?(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
| 261 | $this->targetColumn = $matches[3]; |
||
| 262 | $this->targetRow = (int)$matches[4]; |
||
| 263 | $this->movedPiece = self::PIECE_PAWN; |
||
| 264 | $this->movedPieceDisambiguationColumn = $matches[1]; |
||
| 265 | $this->movedPieceDisambiguationRow = (int)$matches[2]; |
||
| 266 | $this->capture = true; |
||
| 267 | $this->promotedPiece = $matches[5] ?: null; |
||
| 268 | $this->check = $matches[6] === '+'; |
||
| 269 | $this->checkmate = $matches[6] === '#'; |
||
| 270 | $this->annotation = isset($matches[7]) ? $matches[7] : null; |
||
| 271 | $this->longSan = true; |
||
| 272 | return; |
||
| 273 | } |
||
| 274 | |||
| 275 | // Piece capture: |
||
| 276 | if (preg_match('/^([KQBNR])x([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
| 277 | $this->movedPiece = $matches[1]; |
||
| 278 | $this->targetColumn = $matches[2]; |
||
| 279 | $this->targetRow = (int)$matches[3]; |
||
| 280 | $this->check = $matches[4] === '+'; |
||
| 281 | $this->checkmate = $matches[4] === '#'; |
||
| 282 | $this->capture = true; |
||
| 283 | $this->annotation = isset($matches[5]) ? $matches[5] : null; |
||
| 284 | return; |
||
| 285 | } |
||
| 286 | |||
| 287 | // Piece capture from a specific column: |
||
| 288 | if (preg_match('/^([KQBNR])([a-h])x([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
| 289 | $this->movedPiece = $matches[1]; |
||
| 290 | $this->movedPieceDisambiguationColumn = $matches[2]; |
||
| 291 | $this->targetColumn = $matches[3]; |
||
| 292 | $this->targetRow = (int)$matches[4]; |
||
| 293 | $this->check = $matches[5] === '+'; |
||
| 294 | $this->checkmate = $matches[5] === '#'; |
||
| 295 | $this->capture = true; |
||
| 296 | $this->annotation = isset($matches[6]) ? $matches[6] : null; |
||
| 297 | return; |
||
| 298 | } |
||
| 299 | |||
| 300 | // Piece capture from a specific row: |
||
| 301 | if (preg_match('/^([KQBNR])([0-9])x([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
| 302 | $this->movedPiece = $matches[1]; |
||
| 303 | $this->movedPieceDisambiguationRow = (int)$matches[2]; |
||
| 304 | $this->targetColumn = $matches[3]; |
||
| 305 | $this->targetRow = (int)$matches[4]; |
||
| 306 | $this->check = $matches[5] === '+'; |
||
| 307 | $this->checkmate = $matches[5] === '#'; |
||
| 308 | $this->capture = true; |
||
| 309 | $this->annotation = isset($matches[6]) ? $matches[6] : null; |
||
| 310 | return; |
||
| 311 | } |
||
| 312 | |||
| 313 | // Piece capture from a specific column and row (long san): |
||
| 314 | if (preg_match('/^([KQBNR])([a-h])([0-9])x([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
| 315 | $this->movedPiece = $matches[1]; |
||
| 316 | $this->movedPieceDisambiguationColumn = $matches[2]; |
||
| 317 | $this->movedPieceDisambiguationRow = (int)$matches[3]; |
||
| 318 | $this->targetColumn = $matches[4]; |
||
| 319 | $this->targetRow = (int)$matches[5]; |
||
| 320 | $this->check = $matches[6] === '+'; |
||
| 321 | $this->checkmate = $matches[6] === '#'; |
||
| 322 | $this->capture = true; |
||
| 323 | $this->annotation = isset($matches[7]) ? $matches[7] : null; |
||
| 324 | $this->longSan = true; |
||
| 325 | return; |
||
| 326 | } |
||
| 327 | |||
| 328 | // Check for pawn promotion: |
||
| 329 | if (preg_match('/^([a-h])([1-8])=?([KQBNR])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
| 330 | $this->movedPiece = self::PIECE_PAWN; |
||
| 331 | $this->targetColumn = $matches[1]; |
||
| 332 | $this->targetRow = (int)$matches[2]; |
||
| 333 | $this->promotedPiece = $matches[3]; |
||
| 334 | $this->check = $matches[4] === '+'; |
||
| 335 | $this->checkmate = $matches[4] === '#'; |
||
| 336 | $this->annotation = isset($matches[5]) ? $matches[5] : null; |
||
| 337 | return; |
||
| 338 | } |
||
| 339 | |||
| 340 | throw new InvalidArgumentException(sprintf( |
||
| 341 | 'The value "%s" could not be parsed.', |
||
| 342 | $value |
||
| 343 | )); |
||
| 603 |