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 |
||
| 10 | class Table implements ArrayAccess |
||
| 11 | { |
||
| 12 | private $db; |
||
| 13 | private $row; |
||
| 14 | private $collection; |
||
| 15 | private $cache = []; |
||
| 16 | |||
| 17 | public $name; |
||
| 18 | public $fields = []; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Constructor. |
||
| 22 | * |
||
| 23 | * @param SimpleCrud $db |
||
| 24 | * @param string $name |
||
| 25 | */ |
||
| 26 | final public function __construct(SimpleCrud $db, $name) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Debug info. |
||
| 45 | * |
||
| 46 | * @return array |
||
| 47 | */ |
||
| 48 | public function __debugInfo() |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Callback used to init the table. |
||
| 58 | */ |
||
| 59 | protected function init() |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Store a row in the cache. |
||
| 65 | * |
||
| 66 | * @param int $id |
||
|
|
|||
| 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 | * Magic method to create queries related with this table. |
||
| 84 | * |
||
| 85 | * @param string $name |
||
| 86 | * @param array $arguments |
||
| 87 | * |
||
| 88 | * @throws SimpleCrudException |
||
| 89 | * |
||
| 90 | * @return Queries\Query|null |
||
| 91 | */ |
||
| 92 | public function __call($name, $arguments) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Check if a row with a specific id exists. |
||
| 99 | * |
||
| 100 | * @see ArrayAccess |
||
| 101 | * |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | public function offsetExists($offset) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Returns a row with a specific id. |
||
| 118 | * |
||
| 119 | * @see ArrayAccess |
||
| 120 | * |
||
| 121 | * @return Row|null |
||
| 122 | */ |
||
| 123 | public function offsetGet($offset) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Store a row with a specific id. |
||
| 137 | * |
||
| 138 | * @see ArrayAccess |
||
| 139 | */ |
||
| 140 | public function offsetSet($offset, $value) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Remove a row with a specific id. |
||
| 178 | * |
||
| 179 | * @see ArrayAccess |
||
| 180 | */ |
||
| 181 | public function offsetUnset($offset) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Returns the SimpleCrud instance associated with this table. |
||
| 193 | * |
||
| 194 | * @return SimpleCrud |
||
| 195 | */ |
||
| 196 | public function getDatabase() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Returns the table scheme. |
||
| 203 | * |
||
| 204 | * @return array |
||
| 205 | */ |
||
| 206 | public function getScheme() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Returns an attribute. |
||
| 213 | * |
||
| 214 | * @param string $name |
||
| 215 | * |
||
| 216 | * @return null|mixed |
||
| 217 | */ |
||
| 218 | public function getAttribute($name) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Defines the Row class used by this table. |
||
| 225 | * |
||
| 226 | * @param Row $row |
||
| 227 | */ |
||
| 228 | public function setRow(Row $row) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Defines the RowCollection class used by this table. |
||
| 235 | * |
||
| 236 | * @param RowCollection $collection |
||
| 237 | */ |
||
| 238 | public function setCollection(RowCollection $collection) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Creates a new row instance. |
||
| 245 | * |
||
| 246 | * @param array $data The values of the row |
||
| 247 | * |
||
| 248 | * @return Row |
||
| 249 | */ |
||
| 250 | public function create(array $data = []) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Creates a new rowCollection instance. |
||
| 267 | * |
||
| 268 | * @param array $data Rows added to this collection |
||
| 269 | * |
||
| 270 | * @return RowCollection |
||
| 271 | */ |
||
| 272 | public function createCollection(array $data = []) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Default data converter/validator from database. |
||
| 285 | * |
||
| 286 | * @param array $data The values before insert to database |
||
| 287 | * @param bool $new True for inserts, false for updates |
||
| 288 | */ |
||
| 289 | public function dataToDatabase(array $data, $new) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Default data converter from database. |
||
| 296 | * |
||
| 297 | * @param array $data The database format values |
||
| 298 | */ |
||
| 299 | public function dataFromDatabase(array $data) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Prepares the data from the result of a database selection. |
||
| 306 | * |
||
| 307 | * @param array $data |
||
| 308 | * |
||
| 309 | * @return array |
||
| 310 | */ |
||
| 311 | public function createFromDatabase(array $data) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Prepares the data before save into database (used by update and insert). |
||
| 335 | * |
||
| 336 | * @param array $data |
||
| 337 | * @param bool $new |
||
| 338 | * |
||
| 339 | * @return array |
||
| 340 | */ |
||
| 341 | public function prepareDataToDatabase(array $data, $new) |
||
| 358 | } |
||
| 359 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.