| Conditions | 17 |
| Paths | 16 |
| Total Lines | 85 |
| Code Lines | 49 |
| 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 |
||
| 72 | * @return array|null |
||
| 73 | * @throws \LogicException |
||
| 74 | * @throws \Tebru\Gson\Exception\JsonSyntaxException If trying to read from non object/array |
||
| 75 | */ |
||
| 76 | 14 | public function read(JsonReadable $reader): ?array |
|
| 77 | { |
||
| 78 | 14 | $token = $reader->peek(); |
|
| 79 | 14 | if ($token === JsonToken::NULL) { |
|
| 80 | 1 | $reader->nextNull(); |
|
| 81 | 1 | return null; |
|
| 82 | } |
||
| 83 | |||
| 84 | 13 | $array = []; |
|
| 85 | |||
| 86 | 13 | if ($this->numberOfGenerics > 2) { |
|
| 87 | 1 | throw new LogicException(\sprintf('Array may not have more than 2 generic types at "%s"', $reader->getPath())); |
|
| 88 | } |
||
| 89 | |||
| 90 | switch ($token) { |
||
| 91 | 12 | case JsonToken::BEGIN_OBJECT: |
|
| 92 | 7 | $reader->beginObject(); |
|
| 93 | |||
| 94 | 7 | while ($reader->hasNext()) { |
|
| 95 | 7 | $name = $reader->nextName(); |
|
| 96 | |||
| 97 | 7 | switch ($this->numberOfGenerics) { |
|
| 98 | // no generics specified |
||
| 99 | 7 | case 0: |
|
| 100 | // By now we know that we're deserializing a json object to an array. |
||
| 101 | // If there is a nested object, continue deserializing to an array, |
||
| 102 | // otherwise guess the type using the wildcard |
||
| 103 | 3 | $type = $reader->peek() === JsonToken::BEGIN_OBJECT |
|
| 104 | 1 | ? TypeToken::create(TypeToken::HASH) |
|
| 105 | 3 | : TypeToken::create(TypeToken::WILDCARD); |
|
| 106 | |||
| 107 | 3 | $adapter = $this->typeAdapterProvider->getAdapter($type); |
|
| 108 | 3 | $array[$name] = $adapter->read($reader); |
|
| 109 | 3 | break; |
|
| 110 | // generic for value specified |
||
| 111 | 5 | case 1: |
|
| 112 | 1 | $array[$name] = $this->valueTypeAdapter->read($reader); |
|
| 113 | 1 | break; |
|
| 114 | // generic for key and value specified |
||
| 115 | 4 | case 2: |
|
| 116 | 4 | if (!$this->keyType->isString() && !$this->keyType->isInteger()) { |
|
| 117 | 1 | throw new LogicException(\sprintf('Array keys must be strings or integers at "%s"', $reader->getPath())); |
|
| 118 | } |
||
| 119 | |||
| 120 | 3 | if ($this->keyType->isInteger()) { |
|
| 121 | 2 | if (!\ctype_digit($name)) { |
|
| 122 | 1 | throw new JsonSyntaxException(\sprintf('Expected integer, but found string for key at "%s"', $reader->getPath())); |
|
| 123 | } |
||
| 124 | |||
| 125 | 1 | $name = (int)$name; |
|
| 126 | } |
||
| 127 | |||
| 128 | 2 | $array[$name] = $this->valueTypeAdapter->read($reader); |
|
| 129 | |||
| 130 | 2 | break; |
|
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | 5 | $reader->endObject(); |
|
| 135 | |||
| 136 | 5 | break; |
|
| 137 | 5 | case JsonToken::BEGIN_ARRAY: |
|
| 138 | 4 | $reader->beginArray(); |
|
| 139 | |||
| 140 | 4 | while ($reader->hasNext()) { |
|
| 141 | 4 | switch ($this->numberOfGenerics) { |
|
| 142 | // no generics specified |
||
| 143 | 4 | case 0: |
|
| 144 | 3 | case 1: |
|
| 145 | 3 | $array[] = $this->valueTypeAdapter->read($reader); |
|
| 146 | |||
| 147 | 3 | break; |
|
| 148 | default: |
||
| 149 | 1 | throw new LogicException(\sprintf('An array may only specify a generic type for the value at "%s"', $reader->getPath())); |
|
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | 3 | $reader->endArray(); |
|
| 154 | |||
| 155 | 3 | break; |
|
| 156 | default: |
||
| 157 | 1 | throw new JsonSyntaxException(\sprintf('Could not parse json, expected array or object but found "%s" at "%s"', $token, $reader->getPath())); |
|
| 243 |