Conditions | 12 |
Paths | 11 |
Total Lines | 114 |
Code Lines | 82 |
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); |
||
130 | private function parse(string $value): void |
||
131 | { |
||
132 | // Check for castling: |
||
133 | if (preg_match('/^(O-O|O-O-O)(\+|\#?)$/', $value, $matches)) { |
||
134 | $this->castling = $matches[1]; |
||
135 | $this->check = $matches[2] === '+'; |
||
136 | $this->checkmate = $matches[2] === '#'; |
||
137 | return; |
||
138 | } |
||
139 | |||
140 | // Pawn movement: |
||
141 | if (preg_match('/^([a-h])([1-8])(\+|\#?)$/', $value, $matches)) { |
||
142 | $this->targetColumn = $matches[1]; |
||
143 | $this->targetRow = (int)$matches[2]; |
||
144 | $this->check = $matches[3] === '+'; |
||
145 | $this->checkmate = $matches[3] === '#'; |
||
146 | $this->movedPiece = self::PIECE_PAWN; |
||
147 | return; |
||
148 | } |
||
149 | |||
150 | // Piece movement: |
||
151 | if (preg_match('/^([KQBNR])([a-h])([1-8])(\+|\#?)$/', $value, $matches)) { |
||
152 | $this->movedPiece = $matches[1]; |
||
153 | $this->targetColumn = $matches[2]; |
||
154 | $this->targetRow = (int)$matches[3]; |
||
155 | $this->check = $matches[4] === '+'; |
||
156 | $this->checkmate = $matches[4] === '#'; |
||
157 | return; |
||
158 | } |
||
159 | |||
160 | // Piece movement from a specific column: |
||
161 | if (preg_match('/^([KQBNR])([a-h])([a-h])([1-8])(\+|\#?)$/', $value, $matches)) { |
||
162 | $this->movedPiece = $matches[1]; |
||
163 | $this->movedPieceDisambiguationColumn = $matches[2]; |
||
164 | $this->targetColumn = $matches[3]; |
||
165 | $this->targetRow = (int)$matches[4]; |
||
166 | $this->check = $matches[5] === '+'; |
||
167 | $this->checkmate = $matches[5] === '#'; |
||
168 | return; |
||
169 | } |
||
170 | |||
171 | // Piece movement from a specific row: |
||
172 | if (preg_match('/^([KQBNR])([0-9])([a-h])([1-8])(\+|\#?)$/', $value, $matches)) { |
||
173 | $this->movedPiece = $matches[1]; |
||
174 | $this->movedPieceDisambiguationRow = (int)$matches[2]; |
||
175 | $this->targetColumn = $matches[3]; |
||
176 | $this->targetRow = (int)$matches[4]; |
||
177 | $this->check = $matches[5] === '+'; |
||
178 | $this->checkmate = $matches[5] === '#'; |
||
179 | return; |
||
180 | } |
||
181 | |||
182 | // Pawn capture: |
||
183 | if (preg_match('/^([a-h])x([a-h])([1-8])(?:=([KQBNR]))?(\+|\#?)$/', $value, $matches)) { |
||
184 | $this->targetColumn = $matches[2]; |
||
185 | $this->targetRow = (int)$matches[3]; |
||
186 | $this->movedPiece = self::PIECE_PAWN; |
||
187 | $this->movedPieceDisambiguationColumn = $matches[1]; |
||
188 | $this->capture = true; |
||
189 | $this->promotedPiece = $matches[4] ?: null; |
||
190 | $this->check = $matches[5] === '+'; |
||
191 | $this->checkmate = $matches[5] === '#'; |
||
192 | return; |
||
193 | } |
||
194 | |||
195 | // Piece capture: |
||
196 | if (preg_match('/^([KQBNR])x([a-h])([1-8])(\+|\#?)$/', $value, $matches)) { |
||
197 | $this->movedPiece = $matches[1]; |
||
198 | $this->targetColumn = $matches[2]; |
||
199 | $this->targetRow = (int)$matches[3]; |
||
200 | $this->check = $matches[4] === '+'; |
||
201 | $this->checkmate = $matches[4] === '#'; |
||
202 | $this->capture = true; |
||
203 | return; |
||
204 | } |
||
205 | |||
206 | // Piece capture from a specific column: |
||
207 | if (preg_match('/^([KQBNR])([a-h])x([a-h])([1-8])(\+|\#?)$/', $value, $matches)) { |
||
208 | $this->movedPiece = $matches[1]; |
||
209 | $this->movedPieceDisambiguationColumn = $matches[2]; |
||
210 | $this->targetColumn = $matches[3]; |
||
211 | $this->targetRow = (int)$matches[4]; |
||
212 | $this->check = $matches[5] === '+'; |
||
213 | $this->checkmate = $matches[5] === '#'; |
||
214 | $this->capture = true; |
||
215 | return; |
||
216 | } |
||
217 | |||
218 | // Piece capture from a specific column: |
||
219 | if (preg_match('/^([KQBNR])([0-9])x([a-h])([1-8])(\+|\#?)$/', $value, $matches)) { |
||
220 | $this->movedPiece = $matches[1]; |
||
221 | $this->movedPieceDisambiguationRow = (int)$matches[2]; |
||
222 | $this->targetColumn = $matches[3]; |
||
223 | $this->targetRow = (int)$matches[4]; |
||
224 | $this->check = $matches[5] === '+'; |
||
225 | $this->checkmate = $matches[5] === '#'; |
||
226 | $this->capture = true; |
||
227 | return; |
||
228 | } |
||
229 | |||
230 | // Check for pawn promotion: |
||
231 | if (preg_match('/^([a-h])([1-8])=([KQBNR])(\+|\#?)$/', $value, $matches)) { |
||
232 | $this->movedPiece = self::PIECE_PAWN; |
||
233 | $this->targetColumn = $matches[1]; |
||
234 | $this->targetRow = (int)$matches[2]; |
||
235 | $this->promotedPiece = $matches[3]; |
||
236 | $this->check = $matches[4] === '+'; |
||
237 | $this->checkmate = $matches[4] === '#'; |
||
238 | return; |
||
239 | } |
||
240 | |||
241 | throw new InvalidArgumentException(sprintf( |
||
242 | 'The value "%s" could not be parsed.', |
||
243 | $value |
||
244 | )); |
||
484 |
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: