| Conditions | 11 |
| Paths | 20 |
| Total Lines | 42 |
| 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 |
||
| 21 | public function jsonValid($value) { |
||
| 22 | $result = json_decode($value); |
||
| 23 | |||
| 24 | switch (json_last_error()) { |
||
| 25 | case JSON_ERROR_NONE: |
||
| 26 | $error = ''; |
||
| 27 | break; |
||
| 28 | case JSON_ERROR_DEPTH: |
||
| 29 | $error = 'The maximum stack depth has been exceeded.'; |
||
| 30 | break; |
||
| 31 | case JSON_ERROR_STATE_MISMATCH: |
||
| 32 | $error = 'Invalid or malformed JSON.'; |
||
| 33 | break; |
||
| 34 | case JSON_ERROR_CTRL_CHAR: |
||
| 35 | $error = 'Control character error, possibly incorrectly encoded.'; |
||
| 36 | break; |
||
| 37 | case JSON_ERROR_SYNTAX: |
||
| 38 | $error = 'Syntax error, malformed JSON.'; |
||
| 39 | break; |
||
| 40 | case JSON_ERROR_UTF8: |
||
| 41 | $error = 'Malformed UTF-8 characters, possibly incorrectly encoded.'; |
||
| 42 | break; |
||
| 43 | case JSON_ERROR_RECURSION: |
||
| 44 | $error = 'One or more recursive references in the value to be encoded.'; |
||
| 45 | break; |
||
| 46 | case JSON_ERROR_INF_OR_NAN: |
||
| 47 | $error = 'One or more NAN or INF values in the value to be encoded.'; |
||
| 48 | break; |
||
| 49 | case JSON_ERROR_UNSUPPORTED_TYPE: |
||
| 50 | $error = 'A value of a type that cannot be encoded was given.'; |
||
| 51 | break; |
||
| 52 | default: |
||
| 53 | $error = 'Unknown JSON error occured.'; |
||
| 54 | break; |
||
| 55 | } |
||
| 56 | |||
| 57 | if ($error !== '') { |
||
| 58 | throw new ParsingException($error); |
||
| 59 | } |
||
| 60 | |||
| 61 | return $result; |
||
| 62 | } |
||
| 63 | |||
| 82 |