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 | const ATTR_LOCALE = 'simplecrud.language'; |
||
| 11 | const ATTR_UPLOADS = 'simplecrud.uploads'; |
||
| 12 | |||
| 13 | protected $connection; |
||
| 14 | protected $scheme; |
||
| 15 | protected $tables = []; |
||
| 16 | protected $inTransaction = false; |
||
| 17 | protected $attributes = []; |
||
| 18 | protected $onExecute; |
||
| 19 | |||
| 20 | protected $tableFactory; |
||
| 21 | protected $queryFactory; |
||
| 22 | protected $fieldFactory; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Set the connection and the tableFactory. |
||
| 26 | * |
||
| 27 | * @param PDO $connection |
||
| 28 | */ |
||
| 29 | public function __construct(PDO $connection) |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Set the database scheme. |
||
| 37 | * |
||
| 38 | * @param array $scheme |
||
| 39 | * |
||
| 40 | * @return self |
||
| 41 | */ |
||
| 42 | public function setScheme(array $scheme) |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Returns the database scheme. |
||
| 51 | * |
||
| 52 | * @return array |
||
| 53 | */ |
||
| 54 | public function getScheme() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Define a callback executed for each query executed |
||
| 73 | * |
||
| 74 | * @param callable|null $callback |
||
| 75 | */ |
||
| 76 | public function onExecute(callable $callback = null) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Set the TableFactory instance used to create all tables. |
||
| 83 | * |
||
| 84 | * @param TableFactory $tableFactory |
||
| 85 | * |
||
| 86 | * @return self |
||
| 87 | */ |
||
| 88 | public function setTableFactory(TableFactory $tableFactory) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Returns the TableFactory instance used. |
||
| 97 | * |
||
| 98 | * @return TableFactory |
||
| 99 | */ |
||
| 100 | public function getTableFactory() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Set the QueryFactory instance used by the tables. |
||
| 111 | * |
||
| 112 | * @param QueryFactory $queryFactory |
||
| 113 | * |
||
| 114 | * @return self |
||
| 115 | */ |
||
| 116 | public function setQueryFactory(QueryFactory $queryFactory) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Returns the QueryFactory instance used by the tables. |
||
| 125 | * |
||
| 126 | * @return QueryFactory |
||
| 127 | */ |
||
| 128 | public function getQueryFactory() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Set the FieldFactory instance used by the tables. |
||
| 142 | * |
||
| 143 | * @param FieldFactory $fieldFactory |
||
| 144 | * |
||
| 145 | * @return self |
||
| 146 | */ |
||
| 147 | public function setFieldFactory(FieldFactory $fieldFactory) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Returns the FieldFactory instance used by the tables. |
||
| 156 | * |
||
| 157 | * @return FieldFactory |
||
| 158 | */ |
||
| 159 | public function getFieldFactory() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Magic method to initialize the tables in lazy mode. |
||
| 170 | * |
||
| 171 | * @param string $name The table name |
||
| 172 | * |
||
| 173 | * @throws SimpleCrudException If the table cannot be instantiated |
||
| 174 | * |
||
| 175 | * @return Table |
||
| 176 | */ |
||
| 177 | public function __get($name) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Magic method to check if a table exists or not. |
||
| 188 | * |
||
| 189 | * @param string $name |
||
| 190 | * |
||
| 191 | * @return bool |
||
| 192 | */ |
||
| 193 | public function __isset($name) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Execute a query and returns the statement object with the result. |
||
| 200 | * |
||
| 201 | * @param string $query The Mysql query to execute |
||
| 202 | * @param array $marks The marks passed to the statement |
||
| 203 | * |
||
| 204 | * @throws Exception On error preparing or executing the statement |
||
| 205 | * |
||
| 206 | * @return PDOStatement The result |
||
| 207 | */ |
||
| 208 | public function execute($query, array $marks = null) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Execute a callable inside a transaction. |
||
| 241 | * |
||
| 242 | * @param callable $callable The function with all operations |
||
| 243 | * |
||
| 244 | * @return mixed The callable returned value |
||
| 245 | */ |
||
| 246 | public function executeTransaction(callable $callable) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Returns the last insert id. |
||
| 269 | * |
||
| 270 | * @return string |
||
| 271 | */ |
||
| 272 | public function lastInsertId() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Starts a transaction if it's not started yet. |
||
| 279 | * |
||
| 280 | * @return bool True if a the transaction is started or false if don't |
||
| 281 | */ |
||
| 282 | public function beginTransaction() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Commits the changes of the transaction to the database. |
||
| 295 | */ |
||
| 296 | public function commit() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * RollBack a transaction. |
||
| 306 | */ |
||
| 307 | public function rollBack() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Check if there is a transaction opened currently in this adapter. |
||
| 317 | */ |
||
| 318 | public function inTransaction() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Saves a new attribute. |
||
| 325 | * |
||
| 326 | * @param string $name |
||
| 327 | * @param mixed $value |
||
| 328 | * |
||
| 329 | * @return $this |
||
| 330 | */ |
||
| 331 | public function setAttribute($name, $value) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Returns an attribute. |
||
| 344 | * |
||
| 345 | * @param string|int $name |
||
| 346 | * |
||
| 347 | * @return null|mixed |
||
| 348 | */ |
||
| 349 | public function getAttribute($name) |
||
| 357 | } |
||
| 358 |