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 | 68 | public function __construct($value) |
|
52 | { |
||
53 | 68 | $this->initialValue = $value; |
|
54 | 68 | $this->options = 0; |
|
55 | 68 | $this->errors = []; |
|
56 | 68 | $this->indent = ' '; |
|
57 | 68 | $this->step = null; |
|
58 | 34 | } |
|
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 | 46 | 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 | 26 | 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 | 4 | public function getErrors() |
|
98 | |||
99 | /** |
||
100 | * Initializes the iterator if it has not been initialized yet. |
||
101 | */ |
||
102 | 50 | private function initialize() |
|
103 | { |
||
104 | 50 | if (!isset($this->stack)) { |
|
105 | 2 | $this->rewind(); |
|
106 | 1 | } |
|
107 | 25 | } |
|
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 | 4 | 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 | 48 | 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 | 68 | public function rewind() |
|
157 | |||
158 | /** |
||
159 | * Iterates the next token or tokens to the output stream. |
||
160 | */ |
||
161 | 48 | 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 | 30 | private function processStack(\Generator $generator, $isObject) |
|
212 | |||
213 | /** |
||
214 | * Handles the given JSON value appropriately depending on it's type. |
||
215 | * @param mixed $value The value that should be encoded as JSON |
||
216 | */ |
||
217 | 68 | private function processValue($value) |
|
233 | |||
234 | /** |
||
235 | 4 | * Tells if the given value should be resolved prior to encoding |
|
236 | * @param mixed $value The value to test |
||
237 | 4 | * @return bool true if the value is resolvable, false if not |
|
238 | 4 | */ |
|
239 | private function isResolvable($value) |
||
243 | |||
244 | 2 | /** |
|
245 | * Adds an JSON encoding error to the list of errors. |
||
246 | * @param string $message The error message to add |
||
247 | * @throws EncodingException If the encoding should not continue due to the error |
||
248 | */ |
||
249 | private function addError($message) |
||
263 | 34 | ||
264 | 34 | /** |
|
265 | 17 | * Pushes the given iterable to the value stack. |
|
266 | * @param object|array $iterable The iterable value to push to the stack |
||
267 | */ |
||
268 | private function pushStack($iterable) |
||
283 | |||
284 | /** |
||
285 | 34 | * Creates a generator from the given iterable using a foreach loop. |
|
286 | * @param object|array $iterable The iterable value to iterate |
||
287 | 34 | * @return \Generator The generator using the given iterable |
|
288 | 2 | */ |
|
289 | 32 | private function getIterator($iterable) |
|
295 | |||
296 | /** |
||
297 | * Tells if the given iterable should be handled as a JSON object or not. |
||
298 | * @param object|array $iterable The iterable value to test |
||
299 | 34 | * @param \Generator $generator Generator created from the iterable value |
|
300 | * @return bool True if the given iterable should be treated as object, false if not |
||
301 | 34 | */ |
|
302 | 30 | private function isObject($iterable, \Generator $generator) |
|
312 | |||
313 | 17 | /** |
|
314 | * Removes the top element of the value stack. |
||
315 | */ |
||
316 | private function popStack() |
||
331 | |||
332 | /** |
||
333 | * Encodes the given value as JSON and passes it to output stream. |
||
334 | * @param mixed $value The value to output as JSON |
||
335 | * @param int $token The token type of the value |
||
336 | 34 | */ |
|
337 | private function outputJson($value, $token) |
||
347 | 66 | ||
348 | /** |
||
349 | 66 | * Passes the given token to the output stream and ensures the next token is preceded by a newline. |
|
350 | 10 | * @param string $string The token to write to the output stream |
|
351 | 10 | * @param int $token The type of the token |
|
352 | */ |
||
353 | 10 | private function outputLine($string, $token) |
|
358 | 10 | ||
359 | 5 | /** |
|
360 | * Passes the given token to the output stream. |
||
361 | 66 | * @param string $string The token to write to the output stream |
|
362 | 66 | * @param int $token The type of the token |
|
363 | 66 | */ |
|
364 | 33 | private function output($string, $token) |
|
382 | |||
383 | /** |
||
384 | * Actually handles the writing of the given token to the output stream. |
||
385 | * @param string $string The given token to write |
||
386 | * @param int $token The type of the token |
||
387 | * @return void |
||
388 | */ |
||
389 | abstract protected function write($string, $token); |
||
390 | } |
||
391 |
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.