| Conditions | 13 |
| Paths | 12 |
| Total Lines | 50 |
| Code Lines | 41 |
| 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 |
||
| 81 | public function jsonToArray($file) |
||
| 82 | { |
||
| 83 | $json = file_get_contents($file); |
||
| 84 | |||
| 85 | $conf = json_decode($json, true); |
||
| 86 | |||
| 87 | $jsonError = json_last_error(); |
||
| 88 | if (!is_array($conf) && $jsonError !== JSON_ERROR_NONE) { |
||
| 89 | |||
| 90 | switch ($jsonError) { |
||
| 91 | case JSON_ERROR_DEPTH: |
||
| 92 | $jsonErrorText = 'The maximum stack depth has been exceeded'; |
||
| 93 | break; |
||
| 94 | case JSON_ERROR_STATE_MISMATCH: |
||
| 95 | $jsonErrorText = 'Invalid or malformed JSON'; |
||
| 96 | break; |
||
| 97 | case JSON_ERROR_CTRL_CHAR: |
||
| 98 | $jsonErrorText = 'Control character error, possibly incorrectly encoded'; |
||
| 99 | break; |
||
| 100 | case JSON_ERROR_SYNTAX: |
||
| 101 | $jsonErrorText = 'Syntax error'; |
||
| 102 | break; |
||
| 103 | case JSON_ERROR_UTF8: |
||
| 104 | $jsonErrorText = 'Malformed UTF-8 characters, possibly incorrectly encoded'; |
||
| 105 | break; |
||
| 106 | case JSON_ERROR_RECURSION: |
||
| 107 | $jsonErrorText = 'One or more recursive references in the value to be encoded'; |
||
| 108 | break; |
||
| 109 | case JSON_ERROR_INF_OR_NAN: |
||
| 110 | $jsonErrorText = 'One or more NAN or INF values in the value to be encoded'; |
||
| 111 | break; |
||
| 112 | case JSON_ERROR_UNSUPPORTED_TYPE: |
||
| 113 | $jsonErrorText = 'A value of a type that cannot be encoded was given'; |
||
| 114 | break; |
||
| 115 | case JSON_ERROR_INVALID_PROPERTY_NAME: |
||
| 116 | $jsonErrorText = 'A property name that cannot be encoded was given'; |
||
| 117 | break; |
||
| 118 | case JSON_ERROR_UTF16: |
||
| 119 | $jsonErrorText = 'Malformed UTF-16 characters, possibly incorrectly encoded'; |
||
| 120 | break; |
||
| 121 | default: |
||
| 122 | $jsonErrorText = 'Unknown Error Code'; |
||
| 123 | } |
||
| 124 | |||
| 125 | trigger_error('JSON decoding error "' . $jsonErrorText . '" for file ' . $file, E_USER_WARNING); |
||
| 126 | return []; |
||
| 127 | } |
||
| 128 | |||
| 129 | return $conf; |
||
| 130 | } |
||
| 131 | } |
||
| 135 |