| Conditions | 22 |
| Paths | 58 |
| Total Lines | 62 |
| Code Lines | 48 |
| 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 |
||
| 570 | protected function readTransform($value, $type) |
||
| 571 | { |
||
| 572 | if (is_null($value)) { |
||
| 573 | return; |
||
| 574 | } |
||
| 575 | |||
| 576 | if (is_array($type)) { |
||
| 577 | list($type, $param) = $type; |
||
| 578 | } elseif (strpos($type, ':')) { |
||
| 579 | list($type, $param) = explode(':', $type, 2); |
||
| 580 | } |
||
| 581 | |||
| 582 | switch ($type) { |
||
| 583 | case 'integer': |
||
|
1 ignored issue
–
show
|
|||
| 584 | $value = (int) $value; |
||
| 585 | break; |
||
| 586 | case 'float': |
||
|
1 ignored issue
–
show
|
|||
| 587 | if (empty($param)) { |
||
|
1 ignored issue
–
show
|
|||
| 588 | $value = (float) $value; |
||
| 589 | } else { |
||
|
1 ignored issue
–
show
|
|||
| 590 | $value = (float) number_format($value, $param, '.', ''); |
||
| 591 | } |
||
|
1 ignored issue
–
show
|
|||
| 592 | break; |
||
| 593 | case 'boolean': |
||
|
1 ignored issue
–
show
|
|||
| 594 | $value = (bool) $value; |
||
| 595 | break; |
||
| 596 | case 'timestamp': |
||
|
1 ignored issue
–
show
|
|||
| 597 | if (!is_null($value)) { |
||
|
1 ignored issue
–
show
|
|||
| 598 | $format = !empty($param) ? $param : $this->dateFormat; |
||
| 599 | $value = $this->formatDateTime($format, $value, true); |
||
| 600 | } |
||
|
1 ignored issue
–
show
|
|||
| 601 | break; |
||
| 602 | case 'datetime': |
||
|
1 ignored issue
–
show
|
|||
| 603 | if (!is_null($value)) { |
||
|
1 ignored issue
–
show
|
|||
| 604 | $format = !empty($param) ? $param : $this->dateFormat; |
||
| 605 | $value = $this->formatDateTime($format, $value); |
||
| 606 | } |
||
|
1 ignored issue
–
show
|
|||
| 607 | break; |
||
| 608 | case 'json': |
||
|
1 ignored issue
–
show
|
|||
| 609 | $value = json_decode($value, true); |
||
| 610 | break; |
||
| 611 | case 'array': |
||
|
1 ignored issue
–
show
|
|||
| 612 | $value = empty($value) ? [] : json_decode($value, true); |
||
| 613 | break; |
||
| 614 | case 'object': |
||
|
1 ignored issue
–
show
|
|||
| 615 | $value = empty($value) ? new \stdClass() : json_decode($value); |
||
| 616 | break; |
||
| 617 | case 'serialize': |
||
|
1 ignored issue
–
show
|
|||
| 618 | try { |
||
|
1 ignored issue
–
show
|
|||
| 619 | $value = unserialize($value); |
||
| 620 | } catch (\Exception $e) { |
||
|
1 ignored issue
–
show
|
|||
| 621 | $value = null; |
||
| 622 | } |
||
|
1 ignored issue
–
show
|
|||
| 623 | break; |
||
| 624 | default: |
||
|
1 ignored issue
–
show
|
|||
| 625 | if (false !== strpos($type, '\\')) { |
||
|
1 ignored issue
–
show
|
|||
| 626 | // 对象类型 |
||
| 627 | $value = new $type($value); |
||
| 628 | } |
||
|
1 ignored issue
–
show
|
|||
| 629 | } |
||
| 630 | |||
| 631 | return $value; |
||
| 632 | } |
||
| 663 |