| Conditions | 22 |
| Paths | 21 |
| Total Lines | 124 |
| Code Lines | 92 |
| 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); |
||
| 143 | private function parse(string $value): void |
||
| 144 | { |
||
| 145 | // Check for castling: |
||
| 146 | if (preg_match('/^(O-O|O-O-O)(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
| 147 | $this->castling = $matches[1]; |
||
| 148 | $this->check = $matches[2] === '+'; |
||
| 149 | $this->checkmate = $matches[2] === '#'; |
||
| 150 | $this->annotation = isset($matches[3]) ? $matches[3] : null; |
||
| 151 | return; |
||
| 152 | } |
||
| 153 | |||
| 154 | // Pawn movement: |
||
| 155 | if (preg_match('/^([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
| 156 | $this->targetColumn = $matches[1]; |
||
| 157 | $this->targetRow = (int)$matches[2]; |
||
| 158 | $this->check = $matches[3] === '+'; |
||
| 159 | $this->checkmate = $matches[3] === '#'; |
||
| 160 | $this->movedPiece = self::PIECE_PAWN; |
||
| 161 | $this->annotation = isset($matches[4]) ? $matches[4] : null; |
||
| 162 | return; |
||
| 163 | } |
||
| 164 | |||
| 165 | // Piece movement: |
||
| 166 | if (preg_match('/^([KQBNR])([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
| 167 | $this->movedPiece = $matches[1]; |
||
| 168 | $this->targetColumn = $matches[2]; |
||
| 169 | $this->targetRow = (int)$matches[3]; |
||
| 170 | $this->check = $matches[4] === '+'; |
||
| 171 | $this->checkmate = $matches[4] === '#'; |
||
| 172 | $this->annotation = isset($matches[5]) ? $matches[5] : null; |
||
| 173 | return; |
||
| 174 | } |
||
| 175 | |||
| 176 | // Piece movement from a specific column: |
||
| 177 | if (preg_match('/^([KQBNR])([a-h])([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
| 178 | $this->movedPiece = $matches[1]; |
||
| 179 | $this->movedPieceDisambiguationColumn = $matches[2]; |
||
| 180 | $this->targetColumn = $matches[3]; |
||
| 181 | $this->targetRow = (int)$matches[4]; |
||
| 182 | $this->check = $matches[5] === '+'; |
||
| 183 | $this->checkmate = $matches[5] === '#'; |
||
| 184 | $this->annotation = isset($matches[6]) ? $matches[6] : null; |
||
| 185 | return; |
||
| 186 | } |
||
| 187 | |||
| 188 | // Piece movement from a specific row: |
||
| 189 | if (preg_match('/^([KQBNR])([0-9])([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
| 190 | $this->movedPiece = $matches[1]; |
||
| 191 | $this->movedPieceDisambiguationRow = (int)$matches[2]; |
||
| 192 | $this->targetColumn = $matches[3]; |
||
| 193 | $this->targetRow = (int)$matches[4]; |
||
| 194 | $this->check = $matches[5] === '+'; |
||
| 195 | $this->checkmate = $matches[5] === '#'; |
||
| 196 | $this->annotation = isset($matches[6]) ? $matches[6] : null; |
||
| 197 | return; |
||
| 198 | } |
||
| 199 | |||
| 200 | // Pawn capture: |
||
| 201 | if (preg_match('/^([a-h])x([a-h])([1-8])(?:=([KQBNR]))?(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
| 202 | $this->targetColumn = $matches[2]; |
||
| 203 | $this->targetRow = (int)$matches[3]; |
||
| 204 | $this->movedPiece = self::PIECE_PAWN; |
||
| 205 | $this->movedPieceDisambiguationColumn = $matches[1]; |
||
| 206 | $this->capture = true; |
||
| 207 | $this->promotedPiece = $matches[4] ?: null; |
||
| 208 | $this->check = $matches[5] === '+'; |
||
| 209 | $this->checkmate = $matches[5] === '#'; |
||
| 210 | $this->annotation = isset($matches[6]) ? $matches[6] : null; |
||
| 211 | return; |
||
| 212 | } |
||
| 213 | |||
| 214 | // Piece capture: |
||
| 215 | if (preg_match('/^([KQBNR])x([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
| 216 | $this->movedPiece = $matches[1]; |
||
| 217 | $this->targetColumn = $matches[2]; |
||
| 218 | $this->targetRow = (int)$matches[3]; |
||
| 219 | $this->check = $matches[4] === '+'; |
||
| 220 | $this->checkmate = $matches[4] === '#'; |
||
| 221 | $this->capture = true; |
||
| 222 | $this->annotation = isset($matches[5]) ? $matches[5] : null; |
||
| 223 | return; |
||
| 224 | } |
||
| 225 | |||
| 226 | // Piece capture from a specific column: |
||
| 227 | if (preg_match('/^([KQBNR])([a-h])x([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
| 228 | $this->movedPiece = $matches[1]; |
||
| 229 | $this->movedPieceDisambiguationColumn = $matches[2]; |
||
| 230 | $this->targetColumn = $matches[3]; |
||
| 231 | $this->targetRow = (int)$matches[4]; |
||
| 232 | $this->check = $matches[5] === '+'; |
||
| 233 | $this->checkmate = $matches[5] === '#'; |
||
| 234 | $this->capture = true; |
||
| 235 | $this->annotation = isset($matches[6]) ? $matches[6] : null; |
||
| 236 | return; |
||
| 237 | } |
||
| 238 | |||
| 239 | // Piece capture from a specific column: |
||
| 240 | if (preg_match('/^([KQBNR])([0-9])x([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
| 241 | $this->movedPiece = $matches[1]; |
||
| 242 | $this->movedPieceDisambiguationRow = (int)$matches[2]; |
||
| 243 | $this->targetColumn = $matches[3]; |
||
| 244 | $this->targetRow = (int)$matches[4]; |
||
| 245 | $this->check = $matches[5] === '+'; |
||
| 246 | $this->checkmate = $matches[5] === '#'; |
||
| 247 | $this->capture = true; |
||
| 248 | $this->annotation = isset($matches[6]) ? $matches[6] : null; |
||
| 249 | return; |
||
| 250 | } |
||
| 251 | |||
| 252 | // Check for pawn promotion: |
||
| 253 | if (preg_match('/^([a-h])([1-8])=?([KQBNR])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) { |
||
| 254 | $this->movedPiece = self::PIECE_PAWN; |
||
| 255 | $this->targetColumn = $matches[1]; |
||
| 256 | $this->targetRow = (int)$matches[2]; |
||
| 257 | $this->promotedPiece = $matches[3]; |
||
| 258 | $this->check = $matches[4] === '+'; |
||
| 259 | $this->checkmate = $matches[4] === '#'; |
||
| 260 | $this->annotation = isset($matches[5]) ? $matches[5] : null; |
||
| 261 | return; |
||
| 262 | } |
||
| 263 | |||
| 264 | throw new InvalidArgumentException(sprintf( |
||
| 265 | 'The value "%s" could not be parsed.', |
||
| 266 | $value |
||
| 267 | )); |
||
| 517 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: