Conditions | 72 |
Paths | 199 |
Total Lines | 174 |
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 |
||
127 | private function parseDateFormat( $format ) { |
||
128 | $length = strlen( $format ); |
||
129 | |||
130 | $number = $this->getNumberPattern(); |
||
131 | $notFollowedByNumber = '(?!' . $number . ')'; |
||
132 | $optionalPunctuation = '\p{P}*'; |
||
133 | $optionalWhitespace = '\p{Z}*'; |
||
134 | $separation = $notFollowedByNumber . $optionalWhitespace; |
||
135 | $pattern = '<^' . $optionalWhitespace; |
||
136 | |||
137 | for ( $p = 0; $p < $length; $p++ ) { |
||
138 | $code = $format[$p]; |
||
139 | |||
140 | // "x" is used as a prefix for MediaWiki specific, 2- and 3-letter codes. |
||
141 | if ( $code === 'x' && $p < $length - 1 ) { |
||
142 | $code .= $format[++$p]; |
||
143 | |||
144 | if ( preg_match( '<^x[ijkmot]$>', $code ) && $p < $length - 1 ) { |
||
145 | $code .= $format[++$p]; |
||
146 | } |
||
147 | } |
||
148 | |||
149 | switch ( $code ) { |
||
150 | // Year |
||
151 | case 'o': |
||
152 | case 'Y': |
||
153 | $pattern .= '(?P<year>' . $number . '+)' . $separation; |
||
154 | break; |
||
155 | |||
156 | // Month |
||
157 | case 'F': |
||
158 | case 'M': |
||
159 | case 'm': |
||
160 | case 'n': |
||
161 | case 'xg': |
||
162 | $pattern .= '(?P<month>' . $number . '{1,2}' . $notFollowedByNumber |
||
163 | . $this->getMonthNamesPattern() . ')' . $optionalPunctuation |
||
164 | . $optionalWhitespace; |
||
165 | break; |
||
166 | |||
167 | // Day |
||
168 | case 'd': |
||
169 | case 'j': |
||
170 | $pattern .= '(?P<day>' . $number . '{1,2})' . $optionalPunctuation |
||
171 | . $separation; |
||
172 | break; |
||
173 | |||
174 | // Hour |
||
175 | case 'G': |
||
176 | case 'H': |
||
177 | $pattern .= '(?P<hour>' . $number . '{1,2})' . $separation; |
||
178 | break; |
||
179 | |||
180 | // Minute |
||
181 | case 'i': |
||
182 | $pattern .= '(?P<minute>' . $number . '{1,2})' . $separation; |
||
183 | break; |
||
184 | |||
185 | // Second |
||
186 | case 's': |
||
187 | $pattern .= '(?P<second>' . $number . '{1,2})' . $separation; |
||
188 | break; |
||
189 | |||
190 | // Escaped "x" |
||
191 | case 'xx': |
||
192 | $pattern .= 'x'; |
||
193 | break; |
||
194 | |||
195 | // Escaped character or backslash at the end of the sequence |
||
196 | case '\\': |
||
197 | $pattern .= preg_quote( $p < $length - 1 ? $format[++$p] : '\\' ); |
||
198 | break; |
||
199 | |||
200 | // Quoted sequence |
||
201 | case '"': |
||
202 | $endQuote = strpos( $format, '"', $p + 1 ); |
||
203 | if ( $endQuote !== false ) { |
||
204 | $pattern .= preg_quote( substr( $format, $p + 1, $endQuote - $p - 1 ) ); |
||
205 | $p = $endQuote; |
||
206 | } else { |
||
207 | $pattern .= '"'; |
||
208 | } |
||
209 | break; |
||
210 | |||
211 | // We can ignore "raw" and "raw toggle" when parsing, because we always accept |
||
212 | // canonical digits. |
||
213 | case 'xN': |
||
214 | case 'xn': |
||
215 | break; |
||
216 | |||
217 | // 12-hour format |
||
218 | case 'A': |
||
219 | case 'a': |
||
220 | case 'g': |
||
221 | case 'h': |
||
222 | |||
223 | // Full, formatted dates |
||
224 | case 'c': |
||
225 | case 'r': |
||
226 | case 'U': |
||
227 | |||
228 | // Day of the week |
||
229 | case 'D': |
||
230 | case 'l': |
||
231 | case 'N': |
||
232 | case 'w': |
||
233 | |||
234 | // Timezone |
||
235 | case 'e': |
||
236 | case 'O': |
||
237 | case 'P': |
||
238 | case 'T': |
||
239 | case 'Z': |
||
240 | |||
241 | // Daylight saving time ("1" if true) |
||
242 | case 'I': |
||
243 | |||
244 | // Leap year ("1" if true) |
||
245 | case 'L': |
||
246 | |||
247 | // Number of days in the current month |
||
248 | case 't': |
||
249 | case 'xit': |
||
250 | case 'xjt': |
||
251 | |||
252 | // Week number |
||
253 | case 'W': |
||
254 | |||
255 | // "Hebrew" and "Roman" modifiers |
||
256 | case 'xh': |
||
257 | case 'xr': |
||
258 | |||
259 | // 2-digit year |
||
260 | case 'y': |
||
261 | case 'xiy': |
||
262 | |||
263 | // Day of the year |
||
264 | case 'z': |
||
265 | case 'xiz': |
||
266 | |||
267 | // Day, month and year in incompatible calendar models (Hebrew, Iranian, and others) |
||
268 | case 'xiF': |
||
269 | case 'xij': |
||
270 | case 'xin': |
||
271 | case 'xiY': |
||
272 | case 'xjF': |
||
273 | case 'xjj': |
||
274 | case 'xjn': |
||
275 | case 'xjx': |
||
276 | case 'xjY': |
||
277 | case 'xkY': |
||
278 | case 'xmF': |
||
279 | case 'xmj': |
||
280 | case 'xmn': |
||
281 | case 'xmY': |
||
282 | case 'xoY': |
||
283 | case 'xtY': |
||
284 | throw new ParseException( 'Unsupported date format "' . $code . '"' ); |
||
285 | break; |
||
|
|||
286 | |||
287 | // Character with no meaning |
||
288 | default: |
||
289 | if ( preg_match( '<^' . $optionalPunctuation . '$>u', $format[$p] ) ) { |
||
290 | $pattern .= $optionalPunctuation; |
||
291 | } elseif ( preg_match( '<^' . $optionalWhitespace . '$>u', $format[$p] ) ) { |
||
292 | $pattern .= $optionalWhitespace; |
||
293 | } else { |
||
294 | $pattern .= preg_quote( $format[$p] ); |
||
295 | } |
||
296 | } |
||
297 | } |
||
298 | |||
299 | return $pattern . '$>iu'; |
||
300 | } |
||
301 | // @codingStandardsIgnoreEnd |
||
422 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.