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 \Generator[] 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 bool Whether the next value is the first value in an array or an object */ |
||
21 | private $first; |
||
22 | |||
23 | /** @var int The JSON encoding options */ |
||
24 | private $options; |
||
25 | |||
26 | /** @var bool Whether next token should be preceded by new line or not */ |
||
27 | private $newLine; |
||
28 | |||
29 | /** @var string Indent to use for indenting JSON output */ |
||
30 | private $indent; |
||
31 | |||
32 | /** @var string[] Errors that occurred in encoding */ |
||
33 | private $errors; |
||
34 | |||
35 | /** @var int Number of the current line in output */ |
||
36 | private $line; |
||
37 | |||
38 | /** @var int Number of the current column in output */ |
||
39 | private $column; |
||
40 | |||
41 | /** @var mixed The initial value to encode as JSON */ |
||
42 | private $initialValue; |
||
43 | |||
44 | /** @var int|null The current step of the encoder */ |
||
45 | private $step; |
||
46 | |||
47 | /** |
||
48 | * AbstractJsonEncoder constructor. |
||
49 | * @param mixed $value The value to encode as JSON |
||
50 | */ |
||
51 | 108 | public function __construct($value) |
|
52 | { |
||
53 | 108 | $this->initialValue = $value; |
|
54 | 108 | $this->options = 0; |
|
55 | 108 | $this->errors = []; |
|
56 | 108 | $this->indent = ' '; |
|
57 | 108 | $this->step = null; |
|
58 | 36 | } |
|
59 | |||
60 | /** |
||
61 | * Sets the JSON encoding options. |
||
62 | * @param int $options The JSON encoding options that are used by json_encode |
||
63 | * @return $this Returns self for call chaining |
||
64 | */ |
||
65 | 69 | public function setOptions($options) |
|
74 | |||
75 | /** |
||
76 | * Sets the indent for the JSON output. |
||
77 | * @param string|int $indent A string to use as indent or the number of spaces |
||
78 | * @return $this Returns self for call chaining |
||
79 | */ |
||
80 | 39 | public function setIndent($indent) |
|
89 | |||
90 | /** |
||
91 | * Returns the list of errors that occurred during the last encoding process. |
||
92 | * @return string[] List of errors that occurred during encoding |
||
93 | */ |
||
94 | 6 | public function getErrors() |
|
98 | |||
99 | /** |
||
100 | * Initializes the iterator if it has not been initialized yet. |
||
101 | */ |
||
102 | 81 | private function initialize() |
|
103 | { |
||
104 | 81 | if (!isset($this->stack)) { |
|
105 | 3 | $this->rewind(); |
|
106 | 1 | } |
|
107 | 27 | } |
|
108 | |||
109 | /** |
||
110 | * Returns the current number of step in the encoder. |
||
111 | * @return mixed The current step number as integer or null if the current state is not valid |
||
|
|||
112 | */ |
||
113 | 6 | public function key() |
|
119 | |||
120 | /** |
||
121 | * Tells if the encoder has a valid current state. |
||
122 | * @return bool True if the iterator has a valid state, false if not |
||
123 | */ |
||
124 | 78 | public function valid() |
|
130 | |||
131 | /** |
||
132 | * Returns the current value or state from the encoder. |
||
133 | * @return mixed The current value or state from the encoder |
||
134 | */ |
||
135 | abstract public function current(); |
||
136 | |||
137 | /** |
||
138 | * Returns the JSON encoding to the beginning. |
||
139 | */ |
||
140 | 108 | public function rewind() |
|
157 | |||
158 | /** |
||
159 | * Iterates the next token or tokens to the output stream. |
||
160 | */ |
||
161 | 78 | public function next() |
|
179 | |||
180 | /** |
||
181 | * Handles the next value from the generator to be encoded as JSON. |
||
182 | * @param \Generator $generator The generator used to generate the next value |
||
183 | * @param bool $isObject True if the generator is being handled as an object, false if not |
||
184 | */ |
||
185 | 51 | private function processStack(\Generator $generator, $isObject) |
|
198 | |||
199 | /** |
||
200 | * Handles the given value key into JSON. |
||
201 | * @param mixed $key The key to process |
||
202 | * @return bool True if the key is valid, false if not |
||
203 | */ |
||
204 | 30 | private function processKey($key) |
|
224 | |||
225 | /** |
||
226 | * Handles the given JSON value appropriately depending on it's type. |
||
227 | * @param mixed $value The value that should be encoded as JSON |
||
228 | */ |
||
229 | 108 | private function processValue($value) |
|
245 | |||
246 | /** |
||
247 | * Tells if the given value should be resolved prior to encoding. |
||
248 | * @param mixed $value The value to test |
||
249 | * @return bool true if the value is resolvable, false if not |
||
250 | */ |
||
251 | 108 | private function isResolvable($value) |
|
255 | |||
256 | /** |
||
257 | * Adds an JSON encoding error to the list of errors. |
||
258 | * @param string $message The error message to add |
||
259 | * @throws EncodingException If the encoding should not continue due to the error |
||
260 | */ |
||
261 | 9 | private function addError($message) |
|
275 | |||
276 | /** |
||
277 | * Pushes the given iterable to the value stack. |
||
278 | * @param object|array $iterable The iterable value to push to the stack |
||
279 | */ |
||
280 | 57 | private function pushStack($iterable) |
|
295 | |||
296 | /** |
||
297 | * Creates a generator from the given iterable using a foreach loop. |
||
298 | * @param object|array $iterable The iterable value to iterate |
||
299 | * @return \Generator The generator using the given iterable |
||
300 | */ |
||
301 | 57 | private function getIterator($iterable) |
|
307 | |||
308 | /** |
||
309 | * Tells if the given iterable should be handled as a JSON object or not. |
||
310 | * @param object|array $iterable The iterable value to test |
||
311 | * @param \Generator $generator Generator created from the iterable value |
||
312 | * @return bool True if the given iterable should be treated as object, false if not |
||
313 | */ |
||
314 | 57 | private function isObject($iterable, \Generator $generator) |
|
324 | |||
325 | /** |
||
326 | * Removes the top element of the value stack. |
||
327 | */ |
||
328 | 54 | private function popStack() |
|
343 | |||
344 | /** |
||
345 | * Encodes the given value as JSON and passes it to output stream. |
||
346 | * @param mixed $value The value to output as JSON |
||
347 | * @param int $token The token type of the value |
||
348 | */ |
||
349 | 102 | private function outputJson($value, $token) |
|
359 | |||
360 | /** |
||
361 | * Passes the given token to the output stream and ensures the next token is preceded by a newline. |
||
362 | * @param string $string The token to write to the output stream |
||
363 | * @param int $token The type of the token |
||
364 | */ |
||
365 | 57 | private function outputLine($string, $token) |
|
370 | |||
371 | /** |
||
372 | * Passes the given token to the output stream. |
||
373 | * @param string $string The token to write to the output stream |
||
374 | * @param int $token The type of the token |
||
375 | */ |
||
376 | 105 | private function output($string, $token) |
|
394 | |||
395 | /** |
||
396 | * Actually handles the writing of the given token to the output stream. |
||
397 | * @param string $string The given token to write |
||
398 | * @param int $token The type of the token |
||
399 | * @return void |
||
400 | */ |
||
401 | abstract protected function write($string, $token); |
||
402 | } |
||
403 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.