Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like JsonElementReader often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use JsonElementReader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | final class JsonElementReader implements JsonReadable |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * A stack representing the next element to be consumed |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | private $stack = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * An array of types that map to the position in the stack |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | private $stackTypes = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The current size of the stack |
||
| 43 | * |
||
| 44 | * @var int |
||
| 45 | */ |
||
| 46 | private $stackSize = 0; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * A cache of the current [@see JsonToken]. This should get nulled out |
||
| 50 | * whenever a new token should be returned with the subsequent call |
||
| 51 | * to [@see JsonDecodeReader::peek] |
||
| 52 | * |
||
| 53 | * @var |
||
| 54 | */ |
||
| 55 | private $currentToken; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Constructor |
||
| 59 | * |
||
| 60 | * @param JsonElement $jsonElement |
||
| 61 | */ |
||
| 62 | 69 | public function __construct(JsonElement $jsonElement) |
|
| 63 | { |
||
| 64 | 69 | $this->push($jsonElement); |
|
| 65 | 69 | } |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Consumes the next token and asserts it's the beginning of a new array |
||
| 69 | * |
||
| 70 | * @return void |
||
| 71 | * @throws \Tebru\Gson\Exception\UnexpectedJsonTokenException If the next token is not BEGIN_ARRAY |
||
| 72 | */ |
||
| 73 | 15 | public function beginArray(): void |
|
| 74 | { |
||
| 75 | 15 | if ($this->peek() !== JsonToken::BEGIN_ARRAY) { |
|
| 76 | 1 | throw new UnexpectedJsonTokenException( |
|
| 77 | 1 | sprintf('Expected "%s", but found "%s"', JsonToken::BEGIN_ARRAY, $this->peek()) |
|
| 78 | ); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** @var JsonArray $jsonArray */ |
||
| 82 | 14 | $jsonArray = $this->pop(); |
|
| 83 | 14 | $this->push($jsonArray->getIterator(), ArrayIterator::class); |
|
|
|
|||
| 84 | 14 | } |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Consumes the next token and asserts it's the end of an array |
||
| 88 | * |
||
| 89 | * @return void |
||
| 90 | * @throws \Tebru\Gson\Exception\UnexpectedJsonTokenException If the next token is not END_ARRAY |
||
| 91 | */ |
||
| 92 | 5 | public function endArray(): void |
|
| 93 | { |
||
| 94 | 5 | if ($this->peek() !== JsonToken::END_ARRAY) { |
|
| 95 | 1 | throw new UnexpectedJsonTokenException( |
|
| 96 | 1 | sprintf('Expected "%s", but found "%s"', JsonToken::END_ARRAY, $this->peek()) |
|
| 97 | ); |
||
| 98 | } |
||
| 99 | |||
| 100 | 4 | $this->pop(); |
|
| 101 | 4 | } |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Consumes the next token and asserts it's the beginning of a new object |
||
| 105 | * |
||
| 106 | * @return void |
||
| 107 | * @throws \Tebru\Gson\Exception\UnexpectedJsonTokenException If the next token is not BEGIN_OBJECT |
||
| 108 | */ |
||
| 109 | 17 | public function beginObject(): void |
|
| 110 | { |
||
| 111 | 17 | if ($this->peek() !== JsonToken::BEGIN_OBJECT) { |
|
| 112 | 1 | throw new UnexpectedJsonTokenException( |
|
| 113 | 1 | sprintf('Expected "%s", but found "%s"', JsonToken::BEGIN_OBJECT, $this->peek()) |
|
| 114 | ); |
||
| 115 | } |
||
| 116 | |||
| 117 | 16 | $this->push(new JsonObjectIterator($this->pop()->asJsonObject()), JsonObjectIterator::class); |
|
| 118 | 16 | } |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Consumes the next token and asserts it's the end of an object |
||
| 122 | * |
||
| 123 | * @return void |
||
| 124 | * @throws \Tebru\Gson\Exception\UnexpectedJsonTokenException If the next token is not END_OBJECT |
||
| 125 | */ |
||
| 126 | 4 | public function endObject(): void |
|
| 127 | { |
||
| 128 | 4 | if ($this->peek() !== JsonToken::END_OBJECT) { |
|
| 129 | 1 | throw new UnexpectedJsonTokenException( |
|
| 130 | 1 | sprintf('Expected "%s", but found "%s"', JsonToken::END_OBJECT, $this->peek()) |
|
| 131 | ); |
||
| 132 | } |
||
| 133 | |||
| 134 | 3 | $this->pop(); |
|
| 135 | 3 | } |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Returns true if the array or object has another element |
||
| 139 | * |
||
| 140 | * If the current scope is not an array or object, this returns false |
||
| 141 | * |
||
| 142 | * @return bool |
||
| 143 | */ |
||
| 144 | 4 | public function hasNext(): bool |
|
| 145 | { |
||
| 146 | 4 | $peek = $this->peek(); |
|
| 147 | |||
| 148 | 4 | return $peek !== JsonToken::END_OBJECT && $peek !== JsonToken::END_ARRAY; |
|
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Consumes the value of the next token, asserts it's a boolean and returns it |
||
| 153 | * |
||
| 154 | * @return bool |
||
| 155 | * @throws \Tebru\Gson\Exception\UnexpectedJsonTokenException If the next token is not BOOLEAN |
||
| 156 | */ |
||
| 157 | 6 | public function nextBoolean(): bool |
|
| 158 | { |
||
| 159 | 6 | if ($this->peek() !== JsonToken::BOOLEAN) { |
|
| 160 | 1 | throw new UnexpectedJsonTokenException( |
|
| 161 | 1 | sprintf('Expected "%s", but found "%s"', JsonToken::BOOLEAN, $this->peek()) |
|
| 162 | ); |
||
| 163 | } |
||
| 164 | |||
| 165 | 5 | return $this->pop()->asBoolean(); |
|
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Consumes the value of the next token, asserts it's a double and returns it |
||
| 170 | * |
||
| 171 | * @return double |
||
| 172 | * @throws \Tebru\Gson\Exception\UnexpectedJsonTokenException If the next token is not NUMBER |
||
| 173 | */ |
||
| 174 | 3 | View Code Duplication | public function nextDouble(): float |
| 175 | { |
||
| 176 | 3 | if ($this->peek() !== JsonToken::NUMBER) { |
|
| 177 | 1 | throw new UnexpectedJsonTokenException( |
|
| 178 | 1 | sprintf('Expected "%s", but found "%s"', JsonToken::NUMBER, $this->peek()) |
|
| 179 | ); |
||
| 180 | } |
||
| 181 | |||
| 182 | 2 | return $this->pop()->asFloat(); |
|
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Consumes the value of the next token, asserts it's an int and returns it |
||
| 187 | * |
||
| 188 | * @return int |
||
| 189 | * @throws \Tebru\Gson\Exception\UnexpectedJsonTokenException If the next token is not NUMBER |
||
| 190 | */ |
||
| 191 | 6 | public function nextInteger(): int |
|
| 192 | { |
||
| 193 | 6 | if ($this->peek() !== JsonToken::NUMBER) { |
|
| 194 | 1 | throw new UnexpectedJsonTokenException( |
|
| 195 | 1 | sprintf('Expected "%s", but found "%s"', JsonToken::NUMBER, $this->peek()) |
|
| 196 | ); |
||
| 197 | } |
||
| 198 | |||
| 199 | 5 | return $this->pop()->asInteger(); |
|
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Consumes the value of the next token, asserts it's a string and returns it |
||
| 204 | * |
||
| 205 | * @return string |
||
| 206 | * @throws \Tebru\Gson\Exception\UnexpectedJsonTokenException If the next token is not NAME or STRING |
||
| 207 | */ |
||
| 208 | 14 | View Code Duplication | public function nextString(): string |
|
1 ignored issue
–
show
|
|||
| 209 | { |
||
| 210 | 14 | $peek = $this->peek(); |
|
| 211 | 14 | if ($peek === JsonToken::NAME) { |
|
| 212 | 1 | return $this->nextName(); |
|
| 213 | } |
||
| 214 | |||
| 215 | 13 | if ($peek !== JsonToken::STRING) { |
|
| 216 | 1 | throw new UnexpectedJsonTokenException( |
|
| 217 | 1 | sprintf('Expected "%s", but found "%s"', JsonToken::STRING, $this->peek()) |
|
| 218 | ); |
||
| 219 | } |
||
| 220 | |||
| 221 | 12 | return $this->pop()->asString(); |
|
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Consumes the value of the next token and asserts it's null |
||
| 226 | * |
||
| 227 | * @return null |
||
| 228 | * @throws \Tebru\Gson\Exception\UnexpectedJsonTokenException If the next token is not NAME or NULL |
||
| 229 | */ |
||
| 230 | 2 | View Code Duplication | public function nextNull() |
| 231 | { |
||
| 232 | 2 | if ($this->peek() !== JsonToken::NULL) { |
|
| 233 | 1 | throw new UnexpectedJsonTokenException( |
|
| 234 | 1 | sprintf('Expected "%s", but found "%s"', JsonToken::NULL, $this->peek()) |
|
| 235 | ); |
||
| 236 | } |
||
| 237 | |||
| 238 | 1 | $this->pop(); |
|
| 239 | |||
| 240 | 1 | return null; |
|
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Consumes the next name and returns it |
||
| 245 | * |
||
| 246 | * @return string |
||
| 247 | * @throws \Tebru\Gson\Exception\UnexpectedJsonTokenException If the next token is not NAME |
||
| 248 | */ |
||
| 249 | 9 | View Code Duplication | public function nextName(): string |
| 267 | |||
| 268 | /** |
||
| 269 | * Returns an enum representing the type of the next token without consuming it |
||
| 270 | * |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | 69 | public function peek(): string |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Skip the next value. If the next value is an object or array, all children will |
||
| 332 | * also be skipped. |
||
| 333 | * |
||
| 334 | * @return void |
||
| 335 | */ |
||
| 336 | 1 | public function skipValue(): void |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Push an element onto the stack |
||
| 343 | * |
||
| 344 | * @param JsonElement|Iterator $element |
||
| 345 | * @param string $type |
||
| 346 | */ |
||
| 347 | 69 | View Code Duplication | private function push($element, $type = null): void |
|
1 ignored issue
–
show
|
|||
| 348 | { |
||
| 349 | 69 | if (null === $type) { |
|
| 350 | 69 | $type = get_class($element); |
|
|
1 ignored issue
–
show
|
|||
| 351 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Pop the last element off the stack and return it |
||
| 361 | * |
||
| 362 | * @return JsonElement|Iterator |
||
| 363 | */ |
||
| 364 | 45 | View Code Duplication | private function pop() |
| 372 | } |
||
| 373 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: