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 |
||
| 29 | class DB extends AbstractAnonymizer |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * Doctrine DB Adapter |
||
| 33 | * |
||
| 34 | * @var Connection |
||
| 35 | */ |
||
| 36 | private $conn; |
||
| 37 | |||
| 38 | |||
| 39 | /** |
||
| 40 | * Primary Key |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private $priKey; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Limit the number of updates or create |
||
| 48 | * |
||
| 49 | * @var int |
||
| 50 | */ |
||
| 51 | private $limit = 0; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Set the batch size for updates |
||
| 55 | * @var int |
||
| 56 | */ |
||
| 57 | private $batchSize = 1000; |
||
| 58 | 34 | ||
| 59 | |||
| 60 | 34 | /** |
|
| 61 | 33 | * Init connection |
|
| 62 | * |
||
| 63 | * @param $params Parameters to send to Doctrine DB |
||
| 64 | */ |
||
| 65 | public function __construct(array $params) |
||
| 70 | |||
| 71 | 22 | ||
| 72 | /** |
||
| 73 | * Get Doctrine Connection |
||
| 74 | * |
||
| 75 | * @return Connection |
||
| 76 | */ |
||
| 77 | public function getConn(): Connection |
||
| 81 | |||
| 82 | 5 | ||
| 83 | 5 | /** |
|
| 84 | * Set the limit for updates and creates |
||
| 85 | * |
||
| 86 | * @param int $limit |
||
| 87 | * @return DB |
||
| 88 | */ |
||
| 89 | public function setLimit(int $limit): DB |
||
| 98 | |||
| 99 | |||
| 100 | /** |
||
| 101 | * Process an entity by reading / writing to the DB |
||
| 102 | 18 | * |
|
| 103 | 18 | * @param string $entity |
|
| 104 | 1 | * @param callable|null $callback |
|
| 105 | * @param bool $pretend |
||
| 106 | * @param bool $returnRes |
||
| 107 | 17 | * |
|
| 108 | 17 | * @return void|array |
|
| 109 | 16 | */ |
|
| 110 | public function processEntity( |
||
| 151 | |||
| 152 | 16 | ||
| 153 | public function countResults(string $table): int |
||
| 160 | |||
| 161 | 16 | ||
| 162 | /** |
||
| 163 | 16 | * Identify the primary key for a table |
|
| 164 | 16 | * |
|
| 165 | 16 | * @return string Field's name |
|
| 166 | 16 | */ |
|
| 167 | 16 | private function getPrimaryKey(): string |
|
| 177 | |||
| 178 | |||
| 179 | /** |
||
| 180 | * Retrieve columns list for a table with type and length |
||
| 181 | * |
||
| 182 | * @return array $cols |
||
| 183 | */ |
||
| 184 | 9 | private function getTableCols(): array |
|
| 199 | |||
| 200 | |||
| 201 | /** |
||
| 202 | * Execute the Update with Doctrine QueryBuilder |
||
| 203 | * |
||
| 204 | * @param string $primaryKeyVal Primary Key's Value |
||
| 205 | 3 | * @return QueryBuilder Doctrine DBAL QueryBuilder |
|
| 206 | */ |
||
| 207 | 3 | private function prepareUpdate($primaryKeyVal): QueryBuilder |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Execute the Update with Doctrine QueryBuilder |
||
| 225 | 8 | * |
|
| 226 | * @return QueryBuilder Doctrine DBAL QueryBuilder |
||
| 227 | 8 | */ |
|
| 228 | 8 | private function prepareInsert(): QueryBuilder |
|
| 241 | |||
| 242 | |||
| 243 | /** |
||
| 244 | 4 | * To debug, build the final SQL (can be approximative) |
|
| 245 | * @param QueryBuilder $queryBuilder |
||
| 246 | 4 | * @return string |
|
| 247 | 4 | */ |
|
| 248 | 4 | private function getRawSQL(QueryBuilder $queryBuilder) |
|
| 257 | |||
| 258 | 3 | ||
| 259 | 1 | /** |
|
| 260 | 1 | * Execute the Delete with Doctrine Query Builder |
|
| 261 | * |
||
| 262 | * @param string $where |
||
| 263 | 2 | * @param bool $pretend |
|
| 264 | * |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | private function runDelete(string $where, bool $pretend): string |
||
| 288 | 7 | ||
| 289 | 7 | ||
| 290 | /** |
||
| 291 | * Build the condition by casting the value if needed |
||
| 292 | * |
||
| 293 | 7 | * @param string $field |
|
| 294 | 7 | * @return string |
|
| 295 | */ |
||
| 296 | private function getCondition(string $field): string |
||
| 322 | |||
| 323 | |||
| 324 | /** |
||
| 325 | * Get the right CAST for an INTEGER |
||
| 326 | 10 | * |
|
| 327 | * @param string $field |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | 10 | private function getIntegerCast(string $field): string |
|
| 339 | 7 | ||
| 340 | |||
| 341 | 7 | /** |
|
| 342 | 4 | * Update data of table |
|
| 343 | * |
||
| 344 | * @param bool $returnRes |
||
| 345 | 7 | * @param bool $pretend |
|
| 346 | 3 | * @param callable $callback |
|
| 347 | * @return array |
||
| 348 | */ |
||
| 349 | 7 | private function updateData(bool $returnRes, bool $pretend, $callback = null): array |
|
| 392 | |||
| 393 | |||
| 394 | /** |
||
| 395 | * Insert data into table |
||
| 396 | * |
||
| 397 | * @param bool $returnRes |
||
| 398 | * @param bool $pretend |
||
| 399 | * @param callable $callback |
||
| 400 | * @return array |
||
| 401 | */ |
||
| 402 | private function insertData(bool $returnRes, bool $pretend, $callback): array |
||
| 424 | } |
||
| 425 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: