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 |
||
32 | class DB extends AbstractAnonymizer |
||
33 | { |
||
34 | /** |
||
35 | * Doctrine DB Adapter |
||
36 | * @var Connection |
||
37 | */ |
||
38 | private $conn; |
||
39 | |||
40 | /** |
||
41 | * A helper for the current driver |
||
42 | * @var DBHelper\AbstractDBHelper |
||
43 | */ |
||
44 | private $dbHelper; |
||
45 | |||
46 | /** |
||
47 | * Various generic utils |
||
48 | * @var DBUtils |
||
49 | */ |
||
50 | private $dbUtils; |
||
51 | |||
52 | /** |
||
53 | * Primary Key |
||
54 | * @var string |
||
55 | */ |
||
56 | private $priKey; |
||
57 | |||
58 | /** |
||
59 | * Define the way we update / insert data |
||
60 | * @var string |
||
61 | */ |
||
62 | private $mode = 'queries'; |
||
63 | |||
64 | /** |
||
65 | * Contains queries if returnRes is true |
||
66 | * @var array |
||
67 | */ |
||
68 | private $queries = []; |
||
69 | |||
70 | /** |
||
71 | * File resource for the csv (batch mode) |
||
72 | * @var CSVWriter |
||
73 | */ |
||
74 | private $csv; |
||
75 | |||
76 | /** |
||
77 | * Define available update modes |
||
78 | * @var array |
||
79 | */ |
||
80 | private $updateMode = [ |
||
81 | 'queries' => 'doUpdateByQueries', |
||
82 | 'batch' => 'doBatchUpdate' |
||
83 | ]; |
||
84 | |||
85 | /** |
||
86 | * Define available insert modes |
||
87 | * @var array |
||
88 | */ |
||
89 | private $insertMode = [ |
||
90 | 'queries' => 'doInsertByQueries', |
||
91 | 'batch' => 'doBatchInsert' |
||
92 | ]; |
||
93 | |||
94 | /** |
||
95 | * Init connection |
||
96 | * |
||
97 | * @param $params Parameters to send to Doctrine DB |
||
98 | */ |
||
99 | public function __construct(array $params) |
||
111 | 10 | ||
112 | |||
113 | /** |
||
114 | * Get Doctrine Connection |
||
115 | * |
||
116 | * @return Connection |
||
117 | */ |
||
118 | public function getConn(): Connection |
||
122 | |||
123 | |||
124 | /** |
||
125 | * Set the mode for update / insert |
||
126 | * @param string $mode |
||
127 | * @return DB |
||
128 | */ |
||
129 | public function setMode(string $mode): DB |
||
144 | |||
145 | 1 | ||
146 | /** |
||
147 | 1 | * Process an entity by reading / writing to the DB |
|
148 | * |
||
149 | * @param string $entity |
||
150 | * @param callable|null $callback |
||
151 | * |
||
152 | * @return void|array |
||
153 | */ |
||
154 | public function processEntity(string $entity, callable $callback = null): array |
||
193 | 1 | ||
194 | |||
195 | /** |
||
196 | 4 | * Execute the Delete with Doctrine Query Builder |
|
197 | * |
||
198 | * @param string $where |
||
199 | * |
||
200 | * @return string |
||
201 | */ |
||
202 | private function runDelete(string $where): string |
||
219 | |||
220 | 2 | ||
221 | /** |
||
222 | 1 | * Update data of table |
|
223 | * |
||
224 | * @param callable $callback |
||
225 | */ |
||
226 | private function updateData($callback = null): void |
||
265 | |||
266 | 3 | ||
267 | /** |
||
268 | * Execute the Update with Doctrine QueryBuilder |
||
269 | 3 | * @SuppressWarnings("unused") - Used dynamically |
|
270 | * @param array $row Full row |
||
271 | */ |
||
272 | private function doUpdateByQueries(array $row): void |
||
295 | 3 | ||
296 | |||
297 | /** |
||
298 | 3 | * Write the line required for a later LOAD DATA (or \copy) |
|
299 | * @SuppressWarnings("unused") - Used dynamically |
||
300 | * @param array $row Full row |
||
301 | 3 | */ |
|
302 | private function doBatchUpdate(array $row): void |
||
317 | |||
318 | |||
319 | /** |
||
320 | * Insert data into table |
||
321 | * @param callable $callback |
||
322 | */ |
||
323 | private function insertData($callback = null): void |
||
339 | |||
340 | |||
341 | /** |
||
342 | 1 | * Execute an INSERT with Doctrine QueryBuilder |
|
343 | 1 | * @SuppressWarnings("unused") - Used dynamically |
|
344 | */ |
||
345 | 1 | private function doInsertByQueries(): void |
|
364 | |||
365 | |||
366 | /** |
||
367 | * Write the line required for a later LOAD DATA (or \copy) |
||
368 | * @SuppressWarnings("unused") - Used dynamically |
||
369 | */ |
||
370 | private function doBatchInsert(): void |
||
375 | |||
376 | |||
377 | 1 | /** |
|
378 | * If a file has been created for the batch mode, destroy it |
||
379 | 1 | * @SuppressWarnings("unused") - Used dynamically |
|
380 | 1 | * @param string $mode "update" or "insert" |
|
381 | 1 | */ |
|
382 | private function loadDataInBatch(string $mode): void |
||
400 | } |
||
401 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.