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 TaskQuery 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 TaskQuery, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
55 | abstract class TaskQuery extends ModelCriteria |
||
56 | { |
||
57 | protected $entityNotFoundExceptionClass = '\\Propel\\Runtime\\Exception\\EntityNotFoundException'; |
||
58 | |||
59 | /** |
||
60 | * Initializes internal state of \Core\Models\Task\Base\TaskQuery object. |
||
61 | * |
||
62 | * @param string $dbName The database name |
||
63 | * @param string $modelName The phpName of a model, e.g. 'Book' |
||
64 | * @param string $modelAlias The alias for the model in this query, e.g. 'b' |
||
65 | */ |
||
66 | public function __construct($dbName = 'default', $modelName = '\\Core\\Models\\Task\\Task', $modelAlias = null) |
||
70 | |||
71 | /** |
||
72 | * Returns a new ChildTaskQuery object. |
||
73 | * |
||
74 | * @param string $modelAlias The alias of a model in the query |
||
75 | * @param Criteria $criteria Optional Criteria to build the query from |
||
76 | * |
||
77 | * @return ChildTaskQuery |
||
78 | */ |
||
79 | View Code Duplication | public static function create($modelAlias = null, Criteria $criteria = null) |
|
94 | |||
95 | /** |
||
96 | * Find object by primary key. |
||
97 | * Propel uses the instance pool to skip the database if the object exists. |
||
98 | * Go fast if the query is untouched. |
||
99 | * |
||
100 | * <code> |
||
101 | * $obj = $c->findPk(12, $con); |
||
102 | * </code> |
||
103 | * |
||
104 | * @param mixed $key Primary key to use for the query |
||
105 | * @param ConnectionInterface $con an optional connection object |
||
106 | * |
||
107 | * @return ChildTask|array|mixed the result, formatted by the current formatter |
||
108 | */ |
||
109 | View Code Duplication | public function findPk($key, ConnectionInterface $con = null) |
|
130 | |||
131 | /** |
||
132 | * Find object by primary key using raw SQL to go fast. |
||
133 | * Bypass doSelect() and the object formatter by using generated code. |
||
134 | * |
||
135 | * @param mixed $key Primary key to use for the query |
||
136 | * @param ConnectionInterface $con A connection object |
||
137 | * |
||
138 | * @throws \Propel\Runtime\Exception\PropelException |
||
139 | * |
||
140 | * @return ChildTask A model object, or null if the key is not found |
||
141 | */ |
||
142 | View Code Duplication | protected function findPkSimple($key, ConnectionInterface $con) |
|
164 | |||
165 | /** |
||
166 | * Find object by primary key. |
||
167 | * |
||
168 | * @param mixed $key Primary key to use for the query |
||
169 | * @param ConnectionInterface $con A connection object |
||
170 | * |
||
171 | * @return ChildTask|array|mixed the result, formatted by the current formatter |
||
172 | */ |
||
173 | View Code Duplication | protected function findPkComplex($key, ConnectionInterface $con) |
|
183 | |||
184 | /** |
||
185 | * Find objects by primary key |
||
186 | * <code> |
||
187 | * $objs = $c->findPks(array(12, 56, 832), $con); |
||
188 | * </code> |
||
189 | * @param array $keys Primary keys to use for the query |
||
190 | * @param ConnectionInterface $con an optional connection object |
||
191 | * |
||
192 | * @return ObjectCollection|array|mixed the list of results, formatted by the current formatter |
||
193 | */ |
||
194 | View Code Duplication | public function findPks($keys, ConnectionInterface $con = null) |
|
207 | |||
208 | /** |
||
209 | * Filter the query by primary key |
||
210 | * |
||
211 | * @param mixed $key Primary key to use for the query |
||
212 | * |
||
213 | * @return $this|ChildTaskQuery The current query, for fluid interface |
||
214 | */ |
||
215 | public function filterByPrimaryKey($key) |
||
220 | |||
221 | /** |
||
222 | * Filter the query by a list of primary keys |
||
223 | * |
||
224 | * @param array $keys The list of primary key to use for the query |
||
225 | * |
||
226 | * @return $this|ChildTaskQuery The current query, for fluid interface |
||
227 | */ |
||
228 | public function filterByPrimaryKeys($keys) |
||
233 | |||
234 | /** |
||
235 | * Filter the query on the id column |
||
236 | * |
||
237 | * Example usage: |
||
238 | * <code> |
||
239 | * $query->filterById(1234); // WHERE id = 1234 |
||
240 | * $query->filterById(array(12, 34)); // WHERE id IN (12, 34) |
||
241 | * $query->filterById(array('min' => 12)); // WHERE id > 12 |
||
242 | * </code> |
||
243 | * |
||
244 | * @param mixed $id The value to use as filter. |
||
245 | * Use scalar values for equality. |
||
246 | * Use array values for in_array() equivalent. |
||
247 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
248 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
249 | * |
||
250 | * @return $this|ChildTaskQuery The current query, for fluid interface |
||
251 | */ |
||
252 | View Code Duplication | public function filterById($id = null, $comparison = null) |
|
274 | |||
275 | /** |
||
276 | * Filter the query on the created_at column |
||
277 | * |
||
278 | * Example usage: |
||
279 | * <code> |
||
280 | * $query->filterByCreatedAt('2011-03-14'); // WHERE created_at = '2011-03-14' |
||
281 | * $query->filterByCreatedAt('now'); // WHERE created_at = '2011-03-14' |
||
282 | * $query->filterByCreatedAt(array('max' => 'yesterday')); // WHERE created_at > '2011-03-13' |
||
283 | * </code> |
||
284 | * |
||
285 | * @param mixed $createdAt The value to use as filter. |
||
286 | * Values can be integers (unix timestamps), DateTime objects, or strings. |
||
287 | * Empty strings are treated as NULL. |
||
288 | * Use scalar values for equality. |
||
289 | * Use array values for in_array() equivalent. |
||
290 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
291 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
292 | * |
||
293 | * @return $this|ChildTaskQuery The current query, for fluid interface |
||
294 | */ |
||
295 | View Code Duplication | public function filterByCreatedAt($createdAt = null, $comparison = null) |
|
317 | |||
318 | /** |
||
319 | * Filter the query on the updated_at column |
||
320 | * |
||
321 | * Example usage: |
||
322 | * <code> |
||
323 | * $query->filterByUpdatedAt('2011-03-14'); // WHERE updated_at = '2011-03-14' |
||
324 | * $query->filterByUpdatedAt('now'); // WHERE updated_at = '2011-03-14' |
||
325 | * $query->filterByUpdatedAt(array('max' => 'yesterday')); // WHERE updated_at > '2011-03-13' |
||
326 | * </code> |
||
327 | * |
||
328 | * @param mixed $updatedAt The value to use as filter. |
||
329 | * Values can be integers (unix timestamps), DateTime objects, or strings. |
||
330 | * Empty strings are treated as NULL. |
||
331 | * Use scalar values for equality. |
||
332 | * Use array values for in_array() equivalent. |
||
333 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
334 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
335 | * |
||
336 | * @return $this|ChildTaskQuery The current query, for fluid interface |
||
337 | */ |
||
338 | View Code Duplication | public function filterByUpdatedAt($updatedAt = null, $comparison = null) |
|
360 | |||
361 | /** |
||
362 | * Exclude object from result |
||
363 | * |
||
364 | * @param ChildTask $task Object to remove from the list of results |
||
365 | * |
||
366 | * @return $this|ChildTaskQuery The current query, for fluid interface |
||
367 | */ |
||
368 | public function prune($task = null) |
||
376 | |||
377 | /** |
||
378 | * Deletes all rows from the tasks table. |
||
379 | * |
||
380 | * @param ConnectionInterface $con the connection to use |
||
381 | * @return int The number of affected rows (if supported by underlying database driver). |
||
382 | */ |
||
383 | View Code Duplication | public function doDeleteAll(ConnectionInterface $con = null) |
|
403 | |||
404 | /** |
||
405 | * Performs a DELETE on the database based on the current ModelCriteria |
||
406 | * |
||
407 | * @param ConnectionInterface $con the connection to use |
||
408 | * @return int The number of affected rows (if supported by underlying database driver). This includes CASCADE-related rows |
||
409 | * if supported by native driver or if emulated using Propel. |
||
410 | * @throws PropelException Any exceptions caught during processing will be |
||
411 | * rethrown wrapped into a PropelException. |
||
412 | */ |
||
413 | View Code Duplication | public function delete(ConnectionInterface $con = null) |
|
437 | |||
438 | } // TaskQuery |
||
439 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.