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 |
||
34 | class JSONText extends \StringField |
||
35 | { |
||
36 | /** |
||
37 | * @var int |
||
38 | */ |
||
39 | const JSONTEXT_QUERY_OPERATOR = 1; |
||
40 | |||
41 | /** |
||
42 | * @var int |
||
43 | */ |
||
44 | const JSONTEXT_QUERY_JSONPATH = 2; |
||
45 | |||
46 | /** |
||
47 | * Which RDBMS backend are we using? The value set here changes the actual operators and operator-routines for the |
||
48 | * given backend. |
||
49 | * |
||
50 | * @var string |
||
51 | * @config |
||
52 | */ |
||
53 | private static $backend = 'postgres'; |
||
54 | |||
55 | /** |
||
56 | * @var array |
||
57 | * @config |
||
58 | * |
||
59 | * [<backend>] => [ |
||
60 | * [<method> => <operator>] |
||
61 | * ]; // For use in query() method. |
||
62 | */ |
||
63 | private static $allowed_operators = [ |
||
|
|||
64 | 'postgres' => [ |
||
65 | 'matchOnInt' => '->', |
||
66 | 'matchOnStr' => '->>', |
||
67 | 'matchOnPath' => '#>' |
||
68 | ] |
||
69 | ]; |
||
70 | |||
71 | /** |
||
72 | * Legitimate query return types. |
||
73 | * |
||
74 | * @var array |
||
75 | */ |
||
76 | private static $return_types = [ |
||
77 | 'json', 'array', 'silverstripe' |
||
78 | ]; |
||
79 | |||
80 | /** |
||
81 | * @var string |
||
82 | */ |
||
83 | protected $returnType = 'json'; |
||
84 | |||
85 | /** |
||
86 | * @var \Peekmo\JsonPath\JsonStore |
||
87 | */ |
||
88 | protected $jsonStore; |
||
89 | |||
90 | /** |
||
91 | * Taken from {@link Text}. |
||
92 | * |
||
93 | * @see DBField::requireField() |
||
94 | * @return void |
||
95 | */ |
||
96 | public function requireField() |
||
118 | |||
119 | /** |
||
120 | * @param string $title |
||
121 | * @return \HiddenField |
||
122 | */ |
||
123 | public function scaffoldSearchField($title = null) |
||
127 | |||
128 | /** |
||
129 | * @param string $title |
||
130 | * @return \HiddenField |
||
131 | */ |
||
132 | public function scaffoldFormField($title = null) |
||
136 | |||
137 | /** |
||
138 | * Tell all class methods to return data as JSON , an array or an array of SilverStripe DBField subtypes. |
||
139 | * |
||
140 | * @param string $type |
||
141 | * @return JSONText |
||
142 | * @throws \JSONText\Exceptions\JSONTextException |
||
143 | */ |
||
144 | public function setReturnType($type) |
||
155 | |||
156 | /** |
||
157 | * Returns the value of this field as an iterable. |
||
158 | * |
||
159 | * @return \Peekmo\JsonPath\JsonStore |
||
160 | * @throws \JSONText\Exceptions\JSONTextException |
||
161 | */ |
||
162 | public function getJSONStore() |
||
177 | |||
178 | /** |
||
179 | * Returns the JSON value of this field as an array. |
||
180 | * |
||
181 | * @return array |
||
182 | */ |
||
183 | public function getStoreAsArray() |
||
192 | |||
193 | /** |
||
194 | * Utility method to determine whether the data is really JSON or not. |
||
195 | * The Peekmo JSONStore lib won't accept otherwise valid JSON scalars like `true`, `false` & `null` so these need |
||
196 | * to be disallowed. |
||
197 | * |
||
198 | * @param string $value |
||
199 | * @param array $allowed An optional array of allowed "invalid" JSON values |
||
200 | * @return boolean |
||
201 | */ |
||
202 | public function isJson($value, array $allowed = []) |
||
218 | |||
219 | /** |
||
220 | * Convert an array to JSON via json_encode(). |
||
221 | * |
||
222 | * @param array $value |
||
223 | * @return string null|string |
||
224 | */ |
||
225 | public function toJson(array $value) |
||
233 | |||
234 | /** |
||
235 | * Convert an array's values into an array of SilverStripe DBField subtypes ala: |
||
236 | * |
||
237 | * - {@link Int} |
||
238 | * - {@link Float} |
||
239 | * - {@link Boolean} |
||
240 | * - {@link Varchar} |
||
241 | * |
||
242 | * @param array $data |
||
243 | * @return array |
||
244 | */ |
||
245 | public function toSSTypes(array $data) |
||
258 | |||
259 | /** |
||
260 | * @param mixed $value |
||
261 | * @return array |
||
262 | * @throws \JSONText\Exceptions\JSONTextException |
||
263 | */ |
||
264 | public function toArray($value) |
||
275 | |||
276 | /** |
||
277 | * Return an array of the JSON key + value represented as first (top-level) JSON node. |
||
278 | * |
||
279 | * @return array |
||
280 | */ |
||
281 | public function first() |
||
294 | |||
295 | /** |
||
296 | * Return an array of the JSON key + value represented as last JSON node. |
||
297 | * |
||
298 | * @return array |
||
299 | */ |
||
300 | public function last() |
||
314 | |||
315 | /** |
||
316 | * Return an array of the JSON key + value represented as the $n'th JSON node. |
||
317 | * |
||
318 | * @param int $n |
||
319 | * @return mixed array |
||
320 | * @throws \JSONText\Exceptions\JSONTextException |
||
321 | */ |
||
322 | public function nth($n) |
||
345 | |||
346 | /** |
||
347 | * Return the key(s) + value(s) represented by $operator extracting relevant result from the source JSON's structure. |
||
348 | * N.b when using the path match operator '#>' with duplicate keys, an indexed array of results is returned. |
||
349 | * |
||
350 | * @param string $operator One of the legitimate operators for the current backend or a valid JSONPath expression. |
||
351 | * @param string $operand |
||
352 | * @return mixed null|array |
||
353 | * @throws \JSONText\Exceptions\JSONTextException |
||
354 | */ |
||
355 | public function query($operator, $operand = null) |
||
381 | |||
382 | /** |
||
383 | * Based on the passed operator or expression, it marshalls the correct backend matcher method into account. |
||
384 | * |
||
385 | * @param array $args |
||
386 | * @param integer $type |
||
387 | * @return array |
||
388 | * @throws \JSONText\Exceptions\JSONTextException |
||
389 | */ |
||
390 | private function marshallQuery($args, $type = 1) |
||
415 | |||
416 | /** |
||
417 | * Same as standard setValue() method except we can also accept a JSONPath expression. This expression will |
||
418 | * conditionally update the parts of the field's source JSON referenced by $expr with $value |
||
419 | * then re-set the entire JSON string as the field's new value. |
||
420 | * |
||
421 | * Note: The $expr parameter can only accept JSONPath expressions. Using Postgres operators will not work and will |
||
422 | * throw an instance of JSONTextException. |
||
423 | * |
||
424 | * @param mixed $value |
||
425 | * @param array $record |
||
426 | * @param string $expr A valid JSONPath expression. |
||
427 | * @return JSONText |
||
428 | * @throws JSONTextException |
||
429 | */ |
||
430 | public function setValue($value, $record = null, $expr = '') |
||
457 | |||
458 | /** |
||
459 | * Determine the desired userland format to return all query API method results in. |
||
460 | * |
||
461 | * @param mixed |
||
462 | * @return mixed array|null |
||
463 | * @throws \JSONText\Exceptions\JSONTextException |
||
464 | */ |
||
465 | private function returnAsType($data) |
||
496 | |||
497 | /** |
||
498 | * Create an instance of {@link JSONBackend} according to the value of JSONText::backend defined in SS config. |
||
499 | * |
||
500 | * @param string operand |
||
501 | * @return JSONBackend |
||
502 | * @throws JSONTextException |
||
503 | */ |
||
504 | protected function createBackendInst($operand) |
||
520 | |||
521 | /** |
||
522 | * Is the passed JSON operator valid? |
||
523 | * |
||
524 | * @param string $operator |
||
525 | * @return boolean |
||
526 | */ |
||
527 | public function isValidOperator($operator) |
||
537 | |||
538 | /** |
||
539 | * Is the passed JSPONPath expression valid? |
||
540 | * |
||
541 | * @param string $expression |
||
542 | * @return bool |
||
543 | */ |
||
544 | public function isValidExpression($expression) |
||
548 | |||
549 | /** |
||
550 | * Casts a value to a {@link DBField} subclass. |
||
551 | * |
||
552 | * @param mixed $val |
||
553 | * @return mixed DBField|array |
||
554 | */ |
||
555 | private function castToDBField($val) |
||
571 | |||
572 | } |
||
573 |
This check marks private properties in classes that are never used. Those properties can be removed.