Complex classes like SimpleCrud 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 SimpleCrud, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class SimpleCrud |
||
| 9 | { |
||
| 10 | protected $connection; |
||
| 11 | protected $scheme; |
||
| 12 | protected $tables = []; |
||
| 13 | protected $inTransaction = false; |
||
| 14 | protected $attributes = []; |
||
| 15 | |||
| 16 | protected $tableFactory; |
||
| 17 | protected $queryFactory; |
||
| 18 | protected $fieldFactory; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Set the connection and the tableFactory. |
||
| 22 | * |
||
| 23 | * @param PDO $connection |
||
| 24 | */ |
||
| 25 | public function __construct(PDO $connection) |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Set the database scheme. |
||
| 33 | * |
||
| 34 | * @param array $scheme |
||
| 35 | * |
||
| 36 | * @return self |
||
| 37 | */ |
||
| 38 | public function setScheme(array $scheme) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Returns the database scheme. |
||
| 47 | * |
||
| 48 | * @return array |
||
| 49 | */ |
||
| 50 | public function getScheme() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Set the TableFactory instance used to create all tables. |
||
| 69 | * |
||
| 70 | * @param TableFactory $tableFactory |
||
| 71 | * |
||
| 72 | * @return self |
||
| 73 | */ |
||
| 74 | public function setTableFactory(TableFactory $tableFactory) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Returns the TableFactory instance used. |
||
| 83 | * |
||
| 84 | * @return TableFactory |
||
| 85 | */ |
||
| 86 | public function getTableFactory() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Set the QueryFactory instance used by the tables. |
||
| 97 | * |
||
| 98 | * @param QueryFactory $queryFactory |
||
| 99 | * |
||
| 100 | * @return self |
||
| 101 | */ |
||
| 102 | public function setQueryFactory(QueryFactory $queryFactory) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Returns the QueryFactory instance used by the tables. |
||
| 111 | * |
||
| 112 | * @return QueryFactory |
||
| 113 | */ |
||
| 114 | public function getQueryFactory() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Set the FieldFactory instance used by the tables. |
||
| 128 | * |
||
| 129 | * @param FieldFactory $fieldFactory |
||
| 130 | * |
||
| 131 | * @return self |
||
| 132 | */ |
||
| 133 | public function setFieldFactory(FieldFactory $fieldFactory) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Returns the FieldFactory instance used by the tables. |
||
| 142 | * |
||
| 143 | * @return FieldFactory |
||
| 144 | */ |
||
| 145 | public function getFieldFactory() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Magic method to initialize the tables in lazy mode. |
||
| 156 | * |
||
| 157 | * @param string $name The table name |
||
| 158 | * |
||
| 159 | * @throws SimpleCrudException If the table cannot be instantiated |
||
| 160 | * |
||
| 161 | * @return Table |
||
| 162 | */ |
||
| 163 | public function __get($name) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Magic method to check if a table exists or not. |
||
| 174 | * |
||
| 175 | * @param string $name |
||
| 176 | * |
||
| 177 | * @return bool |
||
| 178 | */ |
||
| 179 | public function __isset($name) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Execute a query and returns the statement object with the result. |
||
| 186 | * |
||
| 187 | * @param string $query The Mysql query to execute |
||
| 188 | * @param array $marks The marks passed to the statement |
||
| 189 | * |
||
| 190 | * @throws Exception On error preparing or executing the statement |
||
| 191 | * |
||
| 192 | * @return PDOStatement The result |
||
| 193 | */ |
||
| 194 | public function execute($query, array $marks = null) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Execute a callable inside a transaction. |
||
| 223 | * |
||
| 224 | * @param callable $callable The function with all operations |
||
| 225 | * |
||
| 226 | * @return mixed The callable returned value |
||
| 227 | */ |
||
| 228 | public function executeTransaction(callable $callable) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Returns the last insert id. |
||
| 251 | * |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | public function lastInsertId() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Starts a transaction if it's not started yet. |
||
| 261 | * |
||
| 262 | * @return bool True if a the transaction is started or false if don't |
||
| 263 | */ |
||
| 264 | public function beginTransaction() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Commits the changes of the transaction to the database. |
||
| 277 | */ |
||
| 278 | public function commit() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * RollBack a transaction. |
||
| 288 | */ |
||
| 289 | public function rollBack() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Check if there is a transaction opened currently in this adapter. |
||
| 299 | */ |
||
| 300 | public function inTransaction() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Saves a new attribute. |
||
| 307 | * |
||
| 308 | * @param string $name |
||
| 309 | * @param mixed $value |
||
| 310 | * |
||
| 311 | * @return $this |
||
| 312 | */ |
||
| 313 | public function setAttribute($name, $value) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Returns an attribute. |
||
| 322 | * |
||
| 323 | * @param string|int $name |
||
| 324 | * |
||
| 325 | * @return null|mixed |
||
| 326 | */ |
||
| 327 | public function getAttribute($name) |
||
| 335 | } |
||
| 336 |