| Conditions | 22 |
| Paths | 62 |
| Total Lines | 61 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 506 |
| 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 |
||
| 389 | protected function writeTransform($value, $type) |
||
| 390 | { |
||
| 391 | if (is_null($value)) { |
||
| 392 | return; |
||
| 393 | } |
||
| 394 | |||
| 395 | if ($value instanceof Raw) { |
||
| 396 | return $value; |
||
| 397 | } |
||
| 398 | |||
| 399 | if (is_array($type)) { |
||
| 400 | list($type, $param) = $type; |
||
| 401 | } elseif (strpos($type, ':')) { |
||
| 402 | list($type, $param) = explode(':', $type, 2); |
||
| 403 | } |
||
| 404 | |||
| 405 | switch ($type) { |
||
| 406 | case 'integer': |
||
|
1 ignored issue
–
show
|
|||
| 407 | $value = (int) $value; |
||
| 408 | break; |
||
| 409 | case 'float': |
||
|
1 ignored issue
–
show
|
|||
| 410 | if (empty($param)) { |
||
|
1 ignored issue
–
show
|
|||
| 411 | $value = (float) $value; |
||
| 412 | } else { |
||
|
1 ignored issue
–
show
|
|||
| 413 | $value = (float) number_format($value, $param, '.', ''); |
||
| 414 | } |
||
|
1 ignored issue
–
show
|
|||
| 415 | break; |
||
| 416 | case 'boolean': |
||
|
1 ignored issue
–
show
|
|||
| 417 | $value = (bool) $value; |
||
| 418 | break; |
||
| 419 | case 'timestamp': |
||
|
1 ignored issue
–
show
|
|||
| 420 | if (!is_numeric($value)) { |
||
|
1 ignored issue
–
show
|
|||
| 421 | $value = strtotime($value); |
||
| 422 | } |
||
|
1 ignored issue
–
show
|
|||
| 423 | break; |
||
| 424 | case 'datetime': |
||
|
1 ignored issue
–
show
|
|||
| 425 | $value = is_numeric($value) ? $value : strtotime($value); |
||
| 426 | $value = $this->formatDateTime('Y-m-d H:i:s.u', $value); |
||
| 427 | break; |
||
| 428 | case 'object': |
||
|
1 ignored issue
–
show
|
|||
| 429 | if (is_object($value)) { |
||
|
1 ignored issue
–
show
|
|||
| 430 | $value = json_encode($value, JSON_FORCE_OBJECT); |
||
| 431 | } |
||
|
1 ignored issue
–
show
|
|||
| 432 | break; |
||
| 433 | case 'array': |
||
|
1 ignored issue
–
show
|
|||
| 434 | $value = (array) $value; |
||
| 435 | case 'json': |
||
|
1 ignored issue
–
show
|
|||
| 436 | $option = !empty($param) ? (int) $param : JSON_UNESCAPED_UNICODE; |
||
| 437 | $value = json_encode($value, $option); |
||
| 438 | break; |
||
| 439 | case 'serialize': |
||
|
1 ignored issue
–
show
|
|||
| 440 | $value = serialize($value); |
||
| 441 | break; |
||
| 442 | default: |
||
|
1 ignored issue
–
show
|
|||
| 443 | if (is_object($value) && false !== strpos($type, '\\') && method_exists($value, '__toString')) { |
||
|
1 ignored issue
–
show
|
|||
| 444 | // 对象类型 |
||
| 445 | $value = $value->__toString(); |
||
| 446 | } |
||
|
1 ignored issue
–
show
|
|||
| 447 | } |
||
| 448 | |||
| 449 | return $value; |
||
| 450 | } |
||
| 652 |