Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DbRepository 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 DbRepository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class DbRepository extends ManagedRepository implements RepositoryInterface |
||
11 | { |
||
12 | /** |
||
13 | * @var \Anax\Database\DatabaseQueryBuilder Database service. |
||
14 | */ |
||
15 | protected $db; |
||
16 | |||
17 | /** |
||
18 | * @var string Database table name. |
||
19 | */ |
||
20 | protected $table; |
||
21 | |||
22 | /** |
||
23 | * @var string Model class name. |
||
24 | */ |
||
25 | protected $modelClass; |
||
26 | |||
27 | /** |
||
28 | * @var string Primary key column. |
||
29 | */ |
||
30 | protected $key; |
||
31 | |||
32 | |||
33 | /** |
||
34 | * Constructor. |
||
35 | * |
||
36 | * @param \Anax\Database\DatabaseQueryBuilder $db Database service. |
||
37 | * @param string $table Database table name. |
||
38 | * @param string $modelClass Model class name. |
||
39 | * @param string $key Primary key column. |
||
40 | */ |
||
41 | 30 | public function __construct($db, $table, $modelClass, $key = 'id') |
|
48 | |||
49 | |||
50 | /** |
||
51 | * Return the name of the database table represented by the repository. |
||
52 | */ |
||
53 | 7 | public function getCollectionName() |
|
57 | |||
58 | |||
59 | /** |
||
60 | * Return the class of the model handled by the repository. |
||
61 | */ |
||
62 | 14 | public function getModelClass() |
|
66 | |||
67 | |||
68 | /** |
||
69 | * Find and return first entry by key. |
||
70 | * |
||
71 | * @param string|null $column Key column name (pass null to use registered primary key). |
||
72 | * @param mixed $value Key value. |
||
73 | * |
||
74 | * @return mixed Model instance. |
||
75 | */ |
||
76 | 18 | public function find($column, $value) |
|
80 | |||
81 | |||
82 | /** |
||
83 | * Retrieve first entry, optionally filtered by search criteria. |
||
84 | * |
||
85 | * @param string $conditions Where conditions. |
||
86 | * @param array $values Array of condition values to bind. |
||
87 | * @param array $options Query options. |
||
88 | * |
||
89 | * @return mixed Model instance. |
||
90 | */ |
||
91 | 19 | public function getFirst($conditions = null, $values = [], $options = []) |
|
95 | |||
96 | |||
97 | /** |
||
98 | * Retrieve all entries, optionally filtered by search criteria. |
||
99 | * |
||
100 | * @param string $conditions Where conditions. |
||
101 | * @param array $values Array of condition values to bind. |
||
102 | * @param array $options Query options. |
||
103 | * |
||
104 | * @return array Array of all matching entries. |
||
105 | */ |
||
106 | 4 | public function getAll($conditions = null, $values = [], $options = []) |
|
110 | |||
111 | |||
112 | /** |
||
113 | * Save entry by inserting if ID is missing and updating if ID exists. |
||
114 | * |
||
115 | * @param mixed $model Model instance. |
||
116 | * |
||
117 | * @return void |
||
118 | */ |
||
119 | 5 | public function save($model) |
|
127 | |||
128 | |||
129 | /** |
||
130 | * Delete entry. |
||
131 | * |
||
132 | * @param mixed $model Model instance. |
||
133 | */ |
||
134 | 1 | public function delete($model) |
|
142 | |||
143 | |||
144 | /** |
||
145 | * Count entries, optionally filtered by search criteria. |
||
146 | * |
||
147 | * @param string $conditions Where conditions. |
||
148 | * @param array $values Array of condition values to bind. |
||
149 | * |
||
150 | * @return int Number of entries. |
||
151 | */ |
||
152 | 4 | public function count($conditions = null, $values = []) |
|
158 | |||
159 | |||
160 | /** |
||
161 | * Execute query for selection methods. |
||
162 | * |
||
163 | * @param string $select Selection criteria. |
||
164 | * @param string $conditions Where conditions. |
||
165 | * @param array $values Array of where condition values to bind. |
||
166 | * @param array $options Query options. |
||
167 | * |
||
168 | * @return \Anax\Database\DatabaseQueryBuilder Database service instance with executed internal query. |
||
169 | */ |
||
170 | 26 | protected function executeQuery($select = null, $conditions = null, $values = [], $options = []) |
|
195 | |||
196 | |||
197 | |||
198 | /** |
||
199 | * Populate model instance including retrieved references from join query result. |
||
200 | * |
||
201 | * @param object $result Query result. |
||
202 | * |
||
203 | * @return mixed Populated model instance. |
||
204 | */ |
||
205 | 6 | protected function populateModelFromJoin($result) |
|
240 | |||
241 | |||
242 | /** |
||
243 | * Process single query result and return model instance. |
||
244 | * |
||
245 | * @param \Anax\Database\DatabaseQueryBuilder $query Database service instance with executed internal query. |
||
246 | * |
||
247 | * @return mixed Model instance. |
||
248 | */ |
||
249 | 22 | View Code Duplication | protected function processSingleResult($query) |
263 | |||
264 | |||
265 | /** |
||
266 | * Process multiple query results and return model instances. |
||
267 | * |
||
268 | * @param \Anax\Database\DatabaseQueryBuilder $query Database service instance with executed internal query. |
||
269 | * |
||
270 | * @return array Array of model instances. |
||
271 | */ |
||
272 | 6 | View Code Duplication | protected function processMultipleResults($query) |
290 | |||
291 | |||
292 | /** |
||
293 | * Set up join query for reference retrieval. |
||
294 | * |
||
295 | * @param \Anax\Database\DatabaseQueryBuilder $query Database service instance with initialized query. |
||
296 | * @param string $select Selection criteria. |
||
297 | * @param string $conditions Where conditions. |
||
298 | * @param string $order Order by clause. |
||
299 | * |
||
300 | * @return \Anax\Database\DatabaseQueryBuilder Database service instance with prepared join query. |
||
301 | * |
||
302 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
303 | * @SuppressWarnings(PHPMD.NPathComplexity) |
||
304 | */ |
||
305 | 6 | private function setupJoin($query, $select, $conditions, $order = null) |
|
361 | |||
362 | |||
363 | /** |
||
364 | * Prefix model attributes with the associated table name. |
||
365 | * |
||
366 | * @param string $input Input string. |
||
367 | * @param object $model Model instance. |
||
368 | * |
||
369 | * @return string String with table-prefixed attributes. |
||
370 | */ |
||
371 | 6 | private function prefixModelAttributes($input, $model) |
|
378 | |||
379 | |||
380 | /** |
||
381 | * Create new entry. |
||
382 | * |
||
383 | * @param mixed $model Model instance. |
||
384 | */ |
||
385 | 3 | private function create($model) |
|
394 | |||
395 | |||
396 | /** |
||
397 | * Update entry. |
||
398 | * |
||
399 | * @param mixed $model Model instance. |
||
400 | */ |
||
401 | 5 | private function update($model) |
|
412 | |||
413 | |||
414 | /** |
||
415 | * Get mutable model attributes. |
||
416 | * |
||
417 | * @param object $model Model instance. |
||
418 | * |
||
419 | * @return array Array of attributes. |
||
420 | */ |
||
421 | 5 | private function getMutableAttributes($model) |
|
435 | } |
||
436 |
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: