Conditions | 30 |
Paths | 536 |
Total Lines | 169 |
Code Lines | 93 |
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 |
||
92 | public static function parseValue($value) |
||
93 | { |
||
94 | $value = (string)$value; |
||
95 | |||
96 | if (in_array($value, array('', 'nil'), true)) { |
||
97 | return null; |
||
98 | } elseif (in_array($value, array('true', 'false', 'yes', 'no'), true)) { |
||
99 | return $value === 'true' || $value === 'yes'; |
||
100 | } elseif ($value === (string)($num = (int)$value) |
||
101 | || $value === (string)($num = (double)$value) |
||
102 | ) { |
||
103 | return $num; |
||
104 | } elseif (preg_match( |
||
105 | '/^ |
||
106 | (?:(\d+)w)? |
||
107 | (?:(\d+)d)? |
||
108 | (?:(\d+)(?:\:|h))? |
||
109 | (?| |
||
110 | (\d+)\: |
||
111 | (\d*(?:\.\d{1,9})?) |
||
112 | | |
||
113 | (?:(\d+)m)? |
||
114 | (?:(\d+|\d*\.\d{1,9})s)? |
||
115 | (?:((?5))ms)? |
||
116 | (?:((?5))us)? |
||
117 | (?:((?5))ns)? |
||
118 | ) |
||
119 | $/x', |
||
120 | $value, |
||
121 | $time |
||
122 | )) { |
||
123 | $days = isset($time[2]) ? (int)$time[2] : 0; |
||
124 | if (isset($time[1])) { |
||
125 | $days += 7 * (int)$time[1]; |
||
126 | } |
||
127 | if (empty($time[3])) { |
||
128 | $time[3] = 0; |
||
129 | } |
||
130 | if (empty($time[4])) { |
||
131 | $time[4] = 0; |
||
132 | } |
||
133 | if (empty($time[5])) { |
||
134 | $time[5] = 0; |
||
135 | } |
||
136 | |||
137 | $subsecondTime = 0.0; |
||
138 | //@codeCoverageIgnoreStart |
||
139 | // No PHP version currently supports sub-second DateIntervals, |
||
140 | // meaning this section is untestable, since no version constraints |
||
141 | // can be specified for test inputs. |
||
142 | // All inputs currently use integer seconds only, making this |
||
143 | // section unreachable during tests. |
||
144 | // Nevertheless, this section exists right now, in order to provide |
||
145 | // such support as soon as PHP has it. |
||
146 | if (!empty($time[6])) { |
||
147 | $subsecondTime += ((double)$time[6]) / 1000; |
||
148 | } |
||
149 | if (!empty($time[7])) { |
||
150 | $subsecondTime += ((double)$time[7]) / 1000000; |
||
151 | } |
||
152 | if (!empty($time[8])) { |
||
153 | $subsecondTime += ((double)$time[8]) / 1000000000; |
||
154 | } |
||
155 | //@codeCoverageIgnoreEnd |
||
156 | |||
157 | $secondsSpec = $time[5] + $subsecondTime; |
||
158 | try { |
||
159 | return new DateInterval( |
||
160 | "P{$days}DT{$time[3]}H{$time[4]}M{$secondsSpec}S" |
||
161 | ); |
||
162 | //@codeCoverageIgnoreStart |
||
163 | // See previous ignored section's note. |
||
164 | // |
||
165 | // This section is added for backwards compatibility with current |
||
166 | // PHP versions, when in the future sub-second support is added. |
||
167 | // In that event, the test inputs for older versions will be |
||
168 | // expected to get a rounded up result of the sub-second data. |
||
169 | } catch (E $e) { |
||
170 | $secondsSpec = (int)round($secondsSpec); |
||
171 | return new DateInterval( |
||
172 | "P{$days}DT{$time[3]}H{$time[4]}M{$secondsSpec}S" |
||
173 | ); |
||
174 | } |
||
175 | //@codeCoverageIgnoreEnd |
||
176 | } elseif (preg_match( |
||
177 | '#^ |
||
178 | (?<mon>jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec) |
||
179 | / |
||
180 | (?<day>\d\d?) |
||
181 | / |
||
182 | (?<year>\d{4}) |
||
183 | (?: |
||
184 | \s+(?<time>\d{2}\:\d{2}:\d{2}) |
||
185 | )? |
||
186 | $#uix', |
||
187 | $value, |
||
188 | $date |
||
189 | )) { |
||
190 | if (!isset($date['time'])) { |
||
191 | $date['time'] = '00:00:00'; |
||
192 | } |
||
193 | try { |
||
194 | return new DateTime( |
||
195 | $date['year'] . |
||
196 | '-' . ucfirst($date['mon']) . |
||
197 | "-{$date['day']} {$date['time']}", |
||
198 | new DateTimeZone('UTC') |
||
199 | ); |
||
200 | } catch (E $e) { |
||
201 | return $value; |
||
202 | } |
||
203 | } elseif (('"' === $value[0]) && substr(strrev($value), 0, 1) === '"') { |
||
204 | return str_replace( |
||
205 | array('\"', '\\\\', "\\\n", "\\\r\n", "\\\r"), |
||
206 | array('"', '\\'), |
||
207 | substr($value, 1, -1) |
||
208 | ); |
||
209 | } elseif ('{' === $value[0]) { |
||
210 | $len = strlen($value); |
||
211 | if ($value[$len - 1] === '}') { |
||
212 | $value = substr($value, 1, -1); |
||
213 | if ('' === $value) { |
||
214 | return array(); |
||
215 | } |
||
216 | $parsedValue = preg_split( |
||
217 | '/ |
||
218 | (\"(?:\\\\\\\\|\\\\"|[^"])*\") |
||
219 | | |
||
220 | (\{[^{}]*(?2)?\}) |
||
221 | | |
||
222 | ([^;=]+) |
||
223 | /sx', |
||
224 | $value, |
||
225 | null, |
||
226 | PREG_SPLIT_DELIM_CAPTURE |
||
227 | ); |
||
228 | $result = array(); |
||
229 | $newVal = null; |
||
230 | $newKey = null; |
||
231 | for ($i = 0, $l = count($parsedValue); $i < $l; ++$i) { |
||
232 | switch ($parsedValue[$i]) { |
||
233 | case '': |
||
234 | break; |
||
235 | case ';': |
||
236 | if (null === $newKey) { |
||
237 | $result[] = $newVal; |
||
238 | } else { |
||
239 | $result[$newKey] = $newVal; |
||
240 | } |
||
241 | $newKey = $newVal = null; |
||
242 | break; |
||
243 | case '=': |
||
244 | $newKey = static::parseValue($parsedValue[$i - 1]); |
||
245 | $newVal = static::parseValue($parsedValue[++$i]); |
||
246 | break; |
||
247 | default: |
||
248 | $newVal = static::parseValue($parsedValue[$i]); |
||
249 | } |
||
250 | } |
||
251 | if (null === $newKey) { |
||
252 | $result[] = $newVal; |
||
253 | } else { |
||
254 | $result[$newKey] = $newVal; |
||
255 | } |
||
256 | return $result; |
||
257 | } |
||
258 | } |
||
259 | return $value; |
||
260 | } |
||
261 | |||
474 |