| Conditions | 10 |
| Paths | 10 |
| Total Lines | 32 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 23 |
| CRAP Score | 10.0512 |
| 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 |
||
| 19 | 2 | public function getSerializedValue($scopeValue, $escape = False) |
|
| 20 | { |
||
| 21 | 2 | $valueType = gettype($scopeValue); |
|
| 22 | |||
| 23 | switch ($valueType) { |
||
| 24 | 2 | case 'string': |
|
|
|
|||
| 25 | 1 | if ($escape) { |
|
| 26 | $result = json_encode($scopeValue); |
||
| 27 | return substr($result, 1, -1); // eliminate quotes |
||
| 28 | } else { |
||
| 29 | 1 | return '' . $scopeValue; |
|
| 30 | } |
||
| 31 | 2 | case 'boolean': |
|
| 32 | 1 | return ($scopeValue ? 'true' : 'false'); |
|
| 33 | 2 | case 'integer': |
|
| 34 | 1 | return '' . $scopeValue; |
|
| 35 | 2 | case 'double': |
|
| 36 | 1 | return sprintf('%.5f', $scopeValue); |
|
| 37 | 2 | case 'array': |
|
| 38 | 2 | case 'object': |
|
| 39 | 1 | return json_encode($scopeValue); |
|
| 40 | 2 | case 'NULL': |
|
| 41 | 1 | return 'null'; |
|
| 42 | 1 | default: |
|
| 43 | 1 | throw new ScopeTokenValueSerializationException( |
|
| 44 | 1 | sprintf( |
|
| 45 | 1 | 'Unable to serialize value of type "%s".', |
|
| 46 | $valueType |
||
| 47 | 1 | ) |
|
| 48 | 1 | ); |
|
| 49 | 1 | } |
|
| 50 | } |
||
| 51 | } |