Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Table 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 Table, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Table implements ArrayAccess |
||
| 12 | { |
||
| 13 | private $db; |
||
| 14 | private $row; |
||
| 15 | private $rowCollection; |
||
| 16 | private $cache = []; |
||
| 17 | |||
| 18 | public $name; |
||
| 19 | public $fields = []; |
||
| 20 | public $queriesModifiers = []; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Constructor. |
||
| 24 | * |
||
| 25 | * @param SimpleCrud $db |
||
| 26 | * @param string $name |
||
| 27 | */ |
||
| 28 | final public function __construct(SimpleCrud $db, $name) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Debug info. |
||
| 47 | * |
||
| 48 | * @return array |
||
| 49 | */ |
||
| 50 | public function __debugInfo() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Callback used to init the table. |
||
| 60 | */ |
||
| 61 | protected function init() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Store a row in the cache. |
||
| 67 | * |
||
| 68 | * @param Row $row |
||
| 69 | */ |
||
| 70 | public function cache(Row $row) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Clear the current cache. |
||
| 77 | */ |
||
| 78 | public function clearCache() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Register a new query modifier. |
||
| 85 | * |
||
| 86 | * @param string $name |
||
| 87 | * @param callable $modifier |
||
| 88 | */ |
||
| 89 | public function addQueryModifier($name, callable $modifier) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Magic method to create queries related with this table. |
||
| 100 | * |
||
| 101 | * @param string $name |
||
| 102 | * @param array $arguments |
||
| 103 | * |
||
| 104 | * @throws SimpleCrudException |
||
| 105 | * |
||
| 106 | * @return Queries\Query|null |
||
| 107 | */ |
||
| 108 | public function __call($name, $arguments) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Check if a row with a specific id exists. |
||
| 123 | * |
||
| 124 | * @see ArrayAccess |
||
| 125 | * |
||
| 126 | * @return bool |
||
| 127 | */ |
||
| 128 | public function offsetExists($offset) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Returns a row with a specific id. |
||
| 142 | * |
||
| 143 | * @see ArrayAccess |
||
| 144 | * |
||
| 145 | * @return Row|null |
||
| 146 | */ |
||
| 147 | public function offsetGet($offset) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Store a row with a specific id. |
||
| 161 | * |
||
| 162 | * @see ArrayAccess |
||
| 163 | */ |
||
| 164 | public function offsetSet($offset, $value) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Remove a row with a specific id. |
||
| 202 | * |
||
| 203 | * @see ArrayAccess |
||
| 204 | */ |
||
| 205 | public function offsetUnset($offset) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Returns the SimpleCrud instance associated with this table. |
||
| 217 | * |
||
| 218 | * @return SimpleCrud |
||
| 219 | */ |
||
| 220 | public function getDatabase() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Returns the table scheme. |
||
| 227 | * |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | public function getScheme() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Returns an attribute. |
||
| 237 | * |
||
| 238 | * @param string $name |
||
| 239 | * |
||
| 240 | * @return null|mixed |
||
| 241 | */ |
||
| 242 | public function getAttribute($name) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Defines the Row class used by this table. |
||
| 249 | * |
||
| 250 | * @param Row $row |
||
| 251 | */ |
||
| 252 | public function setRow(Row $row) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Register a custom method to the row. |
||
| 259 | * |
||
| 260 | * @param string $name |
||
| 261 | * @param Closure $method |
||
| 262 | * |
||
| 263 | * @return self |
||
| 264 | */ |
||
| 265 | public function setRowMethod($name, Closure $method) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Defines the RowCollection class used by this table. |
||
| 274 | * |
||
| 275 | * @param RowCollection $rowCollection |
||
| 276 | */ |
||
| 277 | public function setRowCollection(RowCollection $rowCollection) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Register a custom method to the rowCollections. |
||
| 284 | * |
||
| 285 | * @param string $name |
||
| 286 | * @param Closure $method |
||
| 287 | * |
||
| 288 | * @return self |
||
| 289 | */ |
||
| 290 | public function setRowCollectionMethod($name, Closure $method) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Creates a new row instance. |
||
| 299 | * |
||
| 300 | * @param array $data The values of the row |
||
| 301 | * |
||
| 302 | * @return Row |
||
| 303 | */ |
||
| 304 | public function create(array $data = []) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Creates a new rowCollection instance. |
||
| 321 | * |
||
| 322 | * @param array $data Rows added to this collection |
||
| 323 | * |
||
| 324 | * @return RowCollection |
||
| 325 | */ |
||
| 326 | public function createCollection(array $data = []) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Default data converter/validator from database. |
||
| 339 | * |
||
| 340 | * @param array $data The values before insert to database |
||
| 341 | * @param bool $new True for inserts, false for updates |
||
| 342 | */ |
||
| 343 | public function dataToDatabase(array $data, $new) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Default data converter from database. |
||
| 350 | * |
||
| 351 | * @param array $data The database format values |
||
| 352 | */ |
||
| 353 | public function dataFromDatabase(array $data) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Prepares the data from the result of a database selection. |
||
| 360 | * |
||
| 361 | * @param array $data |
||
| 362 | * |
||
| 363 | * @return array |
||
| 364 | */ |
||
| 365 | public function createFromDatabase(array $data) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Prepares the data before save into database (used by update and insert). |
||
| 389 | * |
||
| 390 | * @param array $data |
||
| 391 | * @param bool $new |
||
| 392 | * |
||
| 393 | * @return array |
||
| 394 | */ |
||
| 395 | public function prepareDataToDatabase(array $data, $new) |
||
| 412 | } |
||
| 413 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: