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 $name; |
||
| 14 | private $db; |
||
| 15 | private $row; |
||
| 16 | private $rowCollection; |
||
| 17 | private $cache = []; |
||
| 18 | private $queriesModifiers = []; |
||
| 19 | private $fields = []; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Constructor. |
||
| 23 | * |
||
| 24 | * @param SimpleCrud $db |
||
| 25 | * @param string $name |
||
| 26 | */ |
||
| 27 | final public function __construct(SimpleCrud $db, $name) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Debug info. |
||
| 46 | * |
||
| 47 | * @return array |
||
| 48 | */ |
||
| 49 | public function __debugInfo() |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Callback used to init the table. |
||
| 59 | */ |
||
| 60 | protected function init() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Store a row in the cache. |
||
| 66 | * |
||
| 67 | * @param Row $row |
||
| 68 | */ |
||
| 69 | public function cache(Row $row) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Clear the current cache. |
||
| 76 | */ |
||
| 77 | public function clearCache() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Register a new query modifier. |
||
| 84 | * |
||
| 85 | * @param string $name |
||
| 86 | * @param callable $modifier |
||
| 87 | */ |
||
| 88 | public function addQueryModifier($name, callable $modifier) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Magic method to create queries related with this table. |
||
| 99 | * |
||
| 100 | * @param string $name |
||
| 101 | * @param array $arguments |
||
| 102 | * |
||
| 103 | * @throws SimpleCrudException |
||
| 104 | * |
||
| 105 | * @return Queries\Query|null |
||
| 106 | */ |
||
| 107 | public function __call($name, $arguments) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Magic method to get the Field instance of a table field |
||
| 122 | * |
||
| 123 | * @param string $name The field name |
||
| 124 | * |
||
| 125 | * @throws SimpleCrudException |
||
| 126 | * |
||
| 127 | * @return Fields\Field |
||
| 128 | */ |
||
| 129 | public function __get($name) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Magic method to check if a field exists or not. |
||
| 140 | * |
||
| 141 | * @param string $name |
||
| 142 | * |
||
| 143 | * @return bool |
||
| 144 | */ |
||
| 145 | public function __isset($name) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Check if a row with a specific id exists. |
||
| 152 | * |
||
| 153 | * @see ArrayAccess |
||
| 154 | * |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | public function offsetExists($offset) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Returns a row with a specific id. |
||
| 171 | * |
||
| 172 | * @see ArrayAccess |
||
| 173 | * |
||
| 174 | * @return Row|null |
||
| 175 | */ |
||
| 176 | public function offsetGet($offset) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Store a row with a specific id. |
||
| 190 | * |
||
| 191 | * @see ArrayAccess |
||
| 192 | */ |
||
| 193 | public function offsetSet($offset, $value) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Remove a row with a specific id. |
||
| 231 | * |
||
| 232 | * @see ArrayAccess |
||
| 233 | */ |
||
| 234 | public function offsetUnset($offset) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Returns the table name. |
||
| 246 | * |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | public function getName() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Returns the SimpleCrud instance associated with this table. |
||
| 256 | * |
||
| 257 | * @return SimpleCrud |
||
| 258 | */ |
||
| 259 | public function getDatabase() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Returns the table scheme. |
||
| 266 | * |
||
| 267 | * @return array |
||
| 268 | */ |
||
| 269 | public function getScheme() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Returns an attribute. |
||
| 276 | * |
||
| 277 | * @param string $name |
||
| 278 | * |
||
| 279 | * @return null|mixed |
||
| 280 | */ |
||
| 281 | public function getAttribute($name) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Defines the Row class used by this table. |
||
| 288 | * |
||
| 289 | * @param Row $row |
||
| 290 | */ |
||
| 291 | public function setRow(Row $row) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Register a custom method to the row. |
||
| 298 | * |
||
| 299 | * @param string $name |
||
| 300 | * @param Closure $method |
||
| 301 | * |
||
| 302 | * @return self |
||
| 303 | */ |
||
| 304 | public function setRowMethod($name, Closure $method) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Defines the RowCollection class used by this table. |
||
| 313 | * |
||
| 314 | * @param RowCollection $rowCollection |
||
| 315 | */ |
||
| 316 | public function setRowCollection(RowCollection $rowCollection) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Register a custom method to the rowCollections. |
||
| 323 | * |
||
| 324 | * @param string $name |
||
| 325 | * @param Closure $method |
||
| 326 | * |
||
| 327 | * @return self |
||
| 328 | */ |
||
| 329 | public function setRowCollectionMethod($name, Closure $method) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Creates a new row instance. |
||
| 338 | * |
||
| 339 | * @param array $data The values of the row |
||
| 340 | * |
||
| 341 | * @return Row |
||
| 342 | */ |
||
| 343 | public function create(array $data = []) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Creates a new rowCollection instance. |
||
| 360 | * |
||
| 361 | * @param array $data Rows added to this collection |
||
| 362 | * |
||
| 363 | * @return RowCollection |
||
| 364 | */ |
||
| 365 | public function createCollection(array $data = []) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Default data converter/validator from database. |
||
| 378 | * |
||
| 379 | * @param array $data The values before insert to database |
||
| 380 | * @param bool $new True for inserts, false for updates |
||
| 381 | */ |
||
| 382 | public function dataToDatabase(array $data, $new) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Default data converter from database. |
||
| 389 | * |
||
| 390 | * @param array $data The database format values |
||
| 391 | */ |
||
| 392 | public function dataFromDatabase(array $data) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Prepares the data from the result of a database selection. |
||
| 399 | * |
||
| 400 | * @param array $data |
||
| 401 | * |
||
| 402 | * @return array |
||
| 403 | */ |
||
| 404 | public function createFromDatabase(array $data) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Prepares the data before save into database (used by update and insert). |
||
| 428 | * |
||
| 429 | * @param array $data |
||
| 430 | * @param bool $new |
||
| 431 | * |
||
| 432 | * @return array |
||
| 433 | */ |
||
| 434 | public function prepareDataToDatabase(array $data, $new) |
||
| 451 | } |
||
| 452 |
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: