Conditions | 16 |
Paths | 95 |
Total Lines | 61 |
Code Lines | 45 |
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 |
||
29 | public static function fromString(string $dateStyleStr): DateStyle |
||
30 | { |
||
31 | $parts = preg_split('~\W+~', $dateStyleStr, 2); |
||
32 | $fmt = $parts[0]; |
||
33 | $ord = ($parts[1] ?? null); |
||
34 | |||
35 | switch (strtoupper($fmt)) { |
||
36 | case strtoupper(self::FORMAT_ISO): |
||
37 | $format = self::FORMAT_ISO; |
||
38 | $order = self::ORDER_YMD; |
||
39 | break; |
||
40 | |||
41 | case strtoupper(self::FORMAT_POSTGRES): |
||
42 | $format = self::FORMAT_POSTGRES; |
||
43 | $orders = [self::ORDER_MDY, self::ORDER_DMY]; |
||
44 | break; |
||
45 | |||
46 | case strtoupper(self::FORMAT_SQL): |
||
47 | $format = self::FORMAT_SQL; |
||
48 | $orders = [self::ORDER_MDY, self::ORDER_DMY]; |
||
49 | break; |
||
50 | |||
51 | case strtoupper(self::FORMAT_GERMAN): |
||
52 | $format = self::FORMAT_GERMAN; |
||
53 | $order = self::ORDER_DMY; |
||
54 | break; |
||
55 | |||
56 | default: |
||
57 | trigger_error("Unrecognized DateStyle output format specification: $fmt", E_USER_NOTICE); |
||
58 | $format = $fmt; |
||
59 | } |
||
60 | |||
61 | if (!isset($order)) { |
||
62 | switch (strtoupper($ord)) { |
||
|
|||
63 | case strtoupper(self::ORDER_DMY): |
||
64 | case strtoupper('Euro'): |
||
65 | case strtoupper('European'): |
||
66 | $order = self::ORDER_DMY; |
||
67 | break; |
||
68 | |||
69 | case strtoupper(self::ORDER_MDY): |
||
70 | case strtoupper('US'): |
||
71 | case strtoupper('NonEuro'): |
||
72 | case strtoupper('NonEuropean'): |
||
73 | $order = self::ORDER_MDY; |
||
74 | break; |
||
75 | |||
76 | case strtoupper(self::ORDER_YMD): |
||
77 | $order = self::ORDER_YMD; |
||
78 | break; |
||
79 | |||
80 | default: |
||
81 | trigger_error("Unrecognized DateStyle input/output year/month/day ordering: $ord", E_USER_NOTICE); |
||
82 | $order = $ord; |
||
83 | } |
||
84 | if (isset($orders) && !in_array($order, $orders)) { |
||
85 | $order = $orders[0]; // irrelevant order, set the default one according to $format |
||
86 | } |
||
87 | } |
||
88 | |||
89 | return new DateStyle($format, $order); |
||
90 | } |
||
116 |