Complex classes like FlexibleEntity 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 FlexibleEntity, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | abstract class FlexibleEntity extends FlexibleContainer implements \ArrayAccess |
||
| 29 | { |
||
| 30 | public static $strict = true; |
||
| 31 | protected static $has_methods; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * __construct |
||
| 35 | * |
||
| 36 | * Instantiate the entity and hydrate it with the given values. |
||
| 37 | * |
||
| 38 | * @access public |
||
| 39 | * @param array $values Optional starting values. |
||
| 40 | */ |
||
| 41 | public function __construct(array $values = null) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * get |
||
| 50 | * |
||
| 51 | * Returns the $var value |
||
| 52 | * |
||
| 53 | * @final |
||
| 54 | * @access public |
||
| 55 | * @param string|array $var Key(s) you want to retrieve value from. |
||
| 56 | * @throws ModelException if strict and the attribute does not exist. |
||
| 57 | * @return mixed |
||
| 58 | */ |
||
| 59 | final public function get($var) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * has |
||
| 74 | * |
||
| 75 | * Returns true if the given key exists. |
||
| 76 | * |
||
| 77 | * @final |
||
| 78 | * @access public |
||
| 79 | * @param string $var |
||
| 80 | * @return boolean |
||
| 81 | */ |
||
| 82 | final public function has($var) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * set |
||
| 89 | * |
||
| 90 | * Set a value in the var holder. |
||
| 91 | * |
||
| 92 | * @final |
||
| 93 | * @access public |
||
| 94 | * @param String $var Attribute name. |
||
| 95 | * @param Mixed $value Attribute value. |
||
| 96 | * @return FlexibleEntity $this |
||
| 97 | */ |
||
| 98 | final public function set($var, $value) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * add |
||
| 109 | * |
||
| 110 | * When the corresponding attribute is an array, call this method |
||
| 111 | * to set values. |
||
| 112 | * |
||
| 113 | * @access public |
||
| 114 | * @param string $var |
||
| 115 | * @param mixed $value |
||
| 116 | * @return FlexibleEntity $this |
||
| 117 | * @throws ModelException |
||
| 118 | */ |
||
| 119 | public function add($var, $value) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * clear |
||
| 138 | * |
||
| 139 | * Drop an attribute from the var holder. |
||
| 140 | * |
||
| 141 | * @final |
||
| 142 | * @access public |
||
| 143 | * @param String $offset Attribute name. |
||
| 144 | * @return FlexibleEntity $this |
||
| 145 | */ |
||
| 146 | final public function clear($offset) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * __call |
||
| 159 | * |
||
| 160 | * Allows dynamic methods getXxx, setXxx, hasXxx, addXxx or clearXxx. |
||
| 161 | * |
||
| 162 | * @access public |
||
| 163 | * @throws ModelException if method does not exist. |
||
| 164 | * @param mixed $method |
||
| 165 | * @param mixed $arguments |
||
| 166 | * @return mixed |
||
| 167 | */ |
||
| 168 | public function __call($method, $arguments) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * convert |
||
| 190 | * |
||
| 191 | * Make all keys lowercase and hydrate the object. |
||
| 192 | * |
||
| 193 | * @access public |
||
| 194 | * @param Array $values |
||
| 195 | * @return FlexibleEntity |
||
| 196 | */ |
||
| 197 | public function convert(array $values) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * extract |
||
| 210 | * |
||
| 211 | * Returns the fields flatten as arrays. |
||
| 212 | * |
||
| 213 | * The complex stuff in here is when there is an array, since all elements |
||
| 214 | * in arrays are the same type, we check only its first value to know if we need |
||
| 215 | * to traverse it or not. |
||
| 216 | * |
||
| 217 | * @see FlexibleEntityInterface |
||
| 218 | */ |
||
| 219 | public function extract() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * getCustomFields |
||
| 247 | * |
||
| 248 | * Return a list of custom methods with has() accessor. |
||
| 249 | * |
||
| 250 | * @access private |
||
| 251 | * @return array |
||
| 252 | */ |
||
| 253 | private function getCustomFields() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * getIterator |
||
| 272 | * |
||
| 273 | * @see FlexibleEntityInterface |
||
| 274 | */ |
||
| 275 | public function getIterator() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * __set |
||
| 282 | * |
||
| 283 | * PHP magic to set attributes. |
||
| 284 | * |
||
| 285 | * @access public |
||
| 286 | * @param String $var Attribute name. |
||
| 287 | * @param Mixed $value Attribute value. |
||
| 288 | * @return FlexibleEntity $this |
||
| 289 | */ |
||
| 290 | public function __set($var, $value) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * __get |
||
| 300 | * |
||
| 301 | * PHP magic to get attributes. |
||
| 302 | * |
||
| 303 | * @access public |
||
| 304 | * @param String $var Attribute name. |
||
| 305 | * @return Mixed Attribute value. |
||
| 306 | */ |
||
| 307 | public function __get($var) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * __isset |
||
| 316 | * |
||
| 317 | * Easy value check. |
||
| 318 | * |
||
| 319 | * @access public |
||
| 320 | * @param string $var |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | public function __isset($var) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * __unset |
||
| 332 | * |
||
| 333 | * Clear an attribute. |
||
| 334 | * |
||
| 335 | * @access public |
||
| 336 | * @param string $var |
||
| 337 | * @return FlexibleEntity $this |
||
| 338 | */ |
||
| 339 | public function __unset($var) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * {@inheritdoc} |
||
| 348 | */ |
||
| 349 | public function offsetExists($offset) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * {@inheritdoc} |
||
| 358 | */ |
||
| 359 | public function offsetSet($offset, $value) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * {@inheritdoc} |
||
| 366 | */ |
||
| 367 | public function offsetGet($offset) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * {@inheritdoc} |
||
| 374 | */ |
||
| 375 | public function offsetUnset($offset) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * fillHasMethods |
||
| 382 | * |
||
| 383 | * When getIterator is called the first time, the list of "has" methods is |
||
| 384 | * set in a static attribute to boost performances. |
||
| 385 | * |
||
| 386 | * @access protected |
||
| 387 | * @param FlexibleEntity $entity |
||
| 388 | * @return null |
||
| 389 | */ |
||
| 390 | protected static function fillHasMethods(FlexibleEntity $entity) |
||
| 400 | } |
||
| 401 |