Conditions | 11 |
Paths | 6 |
Total Lines | 28 |
Code Lines | 18 |
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 |
||
71 | public function setDateValue($value): ?string |
||
72 | { |
||
73 | if (is_numeric($value)) { |
||
74 | $value = date('Y-m-d', $value); |
||
|
|||
75 | } elseif (is_string($value)) { |
||
76 | // check if m/d/y format |
||
77 | if (preg_match('/^(\d{2})\D?(\d{2})\D?(\d{4}).*/', $value)) { |
||
78 | $value = preg_replace('/^(\d{2})\D?(\d{2})\D?(\d{4}).*/', '$3-$1-$2', $value); |
||
79 | } |
||
80 | |||
81 | // trim time and any extra crap, or leave as-is if it doesn't fit the pattern |
||
82 | $value = preg_replace('/^(\d{4})\D?(\d{2})\D?(\d{2}).*/', '$1-$2-$3', $value); |
||
83 | } elseif (is_array($value) && count(array_filter($value))) { |
||
84 | // collapse array date to string |
||
85 | $value = sprintf( |
||
86 | '%04u-%02u-%02u', |
||
87 | is_numeric($value['yyyy']) ? $value['yyyy'] : 0, |
||
88 | is_numeric($value['mm']) ? $value['mm'] : 0, |
||
89 | is_numeric($value['dd']) ? $value['dd'] : 0 |
||
90 | ); |
||
91 | } else { |
||
92 | if ($value = strtotime($value)) { |
||
93 | $value = date('Y-m-d', $value) ?: null; |
||
94 | } else { |
||
95 | $value = null; |
||
96 | } |
||
97 | } |
||
98 | return $value; |
||
99 | } |
||
120 |