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 | * @var Connection |
||
34 | */ |
||
35 | private $conn; |
||
36 | |||
37 | /** |
||
38 | * Primary Key |
||
39 | * @var string |
||
40 | */ |
||
41 | private $priKey; |
||
42 | |||
43 | |||
44 | /** |
||
45 | * Init connection |
||
46 | * |
||
47 | * @param $params Parameters to send to Doctrine DB |
||
48 | */ |
||
49 | public function __construct(array $params) |
||
54 | |||
55 | |||
56 | /** |
||
57 | * Get Doctrine Connection |
||
58 | 34 | * |
|
59 | * @return Connection |
||
60 | 34 | */ |
|
61 | 33 | public function getConn(): Connection |
|
65 | |||
66 | |||
67 | /** |
||
68 | * Process an entity by reading / writing to the DB |
||
69 | 22 | * |
|
70 | * @param string $entity |
||
71 | 22 | * @param callable|null $callback |
|
72 | * |
||
73 | * @return void|array |
||
74 | */ |
||
75 | public function processEntity(string $entity, callable $callback = null): array |
||
112 | |||
113 | 16 | /** |
|
114 | * Do a simple count for a table |
||
115 | 14 | * |
|
116 | 4 | * @param string $table |
|
117 | 4 | * @return int |
|
118 | 3 | */ |
|
119 | public function countResults(string $table): int |
||
126 | |||
127 | |||
128 | 11 | /** |
|
129 | 3 | * Identify the primary key for a table |
|
130 | 3 | * |
|
131 | 3 | * @return string Field's name |
|
132 | */ |
||
133 | private function getPrimaryKey(): string |
||
143 | |||
144 | 17 | ||
145 | /** |
||
146 | 17 | * Retrieve columns list for a table with type and length |
|
147 | 17 | * |
|
148 | 17 | * @return array $cols |
|
149 | 1 | */ |
|
150 | private function getTableCols(): array |
||
165 | 16 | ||
166 | 16 | ||
167 | 16 | /** |
|
168 | 16 | * To debug, build the final SQL (can be approximative) |
|
169 | 16 | * @param QueryBuilder $queryBuilder |
|
170 | 16 | * @return string |
|
171 | */ |
||
172 | private function getRawSQL(QueryBuilder $queryBuilder) |
||
181 | |||
182 | |||
183 | /** |
||
184 | 9 | * Execute the Delete with Doctrine Query Builder |
|
185 | * |
||
186 | 9 | * @param string $where |
|
187 | * |
||
188 | 7 | * @return string |
|
189 | 7 | */ |
|
190 | 7 | private function runDelete(string $where): string |
|
211 | 3 | ||
212 | 3 | ||
213 | 3 | /** |
|
214 | * Build the condition by casting the value if needed |
||
215 | * |
||
216 | 3 | * @param string $field |
|
217 | * @return string |
||
218 | */ |
||
219 | private function getCondition(string $field): string |
||
245 | |||
246 | 4 | ||
247 | 4 | /** |
|
248 | 4 | * Get the right CAST for an INTEGER |
|
249 | 3 | * |
|
250 | * @param string $field |
||
251 | 4 | * @return string |
|
252 | */ |
||
253 | 4 | private function getIntegerCast(string $field): string |
|
262 | |||
263 | 2 | ||
264 | /** |
||
265 | * Update data of table |
||
266 | * |
||
267 | * @param callable $callback |
||
268 | * @return array |
||
269 | */ |
||
270 | private function updateData($callback = null): array |
||
314 | |||
315 | |||
316 | /** |
||
317 | * Execute the Update with Doctrine QueryBuilder |
||
318 | * |
||
319 | * @param string $primaryKeyVal Primary Key's Value |
||
320 | * @return QueryBuilder Doctrine DBAL QueryBuilder |
||
321 | */ |
||
322 | private function prepareUpdate($primaryKeyVal): QueryBuilder |
||
337 | 9 | ||
338 | |||
339 | 7 | /** |
|
340 | * Insert data into table |
||
341 | 7 | * |
|
342 | 4 | * @param callable $callback |
|
343 | * @return array |
||
344 | */ |
||
345 | 7 | private function insertData($callback): array |
|
367 | |||
368 | 3 | ||
369 | /** |
||
370 | 3 | * Execute an INSERT with Doctrine QueryBuilder |
|
371 | * |
||
372 | 3 | * @return QueryBuilder Doctrine DBAL QueryBuilder |
|
373 | 3 | */ |
|
374 | private function prepareInsert(): QueryBuilder |
||
387 | } |
||
388 |
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: