Complex classes like store 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 store, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | abstract class store { |
||
| 39 | |||
| 40 | public $error; |
||
| 41 | public $root; |
||
| 42 | public $rootoptions; |
||
| 43 | public $mod_lock; |
||
| 44 | public $total; |
||
| 45 | protected $code; |
||
| 46 | protected $files; |
||
| 47 | protected $_filestores; |
||
| 48 | protected $config; |
||
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | public function __construct($path, $config) { |
||
| 56 | |||
| 57 | /* abstract functions |
||
| 58 | need implementation in your implementation of this store |
||
| 59 | */ |
||
| 60 | |||
| 61 | public abstract function call($template, $args, $objects); |
||
| 62 | |||
| 63 | public abstract function count($objects); |
||
| 64 | |||
| 65 | public abstract function info($objects); |
||
| 66 | |||
| 67 | public abstract function get($path); |
||
| 68 | /********************************************************************************** |
||
| 69 | This function takes as argument a path to an object in the store and will retrieve |
||
| 70 | all the necessary data and return this in the objectlist type needed for |
||
| 71 | store->call(). If the requested path does not exist, it will retrieve the object |
||
| 72 | with the longest matching path. |
||
| 73 | |||
| 74 | $path should always start and end with a '/'. |
||
| 75 | **********************************************************************************/ |
||
| 76 | |||
| 77 | public abstract function touch($id, $timestamp = -1); |
||
| 78 | /********************************************************************************** |
||
| 79 | This function takes as argument a path to an object (or id of an object) |
||
| 80 | in the store and will set the timestamp to $timestamp. |
||
| 81 | |||
| 82 | $path should always start and end with a '/'. |
||
| 83 | **********************************************************************************/ |
||
| 84 | |||
| 85 | public abstract function ls($path); |
||
| 86 | /********************************************************************************** |
||
| 87 | This function takes as argument a path to an object in the store and will retrieve |
||
| 88 | all the objects and their data which have this object as their parent. It will |
||
| 89 | then return this in the objectlist type needed for store->call(). If the requested |
||
| 90 | path does not exist, it will retrieve the object with the longest matching path. |
||
| 91 | |||
| 92 | $path should always start and end with a '/'. |
||
| 93 | **********************************************************************************/ |
||
| 94 | |||
| 95 | public abstract function parents($path, $top="/"); |
||
| 96 | /********************************************************************************** |
||
| 97 | This function takes as argument a path to an object in the store. It will return |
||
| 98 | all objects with a path which is a substring of the given path. The resulsts are |
||
| 99 | ordered by path (length), shortest paths first. |
||
| 100 | In effect all parents of an object in the tree are called, in order, starting at |
||
| 101 | the root. |
||
| 102 | |||
| 103 | $path should always start and end with a '/'. |
||
| 104 | **********************************************************************************/ |
||
| 105 | |||
| 106 | public abstract function find($path, $criteria, $limit=100, $offset=0); |
||
| 107 | /********************************************************************************** |
||
| 108 | This function takes as arguments a path to an object in the store and some search |
||
| 109 | criteria. It will search for all matching objects under the given path. If the |
||
| 110 | given path is not in this store but in a substore it will not automatically search |
||
| 111 | that substore. |
||
| 112 | |||
| 113 | $criteria is of the form |
||
| 114 | |||
| 115 | $criteria ::= ({ $property_name => ({ $valuename => ({ $compare_function, $value }) }) }) |
||
| 116 | |||
| 117 | e.g.: $criteria["status"]["value"][">"]="published"; |
||
| 118 | |||
| 119 | $path should always start and end with a '/'. |
||
| 120 | |||
| 121 | **********************************************************************************/ |
||
| 122 | |||
| 123 | |||
| 124 | public abstract function save($path, $type, $data, $properties="", $vtype="", $priority=false); |
||
| 125 | /*************************************************************** |
||
| 126 | This function takes as argument a path, type, objectdata and |
||
| 127 | possibly a properties list and vtype (virtual type). |
||
| 128 | If there exists no object with the given path, a new object is |
||
| 129 | saved with the given type, data, properties and vtype, and a |
||
| 130 | new path is saved pointing to it. |
||
| 131 | If there does exist an object with the given path, it's object |
||
| 132 | data is overwritten with the given data and if vtype is set the |
||
| 133 | current vtype is overwritten with the new one. |
||
| 134 | |||
| 135 | $path must be an absolute path (containing no '..' and starting |
||
| 136 | with '/') |
||
| 137 | $type must be a valid type |
||
| 138 | $data can be any string (usually a serialized object.) |
||
| 139 | $properties is a multidimensional hash of the following form: |
||
| 140 | $properties[{property_name}][][{value_name}]={value} |
||
| 141 | {property_name} must be a valid property name |
||
| 142 | {value_name} must be a valid value name for this property |
||
| 143 | {value} can be a number, boolean or string. |
||
| 144 | example: |
||
| 145 | $properties["name"][0]["value"]="A name"; |
||
| 146 | $properties["name"][1]["value"]="A second name!"; |
||
| 147 | if $properties["name"]=1 then all properties for property name |
||
| 148 | will be removed. |
||
| 149 | |||
| 150 | $vtype must be a valid type. |
||
| 151 | |||
| 152 | if $properties or $vtype are not set or empty ("",0 or false) |
||
| 153 | they will be ignored. $vtype defaults to $type. |
||
| 154 | Only those properties listed in $properties will be updated. |
||
| 155 | Any other property set will remain as it was. |
||
| 156 | ***************************************************************/ |
||
| 157 | |||
| 158 | |||
| 159 | protected abstract function purge($path); |
||
| 160 | /********************************************************************** |
||
| 161 | This function will delete the object pointed to by $path and all |
||
| 162 | other paths pointing to that object. It will then remove any property |
||
| 163 | for this object from all property tables. |
||
| 164 | The function returns the number of paths found and removed or 1 if |
||
| 165 | there was no path found (meaning that the object doesn't exist and |
||
| 166 | therefor purge succeeded while doing nothing.) |
||
| 167 | |||
| 168 | $path should always start and end with a '/'. |
||
| 169 | **********************************************************************/ |
||
| 170 | |||
| 171 | public abstract function delete($path); |
||
| 172 | /********************************************************************** |
||
| 173 | This function deletes the path given. If this is the last path pointing |
||
| 174 | to an object, the object will be purged instead. |
||
| 175 | |||
| 176 | $path should always start and end with a '/'. |
||
| 177 | **********************************************************************/ |
||
| 178 | |||
| 179 | abstract function exists($path); |
||
| 180 | /********************************************************************** |
||
| 181 | This function checks the given path to see if it exists. If it does |
||
| 182 | it returns the id of the object to which it points. Otherwise it returns |
||
| 183 | 0. |
||
| 184 | |||
| 185 | $path should always start and end with a '/'. |
||
| 186 | **********************************************************************/ |
||
| 187 | |||
| 188 | |||
| 189 | public abstract function link($source, $destination); |
||
| 190 | /********************************************************************** |
||
| 191 | Link adds an extra path to an already existing object. It has two |
||
| 192 | arguments: $source and $destination. $source is an existing path of |
||
| 193 | an object, $destination is the new path. $destination must not already |
||
| 194 | exist. |
||
| 195 | |||
| 196 | $destination should always start and end with a '/'. |
||
| 197 | **********************************************************************/ |
||
| 198 | |||
| 199 | public abstract function move($source, $destination); |
||
| 200 | /********************************************************************** |
||
| 201 | $destination should always start and end with a '/'. |
||
| 202 | **********************************************************************/ |
||
| 203 | |||
| 204 | |||
| 205 | public abstract function list_paths($path); |
||
| 206 | /********************************************************************** |
||
| 207 | This function returns an array of all paths pointing to the same object |
||
| 208 | as $path does. |
||
| 209 | **********************************************************************/ |
||
| 210 | |||
| 211 | public abstract function AR_implements($type, $implements); |
||
| 212 | /********************************************************************** |
||
| 213 | This function returns 1 if the $type implements the type or |
||
| 214 | interface in $implements. Otherwise it returns 0. |
||
| 215 | **********************************************************************/ |
||
| 216 | |||
| 217 | public abstract function load_properties($object, $values=""); |
||
| 218 | |||
| 219 | public abstract function load_property($object, $property, $values=""); |
||
| 220 | |||
| 221 | public abstract function add_property($object, $property, $values); |
||
| 222 | |||
| 223 | public abstract function del_property($object, $property="", $values=""); |
||
| 224 | |||
| 225 | protected abstract function get_nextid($path, $mask="{5:id}"); |
||
| 226 | /********************************************************************** |
||
| 227 | 'private' function of mysql store. This will return the next |
||
| 228 | 'autoid' for $path. |
||
| 229 | **********************************************************************/ |
||
| 230 | |||
| 231 | |||
| 232 | |||
| 233 | /* |
||
| 234 | Implemented functions |
||
| 235 | */ |
||
| 236 | |||
| 237 | 184 | public function get_config($field) { |
|
| 252 | |||
| 253 | 60 | public function is_supported($feature) { |
|
| 277 | |||
| 278 | /********************************************************************************** |
||
| 279 | This functions creates a new ariadne object |
||
| 280 | **********************************************************************************/ |
||
| 281 | 184 | public function newobject($path, $parent, $type, $data, $id=0, $lastchanged=0, $vtype="", $size=0, $priority=0) { |
|
| 303 | |||
| 304 | 104 | public function close() { |
|
| 312 | |||
| 313 | 104 | public function __destruct() { |
|
| 316 | |||
| 317 | 112 | public function make_path($curr_dir, $path) { |
|
| 318 | /********************************************************************** |
||
| 319 | This function creates an absolute path from the given starting path |
||
| 320 | ($curr_dir) and a relative (or absolute) path ($path). If $path starts |
||
| 321 | with a '/' $curr_dir is ignored. |
||
| 322 | $path must be a string of substrings seperated by '/'. each of these |
||
| 323 | substrings may consist of charachters and/or numbers. If a substring |
||
| 324 | is "..", it and the previuos substring will be removed. If a substring |
||
| 325 | is "." or "", it is removed. All other substrings are then concatenated |
||
| 326 | with a '/' between them and at the start and the end. This string is |
||
| 327 | then returned. |
||
| 328 | **********************************************************************/ |
||
| 329 | 112 | $this->error = ""; |
|
| 330 | 112 | $result = "/"; |
|
| 331 | 112 | if ($path[0] !== "/") { |
|
| 332 | 88 | $path = $curr_dir . '/' . $path; |
|
| 333 | 66 | } |
|
| 334 | |||
| 335 | 112 | $splitpath = preg_split('|/|', $path, -1, PREG_SPLIT_NO_EMPTY); |
|
| 336 | |||
| 337 | 112 | foreach ($splitpath as $pathticle) { |
|
| 338 | 112 | if($pathticle === '..' ) { |
|
| 339 | 84 | $result = dirname($result); |
|
| 340 | // if second char of $result is not set, then current result is the rootNode |
||
| 341 | 84 | if (isset($result[1])) { |
|
| 342 | 84 | $result = $result . "/"; |
|
| 343 | 63 | } else { |
|
| 344 | 60 | $result = "/"; // make sure that even under windows, slashes are always forward slashes. |
|
| 345 | } |
||
| 346 | 112 | } else if ($pathticle !== '.') { |
|
| 347 | 112 | $result = $result . $pathticle."/"; |
|
| 348 | 84 | } |
|
| 349 | 84 | } |
|
| 350 | 112 | return $result; |
|
| 351 | } |
||
| 352 | |||
| 353 | 68 | public function save_properties($properties, $id) { |
|
| 354 | /******************************************************************** |
||
| 355 | 'private' function of mysql.phtml. It updates all property tables |
||
| 356 | defined in $properties and sets the values to the values in |
||
| 357 | $properties. |
||
| 358 | ********************************************************************/ |
||
| 359 | |||
| 360 | 68 | if ($properties && (is_array($properties)) && (is_numeric($id))) { |
|
| 361 | 48 | foreach ( $properties as $property => $property_set ) { |
|
| 362 | 48 | $this->del_property((int)$id, $property); |
|
| 363 | 48 | if (is_array($property_set)) { |
|
| 364 | 48 | foreach ( $property_set as $values ) { |
|
| 365 | 48 | $this->add_property((int)$id, $property, $values); |
|
| 366 | 36 | } |
|
| 367 | 36 | } |
|
| 368 | 36 | } |
|
| 369 | 36 | } |
|
| 370 | 68 | } |
|
| 371 | |||
| 372 | |||
| 373 | 104 | public function get_filestore($name) { |
|
| 386 | |||
| 387 | 28 | public function get_filestore_svn($name) { |
|
| 388 | 28 | require_once($this->code."modules/mod_filestore_svn.phtml"); |
|
| 394 | |||
| 395 | 188 | protected function compilerFactory(){ |
|
| 404 | |||
| 405 | 34 | protected function serialize($value, $path) { |
|
| 440 | |||
| 441 | 184 | protected function unserialize($value, $path) { |
|
| 472 | |||
| 473 | } |
||
| 474 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.