Conditions | 10 |
Paths | 7 |
Total Lines | 30 |
Code Lines | 15 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
27 | public function normalize(string $input): string |
||
28 | { |
||
29 | if (preg_match('/^\d{4}[\-][0]{2}[\-][0]{2}$/', $input) || preg_match('/^\d{4}[\-]\d{2}[\-][0]{2}$/', $input)) { |
||
30 | return $input; |
||
31 | } |
||
32 | |||
33 | if (preg_match('/^\d{4}[\-|\/]\d{2}[\-|\/]\d{2}$/', $input) || |
||
34 | preg_match('/^\d{1,2}\.? [a-zA-Z]{3,} \d{4}$/', $input) || |
||
35 | preg_match('/^[a-zA-Z]{3,} \d{1,2}, \d{4}$/', $input) |
||
36 | ) { |
||
37 | return (new \DateTime($input))->format('Y-m-d'); |
||
38 | } |
||
39 | |||
40 | if (preg_match('/^\d{4}[\-|\/|\.]\d{2}[\-|\/|\.]\d{2}$/', $input)) { |
||
41 | return str_replace('.', '-', $input); |
||
42 | } |
||
43 | |||
44 | if (preg_match('/^\d{4}[\-|\/|\.]\d{2}$/', $input)) { |
||
45 | return str_replace('/', '-', str_replace('.', '-', $input)) . '-00'; |
||
46 | } |
||
47 | |||
48 | if (preg_match('/^[A-Z][a-z]* \d{4}$/', $input)) { |
||
49 | return (new \DateTime($input))->format('Y-m') . '-00'; |
||
50 | } |
||
51 | |||
52 | if (preg_match('/^\d{4}$/', $input)) { |
||
53 | return $input . '-00-00'; |
||
54 | } |
||
55 | |||
56 | throw new UnrecognizedDateFormat($input); |
||
57 | } |
||
59 |