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