Conditions | 15 |
Paths | 1 |
Total Lines | 51 |
Code Lines | 38 |
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 |
||
202 | protected function _format(string $message, string $format, string $date_format, string $level, array $context=[]): string |
||
203 | { |
||
204 | $parsed = preg_replace_callback('/(\{(([a-z]\.*)+)\})/', function ($match) use ($message, $level, $date_format, $context) { |
||
205 | $key = ''; |
||
206 | $context = array_merge($this->context, $context); |
||
207 | |||
208 | if ($sub_context = strpos($match[2], '.')) { |
||
209 | $parts = explode('.', $match[2]); |
||
210 | $name = $parts[0]; |
||
211 | $key = $parts[1]; |
||
212 | } else { |
||
213 | $name = $match[2]; |
||
214 | } |
||
215 | |||
216 | switch ($name) { |
||
217 | case 'level': |
||
218 | return $match[0] = $level; |
||
219 | break; |
||
220 | case 'date': |
||
221 | return $match[0] = date($date_format); |
||
222 | break; |
||
223 | case 'message': |
||
224 | $replace = []; |
||
225 | foreach ($context as $key => $val) { |
||
226 | if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) { |
||
227 | $replace['{' . $key . '}'] = $val; |
||
228 | } else { |
||
229 | $replace['{' . $key . '}'] = json_encode($val); |
||
230 | } |
||
231 | } |
||
232 | |||
233 | return $match[0] = strtr($message, $replace); |
||
234 | break; |
||
235 | case 'context': |
||
236 | if ($sub_context) { |
||
237 | if (array_key_exists($key, $context)) { |
||
238 | if (!is_array($context[$key]) && (!is_object($context[$key]) || method_exists($context[$key], '__toString'))) { |
||
239 | return $match[0] = $context[$key]; |
||
240 | } else { |
||
241 | return $match[0] = json_encode($context[$key]); |
||
242 | } |
||
243 | } |
||
244 | } else { |
||
245 | return $match[0] = json_encode($context); |
||
246 | } |
||
247 | break; |
||
248 | } |
||
249 | }, $format); |
||
250 | |||
251 | return $parsed; |
||
252 | } |
||
253 | } |
||
254 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.