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 |
||
| 13 | class Table implements ArrayAccess |
||
| 14 | { |
||
| 15 | private $name; |
||
| 16 | private $db; |
||
| 17 | private $row; |
||
| 18 | private $rowCollection; |
||
| 19 | private $cache = []; |
||
| 20 | private $queriesModifiers = []; |
||
| 21 | private $fields = []; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Constructor. |
||
| 25 | * |
||
| 26 | * @param SimpleCrud $db |
||
| 27 | * @param string $name |
||
| 28 | */ |
||
| 29 | final public function __construct(SimpleCrud $db, $name) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Debug info. |
||
| 48 | * |
||
| 49 | * @return array |
||
| 50 | */ |
||
| 51 | public function __debugInfo() |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Callback used to init the table. |
||
| 61 | */ |
||
| 62 | protected function init() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Store a row in the cache. |
||
| 68 | * |
||
| 69 | * @param Row $row |
||
| 70 | */ |
||
| 71 | public function cache(Row $row) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Clear the current cache. |
||
| 78 | */ |
||
| 79 | public function clearCache() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Register a new query modifier. |
||
| 86 | * |
||
| 87 | * @param string $name |
||
| 88 | * @param callable $modifier |
||
| 89 | */ |
||
| 90 | public function addQueryModifier($name, callable $modifier) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Magic method to create queries related with this table. |
||
| 101 | * |
||
| 102 | * @param string $name |
||
| 103 | * @param array $arguments |
||
| 104 | * |
||
| 105 | * @throws SimpleCrudException |
||
| 106 | * |
||
| 107 | * @return Queries\Query|null |
||
| 108 | */ |
||
| 109 | public function __call($name, $arguments) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Magic method to get the Field instance of a table field |
||
| 124 | * |
||
| 125 | * @param string $name The field name |
||
| 126 | * |
||
| 127 | * @throws SimpleCrudException |
||
| 128 | * |
||
| 129 | * @return Fields\Field |
||
| 130 | */ |
||
| 131 | public function __get($name) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Magic method to check if a field exists or not. |
||
| 142 | * |
||
| 143 | * @param string $name |
||
| 144 | * |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | public function __isset($name) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Check if a row with a specific id exists. |
||
| 154 | * |
||
| 155 | * @see ArrayAccess |
||
| 156 | * |
||
| 157 | * @return bool |
||
| 158 | */ |
||
| 159 | public function offsetExists($offset) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Returns a row with a specific id. |
||
| 173 | * |
||
| 174 | * @see ArrayAccess |
||
| 175 | * |
||
| 176 | * @return Row|null |
||
| 177 | */ |
||
| 178 | public function offsetGet($offset) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Store a row with a specific id. |
||
| 192 | * |
||
| 193 | * @see ArrayAccess |
||
| 194 | */ |
||
| 195 | public function offsetSet($offset, $value) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Remove a row with a specific id. |
||
| 233 | * |
||
| 234 | * @see ArrayAccess |
||
| 235 | */ |
||
| 236 | public function offsetUnset($offset) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Returns the table name. |
||
| 248 | * |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | public function getName() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Returns the SimpleCrud instance associated with this table. |
||
| 258 | * |
||
| 259 | * @return SimpleCrud |
||
| 260 | */ |
||
| 261 | public function getDatabase() |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Returns all fields. |
||
| 268 | * |
||
| 269 | * @return Fields\Field[] |
||
| 270 | */ |
||
| 271 | public function getFields() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Returns the table scheme. |
||
| 278 | * |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | public function getScheme() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Returns an attribute. |
||
| 288 | * |
||
| 289 | * @param string $name |
||
| 290 | * |
||
| 291 | * @return null|mixed |
||
| 292 | */ |
||
| 293 | public function getAttribute($name) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Defines the Row class used by this table. |
||
| 300 | * |
||
| 301 | * @param Row $row |
||
| 302 | */ |
||
| 303 | public function setRow(Row $row) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Register a custom method to the row. |
||
| 310 | * |
||
| 311 | * @param string $name |
||
| 312 | * @param Closure $method |
||
| 313 | * |
||
| 314 | * @return self |
||
| 315 | */ |
||
| 316 | public function setRowMethod($name, Closure $method) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Defines the RowCollection class used by this table. |
||
| 325 | * |
||
| 326 | * @param RowCollection $rowCollection |
||
| 327 | */ |
||
| 328 | public function setRowCollection(RowCollection $rowCollection) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Register a custom method to the rowCollections. |
||
| 335 | * |
||
| 336 | * @param string $name |
||
| 337 | * @param Closure $method |
||
| 338 | * |
||
| 339 | * @return self |
||
| 340 | */ |
||
| 341 | public function setRowCollectionMethod($name, Closure $method) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Creates a new row instance. |
||
| 350 | * |
||
| 351 | * @param array $data The values of the row |
||
| 352 | * |
||
| 353 | * @return Row |
||
| 354 | */ |
||
| 355 | public function create(array $data = []) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Creates a new rowCollection instance. |
||
| 372 | * |
||
| 373 | * @param array $data Rows added to this collection |
||
| 374 | * |
||
| 375 | * @return RowCollection |
||
| 376 | */ |
||
| 377 | public function createCollection(array $data = []) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Default data converter/validator from database. |
||
| 390 | * |
||
| 391 | * @param array $data The values before insert to database |
||
| 392 | * @param bool $new True for inserts, false for updates |
||
| 393 | */ |
||
| 394 | public function dataToDatabase(array $data, $new) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Default data converter from database. |
||
| 401 | * |
||
| 402 | * @param array $data The database format values |
||
| 403 | */ |
||
| 404 | public function dataFromDatabase(array $data) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Prepares the data from the result of a database selection. |
||
| 411 | * |
||
| 412 | * @param array $data |
||
| 413 | * |
||
| 414 | * @return Row |
||
| 415 | */ |
||
| 416 | public function createFromDatabase(array $data) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Prepares the data before save into database (used by update and insert). |
||
| 440 | * |
||
| 441 | * @param array $data |
||
| 442 | * @param bool $new |
||
| 443 | * |
||
| 444 | * @return array |
||
| 445 | */ |
||
| 446 | public function prepareDataToDatabase(array $data, $new) |
||
| 463 | } |
||
| 464 |
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: