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