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 UserQuery 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 UserQuery, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
75 | abstract class UserQuery extends ModelCriteria |
||
76 | { |
||
77 | protected $entityNotFoundExceptionClass = '\\Propel\\Runtime\\Exception\\EntityNotFoundException'; |
||
78 | |||
79 | /** |
||
80 | * Initializes internal state of \Core\Models\User\Base\UserQuery object. |
||
81 | * |
||
82 | * @param string $dbName The database name |
||
83 | * @param string $modelName The phpName of a model, e.g. 'Book' |
||
84 | * @param string $modelAlias The alias for the model in this query, e.g. 'b' |
||
85 | */ |
||
86 | public function __construct($dbName = 'default', $modelName = '\\Core\\Models\\User\\User', $modelAlias = null) |
||
90 | |||
91 | /** |
||
92 | * Returns a new ChildUserQuery object. |
||
93 | * |
||
94 | * @param string $modelAlias The alias of a model in the query |
||
95 | * @param Criteria $criteria Optional Criteria to build the query from |
||
96 | * |
||
97 | * @return ChildUserQuery |
||
98 | */ |
||
99 | View Code Duplication | public static function create($modelAlias = null, Criteria $criteria = null) |
|
114 | |||
115 | /** |
||
116 | * Find object by primary key. |
||
117 | * Propel uses the instance pool to skip the database if the object exists. |
||
118 | * Go fast if the query is untouched. |
||
119 | * |
||
120 | * <code> |
||
121 | * $obj = $c->findPk(12, $con); |
||
122 | * </code> |
||
123 | * |
||
124 | * @param mixed $key Primary key to use for the query |
||
125 | * @param ConnectionInterface $con an optional connection object |
||
126 | * |
||
127 | * @return ChildUser|array|mixed the result, formatted by the current formatter |
||
128 | */ |
||
129 | View Code Duplication | public function findPk($key, ConnectionInterface $con = null) |
|
150 | |||
151 | /** |
||
152 | * Find object by primary key using raw SQL to go fast. |
||
153 | * Bypass doSelect() and the object formatter by using generated code. |
||
154 | * |
||
155 | * @param mixed $key Primary key to use for the query |
||
156 | * @param ConnectionInterface $con A connection object |
||
157 | * |
||
158 | * @throws \Propel\Runtime\Exception\PropelException |
||
159 | * |
||
160 | * @return ChildUser A model object, or null if the key is not found |
||
161 | */ |
||
162 | View Code Duplication | protected function findPkSimple($key, ConnectionInterface $con) |
|
184 | |||
185 | /** |
||
186 | * Find object by primary key. |
||
187 | * |
||
188 | * @param mixed $key Primary key to use for the query |
||
189 | * @param ConnectionInterface $con A connection object |
||
190 | * |
||
191 | * @return ChildUser|array|mixed the result, formatted by the current formatter |
||
192 | */ |
||
193 | View Code Duplication | protected function findPkComplex($key, ConnectionInterface $con) |
|
203 | |||
204 | /** |
||
205 | * Find objects by primary key |
||
206 | * <code> |
||
207 | * $objs = $c->findPks(array(12, 56, 832), $con); |
||
208 | * </code> |
||
209 | * @param array $keys Primary keys to use for the query |
||
210 | * @param ConnectionInterface $con an optional connection object |
||
211 | * |
||
212 | * @return ObjectCollection|array|mixed the list of results, formatted by the current formatter |
||
213 | */ |
||
214 | View Code Duplication | public function findPks($keys, ConnectionInterface $con = null) |
|
227 | |||
228 | /** |
||
229 | * Filter the query by primary key |
||
230 | * |
||
231 | * @param mixed $key Primary key to use for the query |
||
232 | * |
||
233 | * @return $this|ChildUserQuery The current query, for fluid interface |
||
234 | */ |
||
235 | public function filterByPrimaryKey($key) |
||
240 | |||
241 | /** |
||
242 | * Filter the query by a list of primary keys |
||
243 | * |
||
244 | * @param array $keys The list of primary key to use for the query |
||
245 | * |
||
246 | * @return $this|ChildUserQuery The current query, for fluid interface |
||
247 | */ |
||
248 | public function filterByPrimaryKeys($keys) |
||
253 | |||
254 | /** |
||
255 | * Filter the query on the id column |
||
256 | * |
||
257 | * Example usage: |
||
258 | * <code> |
||
259 | * $query->filterById(1234); // WHERE id = 1234 |
||
260 | * $query->filterById(array(12, 34)); // WHERE id IN (12, 34) |
||
261 | * $query->filterById(array('min' => 12)); // WHERE id > 12 |
||
262 | * </code> |
||
263 | * |
||
264 | * @param mixed $id The value to use as filter. |
||
265 | * Use scalar values for equality. |
||
266 | * Use array values for in_array() equivalent. |
||
267 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
268 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
269 | * |
||
270 | * @return $this|ChildUserQuery The current query, for fluid interface |
||
271 | */ |
||
272 | View Code Duplication | public function filterById($id = null, $comparison = null) |
|
294 | |||
295 | /** |
||
296 | * Filter the query on the name column |
||
297 | * |
||
298 | * Example usage: |
||
299 | * <code> |
||
300 | * $query->filterByName('fooValue'); // WHERE name = 'fooValue' |
||
301 | * $query->filterByName('%fooValue%'); // WHERE name LIKE '%fooValue%' |
||
302 | * </code> |
||
303 | * |
||
304 | * @param string $name The value to use as filter. |
||
305 | * Accepts wildcards (* and % trigger a LIKE) |
||
306 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
307 | * |
||
308 | * @return $this|ChildUserQuery The current query, for fluid interface |
||
309 | */ |
||
310 | View Code Duplication | public function filterByName($name = null, $comparison = null) |
|
323 | |||
324 | /** |
||
325 | * Filter the query on the email column |
||
326 | * |
||
327 | * Example usage: |
||
328 | * <code> |
||
329 | * $query->filterByEmail('fooValue'); // WHERE email = 'fooValue' |
||
330 | * $query->filterByEmail('%fooValue%'); // WHERE email LIKE '%fooValue%' |
||
331 | * </code> |
||
332 | * |
||
333 | * @param string $email The value to use as filter. |
||
334 | * Accepts wildcards (* and % trigger a LIKE) |
||
335 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
336 | * |
||
337 | * @return $this|ChildUserQuery The current query, for fluid interface |
||
338 | */ |
||
339 | View Code Duplication | public function filterByEmail($email = null, $comparison = null) |
|
352 | |||
353 | /** |
||
354 | * Filter the query on the password column |
||
355 | * |
||
356 | * Example usage: |
||
357 | * <code> |
||
358 | * $query->filterByPassword('fooValue'); // WHERE password = 'fooValue' |
||
359 | * $query->filterByPassword('%fooValue%'); // WHERE password LIKE '%fooValue%' |
||
360 | * </code> |
||
361 | * |
||
362 | * @param string $password The value to use as filter. |
||
363 | * Accepts wildcards (* and % trigger a LIKE) |
||
364 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
365 | * |
||
366 | * @return $this|ChildUserQuery The current query, for fluid interface |
||
367 | */ |
||
368 | View Code Duplication | public function filterByPassword($password = null, $comparison = null) |
|
381 | |||
382 | /** |
||
383 | * Filter the query on the remember_token column |
||
384 | * |
||
385 | * Example usage: |
||
386 | * <code> |
||
387 | * $query->filterByRememberToken('fooValue'); // WHERE remember_token = 'fooValue' |
||
388 | * $query->filterByRememberToken('%fooValue%'); // WHERE remember_token LIKE '%fooValue%' |
||
389 | * </code> |
||
390 | * |
||
391 | * @param string $rememberToken The value to use as filter. |
||
392 | * Accepts wildcards (* and % trigger a LIKE) |
||
393 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
394 | * |
||
395 | * @return $this|ChildUserQuery The current query, for fluid interface |
||
396 | */ |
||
397 | View Code Duplication | public function filterByRememberToken($rememberToken = null, $comparison = null) |
|
410 | |||
411 | /** |
||
412 | * Filter the query on the created_at column |
||
413 | * |
||
414 | * Example usage: |
||
415 | * <code> |
||
416 | * $query->filterByCreatedAt('2011-03-14'); // WHERE created_at = '2011-03-14' |
||
417 | * $query->filterByCreatedAt('now'); // WHERE created_at = '2011-03-14' |
||
418 | * $query->filterByCreatedAt(array('max' => 'yesterday')); // WHERE created_at > '2011-03-13' |
||
419 | * </code> |
||
420 | * |
||
421 | * @param mixed $createdAt The value to use as filter. |
||
422 | * Values can be integers (unix timestamps), DateTime objects, or strings. |
||
423 | * Empty strings are treated as NULL. |
||
424 | * Use scalar values for equality. |
||
425 | * Use array values for in_array() equivalent. |
||
426 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
427 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
428 | * |
||
429 | * @return $this|ChildUserQuery The current query, for fluid interface |
||
430 | */ |
||
431 | View Code Duplication | public function filterByCreatedAt($createdAt = null, $comparison = null) |
|
453 | |||
454 | /** |
||
455 | * Filter the query on the updated_at column |
||
456 | * |
||
457 | * Example usage: |
||
458 | * <code> |
||
459 | * $query->filterByUpdatedAt('2011-03-14'); // WHERE updated_at = '2011-03-14' |
||
460 | * $query->filterByUpdatedAt('now'); // WHERE updated_at = '2011-03-14' |
||
461 | * $query->filterByUpdatedAt(array('max' => 'yesterday')); // WHERE updated_at > '2011-03-13' |
||
462 | * </code> |
||
463 | * |
||
464 | * @param mixed $updatedAt The value to use as filter. |
||
465 | * Values can be integers (unix timestamps), DateTime objects, or strings. |
||
466 | * Empty strings are treated as NULL. |
||
467 | * Use scalar values for equality. |
||
468 | * Use array values for in_array() equivalent. |
||
469 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
470 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
471 | * |
||
472 | * @return $this|ChildUserQuery The current query, for fluid interface |
||
473 | */ |
||
474 | View Code Duplication | public function filterByUpdatedAt($updatedAt = null, $comparison = null) |
|
496 | |||
497 | /** |
||
498 | * Exclude object from result |
||
499 | * |
||
500 | * @param ChildUser $user Object to remove from the list of results |
||
501 | * |
||
502 | * @return $this|ChildUserQuery The current query, for fluid interface |
||
503 | */ |
||
504 | public function prune($user = null) |
||
512 | |||
513 | /** |
||
514 | * Deletes all rows from the users table. |
||
515 | * |
||
516 | * @param ConnectionInterface $con the connection to use |
||
517 | * @return int The number of affected rows (if supported by underlying database driver). |
||
518 | */ |
||
519 | View Code Duplication | public function doDeleteAll(ConnectionInterface $con = null) |
|
539 | |||
540 | /** |
||
541 | * Performs a DELETE on the database based on the current ModelCriteria |
||
542 | * |
||
543 | * @param ConnectionInterface $con the connection to use |
||
544 | * @return int The number of affected rows (if supported by underlying database driver). This includes CASCADE-related rows |
||
545 | * if supported by native driver or if emulated using Propel. |
||
546 | * @throws PropelException Any exceptions caught during processing will be |
||
547 | * rethrown wrapped into a PropelException. |
||
548 | */ |
||
549 | View Code Duplication | public function delete(ConnectionInterface $con = null) |
|
573 | |||
574 | } // UserQuery |
||
575 |
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.