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