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 ConnectionQuery 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 ConnectionQuery, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
97 | abstract class ConnectionQuery extends ModelCriteria |
||
98 | { |
||
99 | protected $entityNotFoundExceptionClass = '\\Propel\\Runtime\\Exception\\EntityNotFoundException'; |
||
100 | |||
101 | /** |
||
102 | * Initializes internal state of \Jalle19\StatusManager\Database\Base\ConnectionQuery object. |
||
103 | * |
||
104 | * @param string $dbName The database name |
||
105 | * @param string $modelName The phpName of a model, e.g. 'Book' |
||
106 | * @param string $modelAlias The alias for the model in this query, e.g. 'b' |
||
107 | */ |
||
108 | public function __construct($dbName = 'tvheadend_status_manager', $modelName = '\\Jalle19\\StatusManager\\Database\\Connection', $modelAlias = null) |
||
112 | |||
113 | /** |
||
114 | * Returns a new ChildConnectionQuery object. |
||
115 | * |
||
116 | * @param string $modelAlias The alias of a model in the query |
||
117 | * @param Criteria $criteria Optional Criteria to build the query from |
||
118 | * |
||
119 | * @return ChildConnectionQuery |
||
120 | */ |
||
121 | View Code Duplication | public static function create($modelAlias = null, Criteria $criteria = null) |
|
136 | |||
137 | /** |
||
138 | * Find object by primary key. |
||
139 | * Propel uses the instance pool to skip the database if the object exists. |
||
140 | * Go fast if the query is untouched. |
||
141 | * |
||
142 | * <code> |
||
143 | * $obj = $c->findPk(12, $con); |
||
144 | * </code> |
||
145 | * |
||
146 | * @param mixed $key Primary key to use for the query |
||
147 | * @param ConnectionInterface $con an optional connection object |
||
148 | * |
||
149 | * @return ChildConnection|array|mixed the result, formatted by the current formatter |
||
150 | */ |
||
151 | View Code Duplication | public function findPk($key, ConnectionInterface $con = null) |
|
172 | |||
173 | /** |
||
174 | * Find object by primary key using raw SQL to go fast. |
||
175 | * Bypass doSelect() and the object formatter by using generated code. |
||
176 | * |
||
177 | * @param mixed $key Primary key to use for the query |
||
178 | * @param ConnectionInterface $con A connection object |
||
179 | * |
||
180 | * @throws \Propel\Runtime\Exception\PropelException |
||
181 | * |
||
182 | * @return ChildConnection A model object, or null if the key is not found |
||
183 | */ |
||
184 | View Code Duplication | protected function findPkSimple($key, ConnectionInterface $con) |
|
206 | |||
207 | /** |
||
208 | * Find object by primary key. |
||
209 | * |
||
210 | * @param mixed $key Primary key to use for the query |
||
211 | * @param ConnectionInterface $con A connection object |
||
212 | * |
||
213 | * @return ChildConnection|array|mixed the result, formatted by the current formatter |
||
214 | */ |
||
215 | View Code Duplication | protected function findPkComplex($key, ConnectionInterface $con) |
|
225 | |||
226 | /** |
||
227 | * Find objects by primary key |
||
228 | * <code> |
||
229 | * $objs = $c->findPks(array(12, 56, 832), $con); |
||
230 | * </code> |
||
231 | * @param array $keys Primary keys to use for the query |
||
232 | * @param ConnectionInterface $con an optional connection object |
||
233 | * |
||
234 | * @return ObjectCollection|array|mixed the list of results, formatted by the current formatter |
||
235 | */ |
||
236 | View Code Duplication | public function findPks($keys, ConnectionInterface $con = null) |
|
249 | |||
250 | /** |
||
251 | * Filter the query by primary key |
||
252 | * |
||
253 | * @param mixed $key Primary key to use for the query |
||
254 | * |
||
255 | * @return $this|ChildConnectionQuery The current query, for fluid interface |
||
256 | */ |
||
257 | public function filterByPrimaryKey($key) |
||
262 | |||
263 | /** |
||
264 | * Filter the query by a list of primary keys |
||
265 | * |
||
266 | * @param array $keys The list of primary key to use for the query |
||
267 | * |
||
268 | * @return $this|ChildConnectionQuery The current query, for fluid interface |
||
269 | */ |
||
270 | public function filterByPrimaryKeys($keys) |
||
275 | |||
276 | /** |
||
277 | * Filter the query on the id column |
||
278 | * |
||
279 | * Example usage: |
||
280 | * <code> |
||
281 | * $query->filterById(1234); // WHERE id = 1234 |
||
282 | * $query->filterById(array(12, 34)); // WHERE id IN (12, 34) |
||
283 | * $query->filterById(array('min' => 12)); // WHERE id > 12 |
||
284 | * </code> |
||
285 | * |
||
286 | * @param mixed $id The value to use as filter. |
||
287 | * Use scalar values for equality. |
||
288 | * Use array values for in_array() equivalent. |
||
289 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
290 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
291 | * |
||
292 | * @return $this|ChildConnectionQuery The current query, for fluid interface |
||
293 | */ |
||
294 | View Code Duplication | public function filterById($id = null, $comparison = null) |
|
316 | |||
317 | /** |
||
318 | * Filter the query on the instance_name column |
||
319 | * |
||
320 | * Example usage: |
||
321 | * <code> |
||
322 | * $query->filterByInstanceName('fooValue'); // WHERE instance_name = 'fooValue' |
||
323 | * $query->filterByInstanceName('%fooValue%'); // WHERE instance_name LIKE '%fooValue%' |
||
324 | * </code> |
||
325 | * |
||
326 | * @param string $instanceName The value to use as filter. |
||
327 | * Accepts wildcards (* and % trigger a LIKE) |
||
328 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
329 | * |
||
330 | * @return $this|ChildConnectionQuery The current query, for fluid interface |
||
331 | */ |
||
332 | View Code Duplication | public function filterByInstanceName($instanceName = null, $comparison = null) |
|
345 | |||
346 | /** |
||
347 | * Filter the query on the user_id column |
||
348 | * |
||
349 | * Example usage: |
||
350 | * <code> |
||
351 | * $query->filterByUserId(1234); // WHERE user_id = 1234 |
||
352 | * $query->filterByUserId(array(12, 34)); // WHERE user_id IN (12, 34) |
||
353 | * $query->filterByUserId(array('min' => 12)); // WHERE user_id > 12 |
||
354 | * </code> |
||
355 | * |
||
356 | * @see filterByUser() |
||
357 | * |
||
358 | * @param mixed $userId The value to use as filter. |
||
359 | * Use scalar values for equality. |
||
360 | * Use array values for in_array() equivalent. |
||
361 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
362 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
363 | * |
||
364 | * @return $this|ChildConnectionQuery The current query, for fluid interface |
||
365 | */ |
||
366 | View Code Duplication | public function filterByUserId($userId = null, $comparison = null) |
|
388 | |||
389 | /** |
||
390 | * Filter the query on the peer column |
||
391 | * |
||
392 | * Example usage: |
||
393 | * <code> |
||
394 | * $query->filterByPeer('fooValue'); // WHERE peer = 'fooValue' |
||
395 | * $query->filterByPeer('%fooValue%'); // WHERE peer LIKE '%fooValue%' |
||
396 | * </code> |
||
397 | * |
||
398 | * @param string $peer The value to use as filter. |
||
399 | * Accepts wildcards (* and % trigger a LIKE) |
||
400 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
401 | * |
||
402 | * @return $this|ChildConnectionQuery The current query, for fluid interface |
||
403 | */ |
||
404 | View Code Duplication | public function filterByPeer($peer = null, $comparison = null) |
|
417 | |||
418 | /** |
||
419 | * Filter the query on the started column |
||
420 | * |
||
421 | * Example usage: |
||
422 | * <code> |
||
423 | * $query->filterByStarted('2011-03-14'); // WHERE started = '2011-03-14' |
||
424 | * $query->filterByStarted('now'); // WHERE started = '2011-03-14' |
||
425 | * $query->filterByStarted(array('max' => 'yesterday')); // WHERE started > '2011-03-13' |
||
426 | * </code> |
||
427 | * |
||
428 | * @param mixed $started The value to use as filter. |
||
429 | * Values can be integers (unix timestamps), DateTime objects, or strings. |
||
430 | * Empty strings are treated as NULL. |
||
431 | * Use scalar values for equality. |
||
432 | * Use array values for in_array() equivalent. |
||
433 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
434 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
435 | * |
||
436 | * @return $this|ChildConnectionQuery The current query, for fluid interface |
||
437 | */ |
||
438 | View Code Duplication | public function filterByStarted($started = null, $comparison = null) |
|
460 | |||
461 | /** |
||
462 | * Filter the query on the type column |
||
463 | * |
||
464 | * Example usage: |
||
465 | * <code> |
||
466 | * $query->filterByType('fooValue'); // WHERE type = 'fooValue' |
||
467 | * $query->filterByType('%fooValue%'); // WHERE type LIKE '%fooValue%' |
||
468 | * </code> |
||
469 | * |
||
470 | * @param string $type The value to use as filter. |
||
471 | * Accepts wildcards (* and % trigger a LIKE) |
||
472 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
473 | * |
||
474 | * @return $this|ChildConnectionQuery The current query, for fluid interface |
||
475 | */ |
||
476 | View Code Duplication | public function filterByType($type = null, $comparison = null) |
|
489 | |||
490 | /** |
||
491 | * Filter the query by a related \Jalle19\StatusManager\Database\Instance object |
||
492 | * |
||
493 | * @param \Jalle19\StatusManager\Database\Instance|ObjectCollection $instance The related object(s) to use as filter |
||
494 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
495 | * |
||
496 | * @throws \Propel\Runtime\Exception\PropelException |
||
497 | * |
||
498 | * @return ChildConnectionQuery The current query, for fluid interface |
||
499 | */ |
||
500 | View Code Duplication | public function filterByInstance($instance, $comparison = null) |
|
516 | |||
517 | /** |
||
518 | * Adds a JOIN clause to the query using the Instance relation |
||
519 | * |
||
520 | * @param string $relationAlias optional alias for the relation |
||
521 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
522 | * |
||
523 | * @return $this|ChildConnectionQuery The current query, for fluid interface |
||
524 | */ |
||
525 | View Code Duplication | public function joinInstance($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
|
548 | |||
549 | /** |
||
550 | * Use the Instance relation Instance object |
||
551 | * |
||
552 | * @see useQuery() |
||
553 | * |
||
554 | * @param string $relationAlias optional alias for the relation, |
||
555 | * to be used as main alias in the secondary query |
||
556 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
557 | * |
||
558 | * @return \Jalle19\StatusManager\Database\InstanceQuery A secondary query class using the current class as primary query |
||
559 | */ |
||
560 | public function useInstanceQuery($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
||
566 | |||
567 | /** |
||
568 | * Filter the query by a related \Jalle19\StatusManager\Database\User object |
||
569 | * |
||
570 | * @param \Jalle19\StatusManager\Database\User|ObjectCollection $user The related object(s) to use as filter |
||
571 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
572 | * |
||
573 | * @throws \Propel\Runtime\Exception\PropelException |
||
574 | * |
||
575 | * @return ChildConnectionQuery The current query, for fluid interface |
||
576 | */ |
||
577 | View Code Duplication | public function filterByUser($user, $comparison = null) |
|
593 | |||
594 | /** |
||
595 | * Adds a JOIN clause to the query using the User relation |
||
596 | * |
||
597 | * @param string $relationAlias optional alias for the relation |
||
598 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
599 | * |
||
600 | * @return $this|ChildConnectionQuery The current query, for fluid interface |
||
601 | */ |
||
602 | View Code Duplication | public function joinUser($relationAlias = null, $joinType = Criteria::LEFT_JOIN) |
|
625 | |||
626 | /** |
||
627 | * Use the User relation User object |
||
628 | * |
||
629 | * @see useQuery() |
||
630 | * |
||
631 | * @param string $relationAlias optional alias for the relation, |
||
632 | * to be used as main alias in the secondary query |
||
633 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
634 | * |
||
635 | * @return \Jalle19\StatusManager\Database\UserQuery A secondary query class using the current class as primary query |
||
636 | */ |
||
637 | public function useUserQuery($relationAlias = null, $joinType = Criteria::LEFT_JOIN) |
||
643 | |||
644 | /** |
||
645 | * Exclude object from result |
||
646 | * |
||
647 | * @param ChildConnection $connection Object to remove from the list of results |
||
648 | * |
||
649 | * @return $this|ChildConnectionQuery The current query, for fluid interface |
||
650 | */ |
||
651 | public function prune($connection = null) |
||
659 | |||
660 | /** |
||
661 | * Deletes all rows from the connection table. |
||
662 | * |
||
663 | * @param ConnectionInterface $con the connection to use |
||
664 | * @return int The number of affected rows (if supported by underlying database driver). |
||
665 | */ |
||
666 | View Code Duplication | public function doDeleteAll(ConnectionInterface $con = null) |
|
686 | |||
687 | /** |
||
688 | * Performs a DELETE on the database based on the current ModelCriteria |
||
689 | * |
||
690 | * @param ConnectionInterface $con the connection to use |
||
691 | * @return int The number of affected rows (if supported by underlying database driver). This includes CASCADE-related rows |
||
692 | * if supported by native driver or if emulated using Propel. |
||
693 | * @throws PropelException Any exceptions caught during processing will be |
||
694 | * rethrown wrapped into a PropelException. |
||
695 | */ |
||
696 | View Code Duplication | public function delete(ConnectionInterface $con = null) |
|
720 | |||
721 | } // ConnectionQuery |
||
722 |
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.