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 | * Returns an input field. |
||
72 | * |
||
73 | * @param string $name |
||
74 | * @param null|string $title |
||
75 | * @param string $value |
||
76 | */ |
||
77 | public function __construct($name, $title = null, $value = '') |
||
81 | |||
82 | /** |
||
83 | * Taken from {@link TextField}. |
||
84 | * @see DBField::requireField() |
||
85 | * @return void |
||
86 | */ |
||
87 | public function requireField() |
||
103 | |||
104 | /** |
||
105 | * @param string $title |
||
106 | * @return HiddenField |
||
107 | */ |
||
108 | public function scaffoldSearchField($title = null) |
||
112 | |||
113 | /** |
||
114 | * @param string $title |
||
115 | * @return HiddenField |
||
116 | */ |
||
117 | public function scaffoldFormField($title = null) |
||
121 | |||
122 | /** |
||
123 | * Tell all class methods to return data as JSON or an array. |
||
124 | * |
||
125 | * @param string $type |
||
126 | * @return \JSONText |
||
127 | * @throws \JSONText\Exceptions\JSONTextException |
||
128 | */ |
||
129 | public function setReturnType($type) |
||
138 | |||
139 | /** |
||
140 | * @return string |
||
141 | */ |
||
142 | public function getReturnType() |
||
146 | |||
147 | /** |
||
148 | * Returns the value of this field as an iterable. |
||
149 | * |
||
150 | * @return \RecursiveIteratorIterator |
||
151 | * @throws \JSONText\Exceptions\JSONTextException |
||
152 | */ |
||
153 | public function getValueAsIterable() |
||
174 | |||
175 | /** |
||
176 | * Utility method to determine whether the data is really JSON or not. |
||
177 | * |
||
178 | * @param string $value |
||
179 | * @return boolean |
||
180 | */ |
||
181 | public function isJson($value) |
||
185 | |||
186 | /** |
||
187 | * @param array $value |
||
188 | * @return mixed null|string |
||
189 | */ |
||
190 | public function toJson($value) |
||
202 | |||
203 | /** |
||
204 | * @param mixed $value |
||
205 | * @return array |
||
206 | * @throws \JSONText\Exceptions\JSONTextException |
||
207 | */ |
||
208 | public function toArray($value) |
||
219 | |||
220 | /** |
||
221 | * Return an array of the JSON key + value represented as first (top-level) JSON node. |
||
222 | * |
||
223 | * @return array |
||
224 | */ |
||
225 | public function first() |
||
238 | |||
239 | /** |
||
240 | * Return an array of the JSON key + value represented as last JSON node. |
||
241 | * |
||
242 | * @return array |
||
243 | */ |
||
244 | public function last() |
||
257 | |||
258 | /** |
||
259 | * Return an array of the JSON key + value represented as the $n'th JSON node. |
||
260 | * |
||
261 | * @param int $n |
||
262 | * @return mixed array |
||
263 | * @throws \JSONText\Exceptions\JSONTextException |
||
264 | */ |
||
265 | public function nth($n) |
||
288 | |||
289 | /** |
||
290 | * Return an array of the JSON key(s) + value(s) represented by $operator extracting relevant result in a JSON |
||
291 | * node's value. |
||
292 | * |
||
293 | * @param string $operator |
||
294 | * @param string $operand |
||
295 | * @return mixed null|array |
||
296 | * @throws \JSONText\Exceptions\JSONTextException |
||
297 | * @todo How to increment an interator for each depth using $data->getDepth() and $i ?? |
||
298 | */ |
||
299 | public function query($operator, $operand) |
||
323 | |||
324 | /** |
||
325 | * Alias of self::query(). |
||
326 | * |
||
327 | * @param string $operator |
||
328 | * @return mixed string|array |
||
329 | * @throws \JSONText\Exceptions\JSONTextException |
||
330 | */ |
||
331 | public function extract($operator) |
||
335 | |||
336 | /** |
||
337 | * @param mixed $key |
||
338 | * @param mixed $val |
||
339 | * @param int $idx |
||
340 | * @param array $args |
||
341 | * @return array |
||
342 | * @throws \JSONText\Exceptions\JSONTextException |
||
343 | */ |
||
344 | private function marshallQuery($key, $val, $idx, $args) |
||
374 | |||
375 | /** |
||
376 | * @param array $data |
||
377 | * @return mixed |
||
378 | */ |
||
379 | private function returnAsType(array $data) |
||
389 | |||
390 | /** |
||
391 | * Is the passed JSON operator valid? |
||
392 | * |
||
393 | * @param string $operator |
||
394 | * @return boolean |
||
395 | */ |
||
396 | private function isValidOperator($operator) |
||
402 | |||
403 | } |
||
404 | |||
415 |
This check marks private properties in classes that are never used. Those properties can be removed.