| Conditions | 15 |
| Paths | 24 |
| Total Lines | 60 |
| Code Lines | 33 |
| 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 |
||
| 101 | protected function convertArray(array $data = null): ?array |
||
| 102 | { |
||
| 103 | if ($data === null) { |
||
| 104 | return null; |
||
| 105 | } |
||
| 106 | |||
| 107 | if (!is_array($data) && !is_object($data)) { |
||
| 108 | throw new \InvalidArgumentException( |
||
| 109 | sprintf('Invalid payload data. Must be null, array or class with mapping ("%s" given).', gettype($data))); |
||
| 110 | } |
||
| 111 | |||
| 112 | if (is_object($data)) { |
||
| 113 | $cfg = $this->getClassMappingConfigOrThrow($data); |
||
| 114 | |||
| 115 | return [$cfg[ ResponseBuilder::KEY_KEY ] => $data->{$cfg[ ResponseBuilder::KEY_METHOD ]}()]; |
||
| 116 | } |
||
| 117 | |||
| 118 | // This is to ensure that we either have array with user provided keys i.e. ['foo'=>'bar'], which will then |
||
| 119 | // be turned into JSON object or array without user specified keys (['bar']) which we would return as JSON |
||
| 120 | // array. But you can't mix these two as the final JSON would not produce predictable results. |
||
| 121 | $user_keys_cnt = 0; |
||
| 122 | $builtin_keys_cnt = 0; |
||
| 123 | foreach ($data as $key => $val) { |
||
| 124 | if (is_int($key)) { |
||
| 125 | $builtin_keys_cnt++; |
||
| 126 | } elseif (is_string($key)) { |
||
| 127 | $user_keys_cnt++; |
||
| 128 | } else { |
||
| 129 | throw new \RuntimeException('Invalid data array. Array keys must either use strings as keys, or not use user provide keys.'); |
||
| 130 | } |
||
| 131 | |||
| 132 | if (($user_keys_cnt > 0) && ($builtin_keys_cnt > 0)) { |
||
| 133 | throw new \RuntimeException( |
||
| 134 | 'Invalid data array. Either set own keys for all the items or do not specify any keys at all. ' . |
||
| 135 | 'Arrays with mixed keys are not supported by design.'); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | foreach ($data as $key => $val) { |
||
| 140 | if (is_array($val)) { |
||
| 141 | foreach ($val as $val_key => $val_val) { |
||
| 142 | // if (is_object($val_val) && (!is_string($val_key))) { |
||
| 143 | // throw new \InvalidArgumentException( |
||
| 144 | // sprintf('Invalid payload data. Must be null, array or object ("%s" given).', gettype($data))); |
||
| 145 | // } |
||
| 146 | } |
||
| 147 | $data[ $key ] = $this->convertArray($val); |
||
| 148 | } elseif (is_object($val)) { |
||
| 149 | $cls = get_class($val); |
||
| 150 | $cfg = $this->getClassMappingConfigOrThrow($val); |
||
| 151 | if (array_key_exists($cls, $this->classes)) { |
||
| 152 | $conversion_method = $this->classes[ $cls ][ ResponseBuilder::KEY_METHOD ]; |
||
| 153 | $converted_data = $val->$conversion_method(); |
||
| 154 | // $data = [$this->classes[ $cls ][ ResponseBuilder::KEY_KEY ] => $converted_data]; |
||
| 155 | $data[ $key ] = $converted_data; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | return $data; |
||
| 161 | } |
||
| 163 |