Complex classes like JSONStorage 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 JSONStorage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class JSONStorage extends CComponent |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * const string the key to keep value information |
||
| 31 | */ |
||
| 32 | const META = 'meta'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * const string the key of the registry |
||
| 36 | */ |
||
| 37 | const REGISTRY = 'registry'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string the filename to save the values to |
||
| 41 | */ |
||
| 42 | protected $filename = 'registry.json'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string the full path to the directory with read/write access to save the registry to. If none set, the |
||
| 46 | * component will used the application's runtime folder |
||
| 47 | */ |
||
| 48 | protected $path; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var bool whether the registry has changed or not |
||
| 52 | */ |
||
| 53 | protected $dirty = false; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var null|string the name of the default registry |
||
| 57 | */ |
||
| 58 | protected $default = "default"; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var array the data of the registry |
||
| 62 | */ |
||
| 63 | protected $data = array( |
||
| 64 | self::META => array( |
||
| 65 | "updated" => "", |
||
| 66 | "hash" => "" |
||
| 67 | ), |
||
| 68 | self::REGISTRY => array( |
||
| 69 | /* collection name */ |
||
| 70 | "default" => array( |
||
| 71 | "foo" => "bar" /* attributes by example */ |
||
| 72 | ) |
||
| 73 | ) |
||
| 74 | ); |
||
| 75 | |||
| 76 | /** |
||
| 77 | * class constructor |
||
| 78 | * |
||
| 79 | * @param null $registry |
||
| 80 | */ |
||
| 81 | public function __construct($registry = null) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * class destructor - flush data |
||
| 101 | */ |
||
| 102 | public function __destruct() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Fires before registry has been saved |
||
| 109 | * |
||
| 110 | * @param CEvent $event |
||
| 111 | */ |
||
| 112 | public function onBeforeSave($event) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Fires after the registry has been saved |
||
| 119 | * |
||
| 120 | * @param CEvent $event |
||
| 121 | */ |
||
| 122 | public function onAfterSave($event) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Property set path |
||
| 129 | * |
||
| 130 | * @param string $path the full path of the directory with read/write access to save the registry file to |
||
| 131 | * |
||
| 132 | * @return bool |
||
| 133 | * @throws Exception |
||
| 134 | */ |
||
| 135 | public function setPath($path) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Property get path |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | public function getPath() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Property set file |
||
| 155 | * |
||
| 156 | * @param string $file the filename to save the registry to |
||
| 157 | */ |
||
| 158 | public function setFile($file) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Property get file |
||
| 165 | * @return string filename |
||
| 166 | */ |
||
| 167 | public function getFile() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Verifies data integrity |
||
| 174 | * @return bool |
||
| 175 | */ |
||
| 176 | public function verify() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Loads registry data into memory |
||
| 188 | * @throws Exception |
||
| 189 | */ |
||
| 190 | public function load() |
||
| 191 | { |
||
| 192 | $filename = $this->getFile(); |
||
| 193 | if (!file_exists($filename)) |
||
| 194 | return; |
||
| 195 | |||
| 196 | $json = file_get_contents($filename); |
||
| 197 | if (strlen($json) == 0) |
||
| 198 | return; |
||
| 199 | |||
| 200 | $data = $this->decode($json); |
||
| 201 | if ($data === null) |
||
| 202 | throw new Exception('Error while trying to decode ' . $filename . '.'); |
||
| 203 | |||
| 204 | $this->data = $data; |
||
| 205 | |||
| 206 | if (!$this->verify()) |
||
| 207 | throw new Exception($filename . ' failed checksum validation.'); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Saves registry data to the file |
||
| 212 | */ |
||
| 213 | public function save() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Saves data to the registry |
||
| 226 | * |
||
| 227 | * @param string $key the name of the key that will hold the data |
||
| 228 | * @param array $data the data to save |
||
| 229 | * @param string $registry the name of the registry |
||
| 230 | * |
||
| 231 | * @return bool |
||
| 232 | */ |
||
| 233 | public function setData($key, $data, $registry = null) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Retrieves a data value from the registry |
||
| 248 | * |
||
| 249 | * @param string $key the name of the key that holds the data |
||
| 250 | * @param string $registry the registry name |
||
| 251 | * |
||
| 252 | * @return mixed the data in the key value, null otherwise |
||
| 253 | */ |
||
| 254 | public function getData($key, $registry = null) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Removes data from a key in the registry |
||
| 269 | * |
||
| 270 | * @param string $key the key name that holds the data to remove |
||
| 271 | * @param string $registry the registry name |
||
| 272 | * |
||
| 273 | * @return bool true if successful, false otherwise |
||
| 274 | */ |
||
| 275 | public function removeData($key, $registry = null) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Retrieves the number of keys in registry |
||
| 293 | * |
||
| 294 | * @param string $registry the registry name |
||
| 295 | * |
||
| 296 | * @return int the data length |
||
| 297 | */ |
||
| 298 | public function getLength($registry = null) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Retrieves a registry collection based on its name |
||
| 311 | * |
||
| 312 | * @param string $registry the name of the registry to retrieve |
||
| 313 | * |
||
| 314 | * @return mixed|null the registry, null if none found |
||
| 315 | */ |
||
| 316 | public function getRegistry($registry) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Checkes whether a collection exists (registry) |
||
| 323 | * |
||
| 324 | * @param string $registry the name of the registry to check existence |
||
| 325 | * |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | public function registryExists($registry) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Add new collection name |
||
| 335 | * |
||
| 336 | * @param string $registry the name of the registry (collection) to create |
||
| 337 | * |
||
| 338 | * @return bool |
||
| 339 | */ |
||
| 340 | public function addRegistry($registry) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Remove an existing collection and all associated data |
||
| 352 | * |
||
| 353 | * @param string $registry the name of the registry to remove |
||
| 354 | * |
||
| 355 | * @return bool |
||
| 356 | */ |
||
| 357 | public function removeRegistry($registry) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Saves the global registry to the file |
||
| 369 | * @return bool |
||
| 370 | * @throws Exception |
||
| 371 | */ |
||
| 372 | private function flush() |
||
| 373 | { |
||
| 374 | // check if writeback is needed |
||
| 375 | if ($this->dirty == false) |
||
| 376 | return true; |
||
| 377 | |||
| 378 | // prepare to writeback to file |
||
| 379 | $data = $this->data; |
||
| 380 | $registry = $this->encode($this->data[self::REGISTRY]); |
||
| 381 | $data[self::META]["updated"] = date("c"); |
||
| 382 | $data[self::META]["hash"] = md5($registry); |
||
| 383 | |||
| 384 | // overwrite existing data |
||
| 385 | $written = file_put_contents($this->getFile(), $this->encode($data)); |
||
| 386 | if ($written === false) |
||
| 387 | throw new Exception(strtr( 'Unable to write back to {FILE}. Data will be lost!', array('{FILE}' => $this->getFile()) )); |
||
| 388 | |||
| 389 | return true; |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * JSON encodes the data |
||
| 394 | * |
||
| 395 | * @param mixed $data |
||
| 396 | * |
||
| 397 | * @return string |
||
| 398 | */ |
||
| 399 | private function encode($data) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * JSON decodes the data |
||
| 406 | * |
||
| 407 | * @param string $data |
||
| 408 | * |
||
| 409 | * @return mixed |
||
| 410 | */ |
||
| 411 | private function decode($data) |
||
| 415 | } |
||
| 416 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.