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 InstanceQuery 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 InstanceQuery, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
92 | abstract class InstanceQuery extends ModelCriteria |
||
93 | { |
||
94 | protected $entityNotFoundExceptionClass = '\\Propel\\Runtime\\Exception\\EntityNotFoundException'; |
||
95 | |||
96 | /** |
||
97 | * Initializes internal state of \Jalle19\StatusManager\Database\Base\InstanceQuery object. |
||
98 | * |
||
99 | * @param string $dbName The database name |
||
100 | * @param string $modelName The phpName of a model, e.g. 'Book' |
||
101 | * @param string $modelAlias The alias for the model in this query, e.g. 'b' |
||
102 | */ |
||
103 | public function __construct($dbName = 'tvheadend_status_manager', $modelName = '\\Jalle19\\StatusManager\\Database\\Instance', $modelAlias = null) |
||
107 | |||
108 | /** |
||
109 | * Returns a new ChildInstanceQuery object. |
||
110 | * |
||
111 | * @param string $modelAlias The alias of a model in the query |
||
112 | * @param Criteria $criteria Optional Criteria to build the query from |
||
113 | * |
||
114 | * @return ChildInstanceQuery |
||
115 | */ |
||
116 | View Code Duplication | public static function create($modelAlias = null, Criteria $criteria = null) |
|
131 | |||
132 | /** |
||
133 | * Find object by primary key. |
||
134 | * Propel uses the instance pool to skip the database if the object exists. |
||
135 | * Go fast if the query is untouched. |
||
136 | * |
||
137 | * <code> |
||
138 | * $obj = $c->findPk(12, $con); |
||
139 | * </code> |
||
140 | * |
||
141 | * @param mixed $key Primary key to use for the query |
||
142 | * @param ConnectionInterface $con an optional connection object |
||
143 | * |
||
144 | * @return ChildInstance|array|mixed the result, formatted by the current formatter |
||
145 | */ |
||
146 | View Code Duplication | public function findPk($key, ConnectionInterface $con = null) |
|
167 | |||
168 | /** |
||
169 | * Find object by primary key using raw SQL to go fast. |
||
170 | * Bypass doSelect() and the object formatter by using generated code. |
||
171 | * |
||
172 | * @param mixed $key Primary key to use for the query |
||
173 | * @param ConnectionInterface $con A connection object |
||
174 | * |
||
175 | * @throws \Propel\Runtime\Exception\PropelException |
||
176 | * |
||
177 | * @return ChildInstance A model object, or null if the key is not found |
||
178 | */ |
||
179 | View Code Duplication | protected function findPkSimple($key, ConnectionInterface $con) |
|
201 | |||
202 | /** |
||
203 | * Find object by primary key. |
||
204 | * |
||
205 | * @param mixed $key Primary key to use for the query |
||
206 | * @param ConnectionInterface $con A connection object |
||
207 | * |
||
208 | * @return ChildInstance|array|mixed the result, formatted by the current formatter |
||
209 | */ |
||
210 | View Code Duplication | protected function findPkComplex($key, ConnectionInterface $con) |
|
220 | |||
221 | /** |
||
222 | * Find objects by primary key |
||
223 | * <code> |
||
224 | * $objs = $c->findPks(array(12, 56, 832), $con); |
||
225 | * </code> |
||
226 | * @param array $keys Primary keys to use for the query |
||
227 | * @param ConnectionInterface $con an optional connection object |
||
228 | * |
||
229 | * @return ObjectCollection|array|mixed the list of results, formatted by the current formatter |
||
230 | */ |
||
231 | View Code Duplication | public function findPks($keys, ConnectionInterface $con = null) |
|
244 | |||
245 | /** |
||
246 | * Filter the query by primary key |
||
247 | * |
||
248 | * @param mixed $key Primary key to use for the query |
||
249 | * |
||
250 | * @return $this|ChildInstanceQuery The current query, for fluid interface |
||
251 | */ |
||
252 | public function filterByPrimaryKey($key) |
||
257 | |||
258 | /** |
||
259 | * Filter the query by a list of primary keys |
||
260 | * |
||
261 | * @param array $keys The list of primary key to use for the query |
||
262 | * |
||
263 | * @return $this|ChildInstanceQuery The current query, for fluid interface |
||
264 | */ |
||
265 | public function filterByPrimaryKeys($keys) |
||
270 | |||
271 | /** |
||
272 | * Filter the query on the name column |
||
273 | * |
||
274 | * Example usage: |
||
275 | * <code> |
||
276 | * $query->filterByName('fooValue'); // WHERE name = 'fooValue' |
||
277 | * $query->filterByName('%fooValue%'); // WHERE name LIKE '%fooValue%' |
||
278 | * </code> |
||
279 | * |
||
280 | * @param string $name The value to use as filter. |
||
281 | * Accepts wildcards (* and % trigger a LIKE) |
||
282 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
283 | * |
||
284 | * @return $this|ChildInstanceQuery The current query, for fluid interface |
||
285 | */ |
||
286 | View Code Duplication | public function filterByName($name = null, $comparison = null) |
|
299 | |||
300 | /** |
||
301 | * Filter the query by a related \Jalle19\StatusManager\Database\User object |
||
302 | * |
||
303 | * @param \Jalle19\StatusManager\Database\User|ObjectCollection $user the related object to use as filter |
||
304 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
305 | * |
||
306 | * @return ChildInstanceQuery The current query, for fluid interface |
||
307 | */ |
||
308 | View Code Duplication | public function filterByUser($user, $comparison = null) |
|
322 | |||
323 | /** |
||
324 | * Adds a JOIN clause to the query using the User relation |
||
325 | * |
||
326 | * @param string $relationAlias optional alias for the relation |
||
327 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
328 | * |
||
329 | * @return $this|ChildInstanceQuery The current query, for fluid interface |
||
330 | */ |
||
331 | View Code Duplication | public function joinUser($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
|
354 | |||
355 | /** |
||
356 | * Use the User relation User object |
||
357 | * |
||
358 | * @see useQuery() |
||
359 | * |
||
360 | * @param string $relationAlias optional alias for the relation, |
||
361 | * to be used as main alias in the secondary query |
||
362 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
363 | * |
||
364 | * @return \Jalle19\StatusManager\Database\UserQuery A secondary query class using the current class as primary query |
||
365 | */ |
||
366 | public function useUserQuery($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
||
372 | |||
373 | /** |
||
374 | * Filter the query by a related \Jalle19\StatusManager\Database\Connection object |
||
375 | * |
||
376 | * @param \Jalle19\StatusManager\Database\Connection|ObjectCollection $connection the related object to use as filter |
||
377 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
378 | * |
||
379 | * @return ChildInstanceQuery The current query, for fluid interface |
||
380 | */ |
||
381 | View Code Duplication | public function filterByConnection($connection, $comparison = null) |
|
395 | |||
396 | /** |
||
397 | * Adds a JOIN clause to the query using the Connection relation |
||
398 | * |
||
399 | * @param string $relationAlias optional alias for the relation |
||
400 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
401 | * |
||
402 | * @return $this|ChildInstanceQuery The current query, for fluid interface |
||
403 | */ |
||
404 | View Code Duplication | public function joinConnection($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
|
427 | |||
428 | /** |
||
429 | * Use the Connection relation Connection object |
||
430 | * |
||
431 | * @see useQuery() |
||
432 | * |
||
433 | * @param string $relationAlias optional alias for the relation, |
||
434 | * to be used as main alias in the secondary query |
||
435 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
436 | * |
||
437 | * @return \Jalle19\StatusManager\Database\ConnectionQuery A secondary query class using the current class as primary query |
||
438 | */ |
||
439 | public function useConnectionQuery($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
||
445 | |||
446 | /** |
||
447 | * Filter the query by a related \Jalle19\StatusManager\Database\Channel object |
||
448 | * |
||
449 | * @param \Jalle19\StatusManager\Database\Channel|ObjectCollection $channel the related object to use as filter |
||
450 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
451 | * |
||
452 | * @return ChildInstanceQuery The current query, for fluid interface |
||
453 | */ |
||
454 | View Code Duplication | public function filterByChannel($channel, $comparison = null) |
|
468 | |||
469 | /** |
||
470 | * Adds a JOIN clause to the query using the Channel relation |
||
471 | * |
||
472 | * @param string $relationAlias optional alias for the relation |
||
473 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
474 | * |
||
475 | * @return $this|ChildInstanceQuery The current query, for fluid interface |
||
476 | */ |
||
477 | View Code Duplication | public function joinChannel($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
|
500 | |||
501 | /** |
||
502 | * Use the Channel relation Channel object |
||
503 | * |
||
504 | * @see useQuery() |
||
505 | * |
||
506 | * @param string $relationAlias optional alias for the relation, |
||
507 | * to be used as main alias in the secondary query |
||
508 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
509 | * |
||
510 | * @return \Jalle19\StatusManager\Database\ChannelQuery A secondary query class using the current class as primary query |
||
511 | */ |
||
512 | public function useChannelQuery($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
||
518 | |||
519 | /** |
||
520 | * Filter the query by a related \Jalle19\StatusManager\Database\Subscription object |
||
521 | * |
||
522 | * @param \Jalle19\StatusManager\Database\Subscription|ObjectCollection $subscription the related object to use as filter |
||
523 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
524 | * |
||
525 | * @return ChildInstanceQuery The current query, for fluid interface |
||
526 | */ |
||
527 | View Code Duplication | public function filterBySubscription($subscription, $comparison = null) |
|
541 | |||
542 | /** |
||
543 | * Adds a JOIN clause to the query using the Subscription relation |
||
544 | * |
||
545 | * @param string $relationAlias optional alias for the relation |
||
546 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
547 | * |
||
548 | * @return $this|ChildInstanceQuery The current query, for fluid interface |
||
549 | */ |
||
550 | View Code Duplication | public function joinSubscription($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
|
573 | |||
574 | /** |
||
575 | * Use the Subscription relation Subscription object |
||
576 | * |
||
577 | * @see useQuery() |
||
578 | * |
||
579 | * @param string $relationAlias optional alias for the relation, |
||
580 | * to be used as main alias in the secondary query |
||
581 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
582 | * |
||
583 | * @return \Jalle19\StatusManager\Database\SubscriptionQuery A secondary query class using the current class as primary query |
||
584 | */ |
||
585 | public function useSubscriptionQuery($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
||
591 | |||
592 | /** |
||
593 | * Exclude object from result |
||
594 | * |
||
595 | * @param ChildInstance $instance Object to remove from the list of results |
||
596 | * |
||
597 | * @return $this|ChildInstanceQuery The current query, for fluid interface |
||
598 | */ |
||
599 | public function prune($instance = null) |
||
607 | |||
608 | /** |
||
609 | * Deletes all rows from the instance table. |
||
610 | * |
||
611 | * @param ConnectionInterface $con the connection to use |
||
612 | * @return int The number of affected rows (if supported by underlying database driver). |
||
613 | */ |
||
614 | View Code Duplication | public function doDeleteAll(ConnectionInterface $con = null) |
|
634 | |||
635 | /** |
||
636 | * Performs a DELETE on the database based on the current ModelCriteria |
||
637 | * |
||
638 | * @param ConnectionInterface $con the connection to use |
||
639 | * @return int The number of affected rows (if supported by underlying database driver). This includes CASCADE-related rows |
||
640 | * if supported by native driver or if emulated using Propel. |
||
641 | * @throws PropelException Any exceptions caught during processing will be |
||
642 | * rethrown wrapped into a PropelException. |
||
643 | */ |
||
644 | View Code Duplication | public function delete(ConnectionInterface $con = null) |
|
668 | |||
669 | } // InstanceQuery |
||
670 |
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.