Complex classes like AbstractJsonEncoder 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 AbstractJsonEncoder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | abstract class AbstractJsonEncoder implements \Iterator |
||
| 13 | { |
||
| 14 | /** @var \Iterator[] Current value stack in encoding */ |
||
| 15 | private $stack; |
||
| 16 | |||
| 17 | /** @var bool[] True for every object in the stack, false for an array */ |
||
| 18 | private $stackType; |
||
| 19 | |||
| 20 | /** @var array Stack of values being encoded */ |
||
| 21 | private $valueStack; |
||
| 22 | |||
| 23 | /** @var bool Whether the next value is the first value in an array or an object */ |
||
| 24 | private $first; |
||
| 25 | |||
| 26 | /** @var int The JSON encoding options */ |
||
| 27 | private $options; |
||
| 28 | |||
| 29 | /** @var bool Whether next token should be preceded by new line or not */ |
||
| 30 | private $newLine; |
||
| 31 | |||
| 32 | /** @var string Indent to use for indenting JSON output */ |
||
| 33 | private $indent; |
||
| 34 | |||
| 35 | /** @var string[] Errors that occurred in encoding */ |
||
| 36 | private $errors; |
||
| 37 | |||
| 38 | /** @var int Number of the current line in output */ |
||
| 39 | private $line; |
||
| 40 | |||
| 41 | /** @var int Number of the current column in output */ |
||
| 42 | private $column; |
||
| 43 | |||
| 44 | /** @var mixed The initial value to encode as JSON */ |
||
| 45 | private $initialValue; |
||
| 46 | |||
| 47 | /** @var int|null The current step of the encoder */ |
||
| 48 | private $step; |
||
| 49 | |||
| 50 | /** |
||
| 51 | 108 | * AbstractJsonEncoder constructor. |
|
| 52 | * @param mixed $value The value to encode as JSON |
||
| 53 | 108 | */ |
|
| 54 | 108 | public function __construct($value) |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Sets the JSON encoding options. |
||
| 64 | * @param int $options The JSON encoding options that are used by json_encode |
||
| 65 | 69 | * @return $this Returns self for call chaining |
|
| 66 | * @throws \RuntimeException If changing encoding options during encoding operation |
||
| 67 | 69 | */ |
|
| 68 | 3 | public function setOptions($options) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Sets the indent for the JSON output. |
||
| 80 | 39 | * @param string|int $indent A string to use as indent or the number of spaces |
|
| 81 | * @return $this Returns self for call chaining |
||
| 82 | 39 | * @throws \RuntimeException If changing indent during encoding operation |
|
| 83 | 3 | */ |
|
| 84 | public function setIndent($indent) |
||
| 93 | |||
| 94 | 6 | /** |
|
| 95 | * Returns the list of errors that occurred during the last encoding process. |
||
| 96 | 6 | * @return string[] List of errors that occurred during encoding |
|
| 97 | */ |
||
| 98 | public function getErrors() |
||
| 102 | 81 | ||
| 103 | /** |
||
| 104 | 81 | * Returns the current encoding value stack. |
|
| 105 | 3 | * @return array The current encoding value stack |
|
| 106 | 1 | */ |
|
| 107 | 27 | protected function getValueStack() |
|
| 111 | |||
| 112 | /** |
||
| 113 | 6 | * Initializes the iterator if it has not been initialized yet. |
|
| 114 | */ |
||
| 115 | 6 | private function initialize() |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Returns the current number of step in the encoder. |
||
| 124 | 78 | * @return mixed The current step number as integer or null if the current state is not valid |
|
|
|
|||
| 125 | */ |
||
| 126 | 78 | public function key() |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Tells if the encoder has a valid current state. |
||
| 135 | * @return bool True if the iterator has a valid state, false if not |
||
| 136 | */ |
||
| 137 | public function valid() |
||
| 143 | 3 | ||
| 144 | /** |
||
| 145 | * Returns the current value or state from the encoder. |
||
| 146 | 108 | * @return mixed The current value or state from the encoder |
|
| 147 | 108 | */ |
|
| 148 | 108 | abstract public function current(); |
|
| 149 | 108 | ||
| 150 | 108 | /** |
|
| 151 | 108 | * Returns the JSON encoding to the beginning. |
|
| 152 | 108 | */ |
|
| 153 | 108 | public function rewind() |
|
| 171 | 51 | ||
| 172 | 17 | /** |
|
| 173 | 55 | * Iterates the next token or tokens to the output stream. |
|
| 174 | */ |
||
| 175 | 19 | public function next() |
|
| 193 | 6 | ||
| 194 | /** |
||
| 195 | 51 | * Handles the next value from the iterator to be encoded as JSON. |
|
| 196 | 51 | * @param \Iterator $iterator The iterator used to generate the next value |
|
| 197 | 17 | * @param bool $isObject True if the iterator is being handled as an object, false if not |
|
| 198 | */ |
||
| 199 | private function processStack(\Iterator $iterator, $isObject) |
||
| 212 | 18 | ||
| 213 | 6 | /** |
|
| 214 | * Handles the given value key into JSON. |
||
| 215 | 30 | * @param mixed $key The key to process |
|
| 216 | 30 | * @return bool True if the key is valid, false if not |
|
| 217 | */ |
||
| 218 | 30 | private function processKey($key) |
|
| 238 | |||
| 239 | 108 | /** |
|
| 240 | 57 | * Handles the given JSON value appropriately depending on it's type. |
|
| 241 | 19 | * @param mixed $value The value that should be encoded as JSON |
|
| 242 | 102 | */ |
|
| 243 | private function processValue($value) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Resolves the actual value of any given value that is about to be processed. |
||
| 258 | * @param mixed $value The value to resolve |
||
| 259 | * @return mixed The resolved value |
||
| 260 | */ |
||
| 261 | 9 | protected function resolveValue($value) |
|
| 273 | 6 | ||
| 274 | /** |
||
| 275 | * Adds an JSON encoding error to the list of errors. |
||
| 276 | * @param string $message The error message to add |
||
| 277 | * @throws EncodingException If the encoding should not continue due to the error |
||
| 278 | */ |
||
| 279 | private function addError($message) |
||
| 293 | 57 | ||
| 294 | 19 | /** |
|
| 295 | * Pushes the given iterable to the value stack. |
||
| 296 | * @param object|array $iterable The iterable value to push to the stack |
||
| 297 | */ |
||
| 298 | private function pushStack($iterable) |
||
| 313 | |||
| 314 | 57 | /** |
|
| 315 | * Creates a generator from the given iterable using a foreach loop. |
||
| 316 | 57 | * @param object|array $iterable The iterable value to iterate |
|
| 317 | 3 | * @return \Generator The generator using the given iterable |
|
| 318 | 54 | */ |
|
| 319 | 45 | private function getIterator($iterable) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Tells if the given iterable should be handled as a JSON object or not. |
||
| 328 | 54 | * @param object|array $iterable The iterable value to test |
|
| 329 | * @param \Iterator $iterator An Iterator created from the iterable value |
||
| 330 | 54 | * @return bool True if the given iterable should be treated as object, false if not |
|
| 331 | 48 | */ |
|
| 332 | 16 | private function isObject($iterable, \Iterator $iterator) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Tells if the given array is an associative array. |
||
| 347 | * @param array $array The array to test |
||
| 348 | * @return bool True if the array is associative, false if not |
||
| 349 | 102 | */ |
|
| 350 | private function isAssociative(array $array) |
||
| 366 | |||
| 367 | 57 | /** |
|
| 368 | 57 | * Removes the top element of the value stack. |
|
| 369 | 19 | */ |
|
| 370 | private function popStack() |
||
| 387 | 15 | ||
| 388 | 5 | /** |
|
| 389 | * Encodes the given value as JSON and passes it to output stream. |
||
| 390 | 105 | * @param mixed $value The value to output as JSON |
|
| 391 | 105 | * @param int $token The token type of the value |
|
| 392 | 105 | */ |
|
| 393 | 35 | private function outputJson($value, $token) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Returns the name of the JSON error constant. |
||
| 407 | * @param int $error The error code to find |
||
| 408 | * @return string The name for the error code |
||
| 409 | */ |
||
| 410 | private function getJsonErrorName($error) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Passes the given token to the output stream and ensures the next token is preceded by a newline. |
||
| 425 | * @param string $string The token to write to the output stream |
||
| 426 | * @param int $token The type of the token |
||
| 427 | */ |
||
| 428 | private function outputLine($string, $token) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Passes the given token to the output stream. |
||
| 436 | * @param string $string The token to write to the output stream |
||
| 437 | * @param int $token The type of the token |
||
| 438 | */ |
||
| 439 | private function output($string, $token) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Actually handles the writing of the given token to the output stream. |
||
| 460 | * @param string $string The given token to write |
||
| 461 | * @param int $token The type of the token |
||
| 462 | * @return void |
||
| 463 | */ |
||
| 464 | abstract protected function write($string, $token); |
||
| 465 | } |
||
| 466 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.