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 |
||
| 33 | class JSONText extends \StringField |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * Which RDBMS backend are we using? The value set here changes the actual operators and operator-routines for the |
||
| 37 | * given backend. |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | * @config |
||
| 41 | */ |
||
| 42 | private static $backend = 'postgres'; |
||
|
1 ignored issue
–
show
|
|||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array |
||
| 46 | * @config |
||
| 47 | * |
||
| 48 | * [<backend>] => [ |
||
| 49 | * [<method> => <operator>] |
||
| 50 | * ]; // For use in query() method. |
||
| 51 | */ |
||
| 52 | private static $allowed_operators = [ |
||
| 53 | 'postgres' => [ |
||
| 54 | 'matchIfKeyIsInt' => '->', |
||
| 55 | 'matchIfKeyIsStr' => '->>', |
||
| 56 | 'matchOnPath' => '#>' |
||
| 57 | ] |
||
| 58 | ]; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | protected $returnType = 'json'; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Object cache for performance improvements. |
||
| 67 | * |
||
| 68 | * @var \RecursiveIteratorIterator |
||
| 69 | */ |
||
| 70 | protected $data; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | protected $cache = []; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param string $val |
||
| 79 | * @return void |
||
| 80 | */ |
||
| 81 | public function updateCache($val) { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Taken from {@link TextField}. |
||
| 87 | * |
||
| 88 | * @see DBField::requireField() |
||
| 89 | * @return void |
||
| 90 | */ |
||
| 91 | public function requireField() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param string $title |
||
| 110 | * @return HiddenField |
||
| 111 | */ |
||
| 112 | public function scaffoldSearchField($title = null) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param string $title |
||
| 119 | * @return HiddenField |
||
| 120 | */ |
||
| 121 | public function scaffoldFormField($title = null) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Tell all class methods to return data as JSON or an array. |
||
| 128 | * |
||
| 129 | * @param string $type |
||
| 130 | * @return \JSONText |
||
| 131 | * @throws \JSONText\Exceptions\JSONTextException |
||
| 132 | */ |
||
| 133 | public function setReturnType($type) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | public function getReturnType() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Returns the value of this field as an iterable. |
||
| 153 | * |
||
| 154 | * @return mixed |
||
| 155 | * @throws \JSONText\Exceptions\JSONTextException |
||
| 156 | */ |
||
| 157 | public function getValueAsIterable() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Returns the value of this field as a flattened array |
||
| 180 | * |
||
| 181 | * @return array |
||
| 182 | */ |
||
| 183 | public function getValueAsArray() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Utility method to determine whether the data is really JSON or not. |
||
| 190 | * |
||
| 191 | * @param string $value |
||
| 192 | * @return boolean |
||
| 193 | */ |
||
| 194 | public function isJson($value) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param array $value |
||
| 201 | * @return mixed null|string |
||
| 202 | */ |
||
| 203 | public function toJson($value) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param mixed $value |
||
| 218 | * @return array |
||
| 219 | * @throws \JSONText\Exceptions\JSONTextException |
||
| 220 | */ |
||
| 221 | public function toArray($value) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Return an array of the JSON key + value represented as first (top-level) JSON node. |
||
| 235 | * |
||
| 236 | * @return array |
||
| 237 | */ |
||
| 238 | public function first() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Return an array of the JSON key + value represented as last JSON node. |
||
| 254 | * |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | public function last() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Return an array of the JSON key + value represented as the $n'th JSON node. |
||
| 273 | * |
||
| 274 | * @param int $n |
||
| 275 | * @return mixed array |
||
| 276 | * @throws \JSONText\Exceptions\JSONTextException |
||
| 277 | */ |
||
| 278 | public function nth($n) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Return the key(s) + value(s) represented by $operator extracting relevant result from the source JSON's structure. |
||
| 304 | * N.b when using the path match operator '#>' with duplicate keys, an indexed array of results is returned. |
||
| 305 | * |
||
| 306 | * @param string $operator |
||
| 307 | * @param string $operand |
||
| 308 | * @return mixed null|array |
||
| 309 | * @throws \JSONText\Exceptions\JSONTextException |
||
| 310 | */ |
||
| 311 | public function query($operator, $operand) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Alias of self::query(). |
||
| 338 | * |
||
| 339 | * @param string $operator |
||
| 340 | * @return mixed string|array |
||
| 341 | * @throws \JSONText\Exceptions\JSONTextException |
||
| 342 | */ |
||
| 343 | public function extract($operator) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Based on the passed operator, ensure the correct backend matcher method is called. |
||
| 350 | * |
||
| 351 | * @param mixed $key |
||
| 352 | * @param mixed $val |
||
| 353 | * @param int $idx |
||
| 354 | * @param array $args |
||
| 355 | * @return array |
||
| 356 | * @throws \JSONText\Exceptions\JSONTextException |
||
| 357 | */ |
||
| 358 | private function marshallQuery($key, $val, $idx, $args) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Determine the desired userland format to return all query API method results in. |
||
| 390 | * |
||
| 391 | * @param array $data |
||
| 392 | * @return mixed |
||
| 393 | */ |
||
| 394 | private function returnAsType(array $data) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Is the passed JSON operator valid? |
||
| 407 | * |
||
| 408 | * @param string $operator |
||
| 409 | * @return boolean |
||
| 410 | */ |
||
| 411 | private function isValidOperator($operator) |
||
| 417 | |||
| 418 | } |
||
| 419 | |||
| 430 |
This check marks private properties in classes that are never used. Those properties can be removed.