Complex classes like JsonArray 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 JsonArray, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class JsonArray implements \JsonSerializable |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * The json data. |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | private $data; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Create a new instance. |
||
| 40 | * |
||
| 41 | * @param string|array $data The json data. |
||
| 42 | */ |
||
| 43 | public function __construct($data = '{}') |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Set the data. |
||
| 54 | * |
||
| 55 | * @param array $data The data array. |
||
| 56 | * |
||
| 57 | * @return JsonArray |
||
| 58 | */ |
||
| 59 | public function setData($data) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Merge the passed data into this instance. |
||
| 68 | * |
||
| 69 | * @param array $data The data to absorb. |
||
| 70 | * |
||
| 71 | * @return JsonArray |
||
| 72 | */ |
||
| 73 | public function merge($data) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Retrieve the data as array. |
||
| 80 | * |
||
| 81 | * @return array |
||
| 82 | */ |
||
| 83 | public function getData() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Load the data. |
||
| 90 | * |
||
| 91 | * @param string $data The json data. |
||
| 92 | * |
||
| 93 | * @return JsonArray |
||
| 94 | * |
||
| 95 | * @throws \RuntimeException When the data is invalid. |
||
| 96 | */ |
||
| 97 | public function load($data) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Split the path into chunks. |
||
| 112 | * |
||
| 113 | * @param string $path The path to split. |
||
| 114 | * |
||
| 115 | * @return array |
||
| 116 | * |
||
| 117 | * @throws \InvalidArgumentException When the path is invalid. |
||
| 118 | */ |
||
| 119 | protected function splitPath($path) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Escape a string to be used as literal path. |
||
| 135 | * |
||
| 136 | * @param string $path The string to escape. |
||
| 137 | * |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | public function unescape($path) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Escape a string to be used as literal path. |
||
| 147 | * |
||
| 148 | * @param string $path The string to escape. |
||
| 149 | * |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | public function escape($path) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Retrieve a value. |
||
| 159 | * |
||
| 160 | * @param string $path The path of the value. |
||
| 161 | * |
||
| 162 | * @param bool $forceArray Flag if the result shall be casted to array. |
||
| 163 | * |
||
| 164 | * @return array|string|int|null |
||
| 165 | */ |
||
| 166 | public function get($path, $forceArray = false) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Set a value. |
||
| 196 | * |
||
| 197 | * @param string $path The path of the value. |
||
| 198 | * |
||
| 199 | * @param mixed $value The value to set. |
||
| 200 | * |
||
| 201 | * @return JsonArray |
||
| 202 | */ |
||
| 203 | public function set($path, $value) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Check if a value exists. |
||
| 241 | * |
||
| 242 | * @param string $path The path of the value. |
||
| 243 | * |
||
| 244 | * @return bool |
||
| 245 | */ |
||
| 246 | public function has($path) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Unset a value. |
||
| 264 | * |
||
| 265 | * @param string $path The path of the value. |
||
| 266 | * |
||
| 267 | * @return JsonArray |
||
| 268 | */ |
||
| 269 | public function remove($path) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Check if a given path has an empty value (or does not exist). |
||
| 276 | * |
||
| 277 | * @param string $path The sub path to be sorted. |
||
| 278 | * |
||
| 279 | * @return bool |
||
| 280 | */ |
||
| 281 | public function isEmpty($path) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Retrieve the contained keys at the given path. |
||
| 288 | * |
||
| 289 | * @param string $path The sub path to be examined. |
||
| 290 | * |
||
| 291 | * @return string[] |
||
| 292 | */ |
||
| 293 | public function getEntries($path) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Sort the array by the provided user function. |
||
| 312 | * |
||
| 313 | * @param callable $callback The callback function to use. |
||
| 314 | * |
||
| 315 | * @param string $path The sub path to be sorted. |
||
| 316 | * |
||
| 317 | * @return void |
||
| 318 | */ |
||
| 319 | public function uasort($callback, $path = '/') |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Encode the array as string and return it. |
||
| 333 | * |
||
| 334 | * @return string |
||
| 335 | */ |
||
| 336 | public function __toString() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Return the data which should be serialized to JSON. |
||
| 344 | * |
||
| 345 | * @return array |
||
| 346 | */ |
||
| 347 | public function jsonSerialize() |
||
| 351 | } |
||
| 352 |