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 |
||
| 38 | class JSONText extends \StringField |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * Which RDBMS backend are we using? The value set here changes the actual operators and operator-routines for the |
||
| 42 | * given backend. |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | * @config |
||
| 46 | */ |
||
| 47 | private static $backend = 'postgres'; |
||
|
1 ignored issue
–
show
|
|||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array |
||
| 51 | * @config |
||
| 52 | * |
||
| 53 | * [<backend>] => [ |
||
| 54 | * [<method> => <operator>] |
||
| 55 | * ]; // For use in query() method. |
||
| 56 | */ |
||
| 57 | private static $allowed_operators = [ |
||
| 58 | 'postgres' => [ |
||
| 59 | 'matchOnInt' => '->', |
||
| 60 | 'matchOnStr' => '->>', |
||
| 61 | 'matchOnPath' => '#>' |
||
| 62 | ] |
||
| 63 | ]; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Legitimate query return types |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | private static $return_types = [ |
||
| 71 | 'json', 'array', 'silverstripe' |
||
| 72 | ]; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | protected $returnType = 'json'; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var \Peekmo\JsonPath\JsonStore |
||
| 81 | */ |
||
| 82 | protected $jsonStore; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Taken from {@link TextField}. |
||
| 86 | * |
||
| 87 | * @see DBField::requireField() |
||
| 88 | * @return void |
||
| 89 | */ |
||
| 90 | public function requireField() |
||
| 91 | { |
||
| 92 | $parts = [ |
||
| 93 | 'datatype' => 'mediumtext', |
||
| 94 | 'character set' => 'utf8', |
||
| 95 | 'collate' => 'utf8_general_ci', |
||
| 96 | 'arrayValue' => $this->arrayValue |
||
| 97 | ]; |
||
| 98 | |||
| 99 | $values = [ |
||
| 100 | 'type' => 'text', |
||
| 101 | 'parts' => $parts |
||
| 102 | ]; |
||
| 103 | |||
| 104 | DB::require_field($this->tableName, $this->name, $values); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param string $title |
||
| 109 | * @return HiddenField |
||
| 110 | */ |
||
| 111 | public function scaffoldSearchField($title = null) |
||
| 112 | { |
||
| 113 | return HiddenField::create($this->getName()); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param string $title |
||
| 118 | * @return HiddenField |
||
| 119 | */ |
||
| 120 | public function scaffoldFormField($title = null) |
||
| 121 | { |
||
| 122 | return HiddenField::create($this->getName()); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Tell all class methods to return data as JSON , an array or an array of SilverStripe DBField subtypes. |
||
| 127 | * |
||
| 128 | * @param string $type |
||
| 129 | * @return \JSONText |
||
| 130 | * @throws \JSONText\Exceptions\JSONTextException |
||
| 131 | */ |
||
| 132 | public function setReturnType($type) |
||
| 133 | { |
||
| 134 | if (!in_array($type, $this->config()->return_types)) { |
||
| 135 | $msg = 'Bad type: ' . $type . ' passed to ' . __FUNCTION__; |
||
| 136 | throw new JSONTextException($msg); |
||
| 137 | } |
||
| 138 | |||
| 139 | $this->returnType = $type; |
||
| 140 | |||
| 141 | return $this; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | public function getReturnType() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Returns the value of this field as an iterable. |
||
| 154 | * |
||
| 155 | * @return \Peekmo\JsonPath\JsonStore |
||
| 156 | * @throws \JSONText\Exceptions\JSONTextException |
||
| 157 | */ |
||
| 158 | public function getJSONStore() |
||
| 159 | { |
||
| 160 | if (!$json = $this->getValue()) { |
||
| 161 | return []; |
||
| 162 | } |
||
| 163 | |||
| 164 | if (!$this->isJson($json)) { |
||
| 165 | $msg = 'DB data is munged.'; |
||
| 166 | throw new JSONTextException($msg); |
||
| 167 | } |
||
| 168 | |||
| 169 | $this->jsonStore = new \Peekmo\JsonPath\JsonStore($json); |
||
| 170 | |||
| 171 | return $this->jsonStore; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Returns the JSON value of this field as an array. |
||
| 176 | * |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | public function getStoreAsArray() |
||
| 180 | { |
||
| 181 | $store = $this->getJSONStore(); |
||
| 182 | if (!is_array($store)) { |
||
| 183 | return $store->toArray(); |
||
| 184 | } |
||
| 185 | |||
| 186 | return $store; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Utility method to determine whether the data is really JSON or not. |
||
| 191 | * |
||
| 192 | * @param string $value |
||
| 193 | * @return boolean |
||
| 194 | */ |
||
| 195 | public function isJson($value) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Convert an array to JSON via json_encode(). |
||
| 202 | * |
||
| 203 | * @param array $value |
||
| 204 | * @return mixed null|string |
||
| 205 | */ |
||
| 206 | public function toJson(array $value) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Convert an array's values into an array of SilverStripe DBField subtypes ala: |
||
| 221 | * |
||
| 222 | * - {@link Int} |
||
| 223 | * - {@link Float} |
||
| 224 | * - {@link Boolean} |
||
| 225 | * - {@link Varchar} |
||
| 226 | * |
||
| 227 | * @param array $data |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | public function toSSTypes(array $data) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param mixed $value |
||
| 246 | * @return array |
||
| 247 | * @throws \JSONText\Exceptions\JSONTextException |
||
| 248 | */ |
||
| 249 | public function toArray($value) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Return an array of the JSON key + value represented as first (top-level) JSON node. |
||
| 263 | * |
||
| 264 | * @return array |
||
| 265 | */ |
||
| 266 | public function first() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Return an array of the JSON key + value represented as last JSON node. |
||
| 282 | * |
||
| 283 | * @return array |
||
| 284 | */ |
||
| 285 | public function last() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Return an array of the JSON key + value represented as the $n'th JSON node. |
||
| 302 | * |
||
| 303 | * @param int $n |
||
| 304 | * @return mixed array |
||
| 305 | * @throws \JSONText\Exceptions\JSONTextException |
||
| 306 | */ |
||
| 307 | public function nth($n) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Return the key(s) + value(s) represented by $operator extracting relevant result from the source JSON's structure. |
||
| 333 | * N.b when using the path match operator '#>' with duplicate keys, an indexed array of results is returned. |
||
| 334 | * |
||
| 335 | * @param string $operator |
||
| 336 | * @param string $operand |
||
| 337 | * @return mixed null|array |
||
| 338 | * @throws \JSONText\Exceptions\JSONTextException |
||
| 339 | */ |
||
| 340 | public function query($operator, $operand) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Based on the passed operator, ensure the correct backend matcher method is called. |
||
| 362 | * |
||
| 363 | * @param array $args |
||
| 364 | * @return array |
||
| 365 | * @throws \JSONText\Exceptions\JSONTextException |
||
| 366 | */ |
||
| 367 | private function marshallQuery($args) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Determine the desired userland format to return all query API method results in. |
||
| 397 | * |
||
| 398 | * @param mixed |
||
| 399 | * @return mixed |
||
| 400 | * @throws \JSONText\Exceptions\JSONTextException |
||
| 401 | */ |
||
| 402 | private function returnAsType($data) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Is the passed JSON operator valid? |
||
| 436 | * |
||
| 437 | * @param string $operator |
||
| 438 | * @return boolean |
||
| 439 | */ |
||
| 440 | private function isValidOperator($operator) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Casts a value to a {@link DBField} subclass. |
||
| 449 | * |
||
| 450 | * @param mixed $val |
||
| 451 | * @return mixed DBField|array |
||
| 452 | */ |
||
| 453 | private function castToDBField($val) |
||
| 469 | |||
| 470 | } |
||
| 471 | |||
| 482 |
This check marks private properties in classes that are never used. Those properties can be removed.