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