Complex classes like DB 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 DB, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class DB extends AbstractAnonymizer |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * Doctrine DB Adapter |
||
| 35 | * @var Connection |
||
| 36 | */ |
||
| 37 | private $conn; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Various Options for drivers |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | private $driverOptions = [ |
||
| 44 | 'pdo_mysql' => [1001 => true], // 1001 = \PDO::MYSQL_ATTR_LOCAL_INFILE |
||
| 45 | ]; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Various generic utils |
||
| 49 | * @var DBUtils |
||
| 50 | */ |
||
| 51 | private $dbUtils; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Primary Key |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | private $priKey; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Define the way we update / insert data |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | private $mode = 'queries'; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Contains queries if returnRes is true |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | private $queries = []; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * File resource for the csv (batch mode) |
||
| 73 | * @var resource |
||
| 74 | */ |
||
| 75 | private $csv; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Define available update modes |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | private $updateMode = [ |
||
| 82 | 'queries' => 'doUpdateByQueries', |
||
| 83 | 'batch' => 'doBatchUpdate' |
||
| 84 | ]; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Define available insert modes |
||
| 88 | * @var array |
||
| 89 | */ |
||
| 90 | private $insertMode = [ |
||
| 91 | 'queries' => 'doInsertByQueries', |
||
| 92 | 'batch' => 'doBatchInsert' |
||
| 93 | ]; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Init connection |
||
| 97 | * |
||
| 98 | * @param $params Parameters to send to Doctrine DB |
||
| 99 | */ |
||
| 100 | 10 | public function __construct(array $params) |
|
| 112 | |||
| 113 | |||
| 114 | /** |
||
| 115 | * Get Doctrine Connection |
||
| 116 | * |
||
| 117 | * @return Connection |
||
| 118 | */ |
||
| 119 | public function getConn(): Connection |
||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * Set the mode for update / insert |
||
| 127 | * @param string $mode |
||
| 128 | * @return DB |
||
| 129 | */ |
||
| 130 | 2 | public function setMode(string $mode): DB |
|
| 149 | |||
| 150 | |||
| 151 | /** |
||
| 152 | * Process an entity by reading / writing to the DB |
||
| 153 | * |
||
| 154 | * @param string $entity |
||
| 155 | * @param callable|null $callback |
||
| 156 | * |
||
| 157 | * @return void|array |
||
| 158 | */ |
||
| 159 | 9 | public function processEntity(string $entity, callable $callback = null): array |
|
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * Execute the Delete with Doctrine Query Builder |
||
| 202 | * |
||
| 203 | * @param string $where |
||
| 204 | * |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | 3 | private function runDelete(string $where): string |
|
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * Update data of table |
||
| 228 | * |
||
| 229 | * @param callable $callback |
||
| 230 | */ |
||
| 231 | 3 | private function updateData($callback = null): void |
|
| 270 | |||
| 271 | |||
| 272 | /** |
||
| 273 | * Execute the Update with Doctrine QueryBuilder |
||
| 274 | * @SuppressWarnings("unused") - Used dynamically |
||
| 275 | * @param array $row Full row |
||
| 276 | */ |
||
| 277 | 3 | private function doUpdateByQueries(array $row): void |
|
| 302 | |||
| 303 | |||
| 304 | /** |
||
| 305 | * Write the line required for a later LOAD DATA (or \copy) |
||
| 306 | * @SuppressWarnings("unused") - Used dynamically |
||
| 307 | * @param array $row Full row |
||
| 308 | */ |
||
| 309 | private function doBatchUpdate(array $row): void |
||
| 324 | |||
| 325 | |||
| 326 | /** |
||
| 327 | * Insert data into table |
||
| 328 | * @param callable $callback |
||
| 329 | */ |
||
| 330 | 1 | private function insertData($callback = null): void |
|
| 346 | |||
| 347 | |||
| 348 | /** |
||
| 349 | * Execute an INSERT with Doctrine QueryBuilder |
||
| 350 | * @SuppressWarnings("unused") - Used dynamically |
||
| 351 | */ |
||
| 352 | private function doInsertByQueries(): void |
||
| 371 | |||
| 372 | |||
| 373 | /** |
||
| 374 | * Write the line required for a later LOAD DATA (or \copy) |
||
| 375 | * @SuppressWarnings("unused") - Used dynamically |
||
| 376 | */ |
||
| 377 | 1 | private function doBatchInsert(): void |
|
| 382 | |||
| 383 | |||
| 384 | /** |
||
| 385 | * If a file has been created for the batch mode, destroy it |
||
| 386 | * @SuppressWarnings("unused") - Used dynamically |
||
| 387 | * @param string $mode "update" or "insert" |
||
| 388 | */ |
||
| 389 | 1 | private function loadDataInBatch(string $mode): void |
|
| 407 | |||
| 408 | |||
| 409 | /** |
||
| 410 | * Load Data for MySQL (Specific Query) |
||
| 411 | * @SuppressWarnings("unused") - Used dynamically |
||
| 412 | * @param array $fields |
||
| 413 | * @param string $mode Not in used here |
||
| 414 | * @return string |
||
| 415 | */ |
||
| 416 | 1 | private function loadDataForMysql(array $fields, string $mode): string |
|
| 430 | |||
| 431 | |||
| 432 | /** |
||
| 433 | * Load Data for Postgres (Specific Query) |
||
| 434 | * @SuppressWarnings("unused") - Used dynamically |
||
| 435 | * @param array $fields |
||
| 436 | * @param string $mode "update" or "insert" to know if we truncate or not |
||
| 437 | * @return string |
||
| 438 | */ |
||
| 439 | private function loadDataForPgsql(array $fields, string $mode): string |
||
| 457 | |||
| 458 | |||
| 459 | /** |
||
| 460 | * Load Data for SQLServer (Specific Query) |
||
| 461 | * @SuppressWarnings("unused") - Used dynamically |
||
| 462 | * @param array $fields |
||
| 463 | * @param string $mode "update" or "insert" to know if we truncate or not |
||
| 464 | * @return string |
||
| 465 | */ |
||
| 466 | private function loadDataForSqlsrv(array $fields, string $mode): string |
||
| 489 | } |
||
| 490 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..