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 | * Init connection |
||
55 | * |
||
56 | * @param $params Parameters to send to Doctrine DB |
||
57 | */ |
||
58 | 34 | public function __construct(array $params) |
|
62 | |||
63 | |||
64 | /** |
||
65 | * Get Doctrine Connection |
||
66 | * |
||
67 | * @return Connection |
||
68 | */ |
||
69 | 22 | public function getConn(): Connection |
|
73 | |||
74 | |||
75 | /** |
||
76 | * Set the limit for updates and creates |
||
77 | * |
||
78 | * @param int $limit |
||
79 | */ |
||
80 | 5 | public function setLimit(int $limit): void |
|
84 | |||
85 | |||
86 | /** |
||
87 | * Process an entity by reading / writing to the DB |
||
88 | * |
||
89 | * @param string $entity |
||
90 | * @param callable|null $callback |
||
91 | * @param bool $pretend |
||
92 | * @param bool $returnRes |
||
93 | * |
||
94 | * @return void|array |
||
95 | */ |
||
96 | 18 | public function processEntity( |
|
137 | |||
138 | |||
139 | /** |
||
140 | * Identify the primary key for a table |
||
141 | * |
||
142 | * @return string Field's name |
||
143 | */ |
||
144 | 17 | private function getPrimaryKey(): string |
|
154 | |||
155 | |||
156 | /** |
||
157 | * Retrieve columns list for a table with type and length |
||
158 | * |
||
159 | * @return array $cols |
||
160 | */ |
||
161 | 16 | private function getTableCols(): array |
|
176 | |||
177 | |||
178 | /** |
||
179 | * Execute the Update with Doctrine QueryBuilder |
||
180 | * |
||
181 | * @param string $primaryKeyVal Primary Key's Value |
||
182 | * @return QueryBuilder Doctrine DBAL QueryBuilder |
||
183 | */ |
||
184 | 9 | private function prepareUpdate($primaryKeyVal): QueryBuilder |
|
199 | |||
200 | /** |
||
201 | * Execute the Update with Doctrine QueryBuilder |
||
202 | * |
||
203 | * @return QueryBuilder Doctrine DBAL QueryBuilder |
||
204 | */ |
||
205 | 3 | private function prepareInsert(): QueryBuilder |
|
218 | |||
219 | |||
220 | /** |
||
221 | * To debug, build the final SQL (can be approximative) |
||
222 | * @param QueryBuilder $queryBuilder |
||
223 | * @return string |
||
224 | */ |
||
225 | 8 | private function getRawSQL(QueryBuilder $queryBuilder) |
|
234 | |||
235 | |||
236 | /** |
||
237 | * Execute the Delete with Doctrine Query Builder |
||
238 | * |
||
239 | * @param string $where |
||
240 | * @param bool $pretend |
||
241 | * |
||
242 | * @return string |
||
243 | */ |
||
244 | 4 | private function runDelete(string $where, bool $pretend): string |
|
265 | |||
266 | |||
267 | /** |
||
268 | * Build the condition by casting the value if needed |
||
269 | * |
||
270 | * @param string $field |
||
271 | * @return string |
||
272 | */ |
||
273 | 7 | private function getCondition(string $field): string |
|
299 | |||
300 | |||
301 | /** |
||
302 | * Get the right CAST for an INTEGER |
||
303 | * |
||
304 | * @param string $field |
||
305 | * @return string |
||
306 | */ |
||
307 | 7 | private function getIntegerCast(string $field): string |
|
316 | |||
317 | |||
318 | /** |
||
319 | * Update data of table |
||
320 | * |
||
321 | * @param bool $returnRes |
||
322 | * @param bool $pretend |
||
323 | * @param callable $callback |
||
324 | * @return array |
||
325 | */ |
||
326 | 10 | private function updateData(bool $returnRes, bool $pretend, $callback): array |
|
356 | |||
357 | |||
358 | /** |
||
359 | * Insert data into table |
||
360 | * |
||
361 | * @param bool $returnRes |
||
362 | * @param bool $pretend |
||
363 | * @param callable $callback |
||
364 | * @return array |
||
365 | */ |
||
366 | 3 | private function insertData(bool $returnRes, bool $pretend, $callback): array |
|
388 | } |
||
389 |
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: