Complex classes like JSONText 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 JSONText, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class JSONText extends \StringField |
||
32 | { |
||
33 | /** |
||
34 | * Which RDBMS backend are we using? The value set here changes the actual operators and operator-routines for the |
||
35 | * given backend. |
||
36 | * |
||
37 | * @var string |
||
38 | * @config |
||
39 | */ |
||
40 | private static $backend = 'postgres'; |
||
1 ignored issue
–
show
|
|||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | * @config |
||
45 | * |
||
46 | * [<backend>] => [ |
||
47 | * [<method> => <operator>] |
||
48 | * ]; // For use in query() method. |
||
49 | */ |
||
50 | private static $allowed_operators = [ |
||
51 | 'postgres' => [ |
||
52 | 'matchIfKeyIsInt' => '->', |
||
53 | 'matchIfKeyIsStr' => '->>', |
||
54 | 'matchOnPath' => '#>' |
||
55 | ] |
||
56 | ]; |
||
57 | |||
58 | /** |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $returnType = 'json'; |
||
62 | |||
63 | /** |
||
64 | * Object cache for performance improvements. |
||
65 | * |
||
66 | * @var \RecursiveIteratorIterator |
||
67 | */ |
||
68 | protected $data; |
||
69 | |||
70 | /** |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $cache = []; |
||
74 | |||
75 | public function updateCache($val) { |
||
78 | |||
79 | /** |
||
80 | * Returns an input field. |
||
81 | * |
||
82 | * @param string $name |
||
83 | * @param null|string $title |
||
84 | * @param string $value |
||
85 | */ |
||
86 | public function __construct($name, $title = null, $value = '') |
||
90 | |||
91 | /** |
||
92 | * Taken from {@link TextField}. |
||
93 | * @see DBField::requireField() |
||
94 | * @return void |
||
95 | */ |
||
96 | public function requireField() |
||
112 | |||
113 | /** |
||
114 | * @param string $title |
||
115 | * @return HiddenField |
||
116 | */ |
||
117 | public function scaffoldSearchField($title = null) |
||
121 | |||
122 | /** |
||
123 | * @param string $title |
||
124 | * @return HiddenField |
||
125 | */ |
||
126 | public function scaffoldFormField($title = null) |
||
130 | |||
131 | /** |
||
132 | * Tell all class methods to return data as JSON or an array. |
||
133 | * |
||
134 | * @param string $type |
||
135 | * @return \JSONText |
||
136 | * @throws \JSONText\Exceptions\JSONTextException |
||
137 | */ |
||
138 | public function setReturnType($type) |
||
147 | |||
148 | /** |
||
149 | * @return string |
||
150 | */ |
||
151 | public function getReturnType() |
||
155 | |||
156 | /** |
||
157 | * Returns the value of this field as an iterable. |
||
158 | * |
||
159 | * @return \RecursiveIteratorIterator |
||
160 | * @throws \JSONText\Exceptions\JSONTextException |
||
161 | */ |
||
162 | public function getValueAsIterable() |
||
182 | |||
183 | /** |
||
184 | * Returns the value of this field as a flattened array |
||
185 | * |
||
186 | * @return array |
||
187 | */ |
||
188 | public function getValueAsArray() |
||
192 | |||
193 | /** |
||
194 | * Utility method to determine whether the data is really JSON or not. |
||
195 | * |
||
196 | * @param string $value |
||
197 | * @return boolean |
||
198 | */ |
||
199 | public function isJson($value) |
||
203 | |||
204 | /** |
||
205 | * @param array $value |
||
206 | * @return mixed null|string |
||
207 | */ |
||
208 | public function toJson($value) |
||
220 | |||
221 | /** |
||
222 | * @param mixed $value |
||
223 | * @return array |
||
224 | * @throws \JSONText\Exceptions\JSONTextException |
||
225 | */ |
||
226 | public function toArray($value) |
||
237 | |||
238 | /** |
||
239 | * Return an array of the JSON key + value represented as first (top-level) JSON node. |
||
240 | * |
||
241 | * @return array |
||
242 | */ |
||
243 | public function first() |
||
256 | |||
257 | /** |
||
258 | * Return an array of the JSON key + value represented as last JSON node. |
||
259 | * |
||
260 | * @return array |
||
261 | */ |
||
262 | public function last() |
||
275 | |||
276 | /** |
||
277 | * Return an array of the JSON key + value represented as the $n'th JSON node. |
||
278 | * |
||
279 | * @param int $n |
||
280 | * @return mixed array |
||
281 | * @throws \JSONText\Exceptions\JSONTextException |
||
282 | */ |
||
283 | public function nth($n) |
||
306 | |||
307 | /** |
||
308 | * Return an array of the JSON key(s) + value(s) represented by $operator extracting relevant result in a JSON |
||
309 | * node's value. |
||
310 | * |
||
311 | * @param string $operator |
||
312 | * @param string $operand |
||
313 | * @return mixed null|array |
||
314 | * @throws \JSONText\Exceptions\JSONTextException |
||
315 | * @todo How to increment an interator for each depth using $data->getDepth() and $i ?? |
||
316 | */ |
||
317 | public function query($operator, $operand) |
||
341 | |||
342 | /** |
||
343 | * Alias of self::query(). |
||
344 | * |
||
345 | * @param string $operator |
||
346 | * @return mixed string|array |
||
347 | * @throws \JSONText\Exceptions\JSONTextException |
||
348 | */ |
||
349 | public function extract($operator) |
||
353 | |||
354 | /** |
||
355 | * @param mixed $key |
||
356 | * @param mixed $val |
||
357 | * @param int $idx |
||
358 | * @param array $args |
||
359 | * @return array |
||
360 | * @throws \JSONText\Exceptions\JSONTextException |
||
361 | */ |
||
362 | private function marshallQuery($key, $val, $idx, $args) |
||
392 | |||
393 | /** |
||
394 | * @param array $data |
||
395 | * @return mixed |
||
396 | */ |
||
397 | private function returnAsType(array $data) |
||
407 | |||
408 | /** |
||
409 | * Is the passed JSON operator valid? |
||
410 | * |
||
411 | * @param string $operator |
||
412 | * @return boolean |
||
413 | */ |
||
414 | private function isValidOperator($operator) |
||
420 | |||
421 | } |
||
422 | |||
433 |
This check marks private properties in classes that are never used. Those properties can be removed.