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 ChannelQuery 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 ChannelQuery, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
82 | abstract class ChannelQuery extends ModelCriteria |
||
83 | { |
||
84 | protected $entityNotFoundExceptionClass = '\\Propel\\Runtime\\Exception\\EntityNotFoundException'; |
||
85 | |||
86 | /** |
||
87 | * Initializes internal state of \Jalle19\StatusManager\Database\Base\ChannelQuery object. |
||
88 | * |
||
89 | * @param string $dbName The database name |
||
90 | * @param string $modelName The phpName of a model, e.g. 'Book' |
||
91 | * @param string $modelAlias The alias for the model in this query, e.g. 'b' |
||
92 | */ |
||
93 | public function __construct($dbName = 'tvheadend_status_manager', $modelName = '\\Jalle19\\StatusManager\\Database\\Channel', $modelAlias = null) |
||
97 | |||
98 | /** |
||
99 | * Returns a new ChildChannelQuery object. |
||
100 | * |
||
101 | * @param string $modelAlias The alias of a model in the query |
||
102 | * @param Criteria $criteria Optional Criteria to build the query from |
||
103 | * |
||
104 | * @return ChildChannelQuery |
||
105 | */ |
||
106 | View Code Duplication | public static function create($modelAlias = null, Criteria $criteria = null) |
|
121 | |||
122 | /** |
||
123 | * Find object by primary key. |
||
124 | * Propel uses the instance pool to skip the database if the object exists. |
||
125 | * Go fast if the query is untouched. |
||
126 | * |
||
127 | * <code> |
||
128 | * $obj = $c->findPk(12, $con); |
||
129 | * </code> |
||
130 | * |
||
131 | * @param mixed $key Primary key to use for the query |
||
132 | * @param ConnectionInterface $con an optional connection object |
||
133 | * |
||
134 | * @return ChildChannel|array|mixed the result, formatted by the current formatter |
||
135 | */ |
||
136 | View Code Duplication | public function findPk($key, ConnectionInterface $con = null) |
|
157 | |||
158 | /** |
||
159 | * Find object by primary key using raw SQL to go fast. |
||
160 | * Bypass doSelect() and the object formatter by using generated code. |
||
161 | * |
||
162 | * @param mixed $key Primary key to use for the query |
||
163 | * @param ConnectionInterface $con A connection object |
||
164 | * |
||
165 | * @throws \Propel\Runtime\Exception\PropelException |
||
166 | * |
||
167 | * @return ChildChannel A model object, or null if the key is not found |
||
168 | */ |
||
169 | View Code Duplication | protected function findPkSimple($key, ConnectionInterface $con) |
|
191 | |||
192 | /** |
||
193 | * Find object by primary key. |
||
194 | * |
||
195 | * @param mixed $key Primary key to use for the query |
||
196 | * @param ConnectionInterface $con A connection object |
||
197 | * |
||
198 | * @return ChildChannel|array|mixed the result, formatted by the current formatter |
||
199 | */ |
||
200 | View Code Duplication | protected function findPkComplex($key, ConnectionInterface $con) |
|
210 | |||
211 | /** |
||
212 | * Find objects by primary key |
||
213 | * <code> |
||
214 | * $objs = $c->findPks(array(12, 56, 832), $con); |
||
215 | * </code> |
||
216 | * @param array $keys Primary keys to use for the query |
||
217 | * @param ConnectionInterface $con an optional connection object |
||
218 | * |
||
219 | * @return ObjectCollection|array|mixed the list of results, formatted by the current formatter |
||
220 | */ |
||
221 | View Code Duplication | public function findPks($keys, ConnectionInterface $con = null) |
|
234 | |||
235 | /** |
||
236 | * Filter the query by primary key |
||
237 | * |
||
238 | * @param mixed $key Primary key to use for the query |
||
239 | * |
||
240 | * @return $this|ChildChannelQuery The current query, for fluid interface |
||
241 | */ |
||
242 | public function filterByPrimaryKey($key) |
||
247 | |||
248 | /** |
||
249 | * Filter the query by a list of primary keys |
||
250 | * |
||
251 | * @param array $keys The list of primary key to use for the query |
||
252 | * |
||
253 | * @return $this|ChildChannelQuery The current query, for fluid interface |
||
254 | */ |
||
255 | public function filterByPrimaryKeys($keys) |
||
260 | |||
261 | /** |
||
262 | * Filter the query on the id column |
||
263 | * |
||
264 | * Example usage: |
||
265 | * <code> |
||
266 | * $query->filterById(1234); // WHERE id = 1234 |
||
267 | * $query->filterById(array(12, 34)); // WHERE id IN (12, 34) |
||
268 | * $query->filterById(array('min' => 12)); // WHERE id > 12 |
||
269 | * </code> |
||
270 | * |
||
271 | * @param mixed $id The value to use as filter. |
||
272 | * Use scalar values for equality. |
||
273 | * Use array values for in_array() equivalent. |
||
274 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
275 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
276 | * |
||
277 | * @return $this|ChildChannelQuery The current query, for fluid interface |
||
278 | */ |
||
279 | View Code Duplication | public function filterById($id = null, $comparison = null) |
|
301 | |||
302 | /** |
||
303 | * Filter the query on the instance_name column |
||
304 | * |
||
305 | * Example usage: |
||
306 | * <code> |
||
307 | * $query->filterByInstanceName('fooValue'); // WHERE instance_name = 'fooValue' |
||
308 | * $query->filterByInstanceName('%fooValue%'); // WHERE instance_name LIKE '%fooValue%' |
||
309 | * </code> |
||
310 | * |
||
311 | * @param string $instanceName The value to use as filter. |
||
312 | * Accepts wildcards (* and % trigger a LIKE) |
||
313 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
314 | * |
||
315 | * @return $this|ChildChannelQuery The current query, for fluid interface |
||
316 | */ |
||
317 | View Code Duplication | public function filterByInstanceName($instanceName = null, $comparison = null) |
|
330 | |||
331 | /** |
||
332 | * Filter the query on the name column |
||
333 | * |
||
334 | * Example usage: |
||
335 | * <code> |
||
336 | * $query->filterByName('fooValue'); // WHERE name = 'fooValue' |
||
337 | * $query->filterByName('%fooValue%'); // WHERE name LIKE '%fooValue%' |
||
338 | * </code> |
||
339 | * |
||
340 | * @param string $name The value to use as filter. |
||
341 | * Accepts wildcards (* and % trigger a LIKE) |
||
342 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
343 | * |
||
344 | * @return $this|ChildChannelQuery The current query, for fluid interface |
||
345 | */ |
||
346 | View Code Duplication | public function filterByName($name = null, $comparison = null) |
|
359 | |||
360 | /** |
||
361 | * Filter the query by a related \Jalle19\StatusManager\Database\Instance object |
||
362 | * |
||
363 | * @param \Jalle19\StatusManager\Database\Instance|ObjectCollection $instance The related object(s) to use as filter |
||
364 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
365 | * |
||
366 | * @throws \Propel\Runtime\Exception\PropelException |
||
367 | * |
||
368 | * @return ChildChannelQuery The current query, for fluid interface |
||
369 | */ |
||
370 | View Code Duplication | public function filterByInstance($instance, $comparison = null) |
|
386 | |||
387 | /** |
||
388 | * Adds a JOIN clause to the query using the Instance relation |
||
389 | * |
||
390 | * @param string $relationAlias optional alias for the relation |
||
391 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
392 | * |
||
393 | * @return $this|ChildChannelQuery The current query, for fluid interface |
||
394 | */ |
||
395 | View Code Duplication | public function joinInstance($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
|
418 | |||
419 | /** |
||
420 | * Use the Instance relation Instance object |
||
421 | * |
||
422 | * @see useQuery() |
||
423 | * |
||
424 | * @param string $relationAlias optional alias for the relation, |
||
425 | * to be used as main alias in the secondary query |
||
426 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
427 | * |
||
428 | * @return \Jalle19\StatusManager\Database\InstanceQuery A secondary query class using the current class as primary query |
||
429 | */ |
||
430 | public function useInstanceQuery($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
||
436 | |||
437 | /** |
||
438 | * Filter the query by a related \Jalle19\StatusManager\Database\Subscription object |
||
439 | * |
||
440 | * @param \Jalle19\StatusManager\Database\Subscription|ObjectCollection $subscription the related object to use as filter |
||
441 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
442 | * |
||
443 | * @return ChildChannelQuery The current query, for fluid interface |
||
444 | */ |
||
445 | View Code Duplication | public function filterBySubscription($subscription, $comparison = null) |
|
459 | |||
460 | /** |
||
461 | * Adds a JOIN clause to the query using the Subscription relation |
||
462 | * |
||
463 | * @param string $relationAlias optional alias for the relation |
||
464 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
465 | * |
||
466 | * @return $this|ChildChannelQuery The current query, for fluid interface |
||
467 | */ |
||
468 | View Code Duplication | public function joinSubscription($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
|
491 | |||
492 | /** |
||
493 | * Use the Subscription relation Subscription object |
||
494 | * |
||
495 | * @see useQuery() |
||
496 | * |
||
497 | * @param string $relationAlias optional alias for the relation, |
||
498 | * to be used as main alias in the secondary query |
||
499 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
500 | * |
||
501 | * @return \Jalle19\StatusManager\Database\SubscriptionQuery A secondary query class using the current class as primary query |
||
502 | */ |
||
503 | public function useSubscriptionQuery($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
||
509 | |||
510 | /** |
||
511 | * Exclude object from result |
||
512 | * |
||
513 | * @param ChildChannel $channel Object to remove from the list of results |
||
514 | * |
||
515 | * @return $this|ChildChannelQuery The current query, for fluid interface |
||
516 | */ |
||
517 | public function prune($channel = null) |
||
525 | |||
526 | /** |
||
527 | * Deletes all rows from the channel table. |
||
528 | * |
||
529 | * @param ConnectionInterface $con the connection to use |
||
530 | * @return int The number of affected rows (if supported by underlying database driver). |
||
531 | */ |
||
532 | View Code Duplication | public function doDeleteAll(ConnectionInterface $con = null) |
|
552 | |||
553 | /** |
||
554 | * Performs a DELETE on the database based on the current ModelCriteria |
||
555 | * |
||
556 | * @param ConnectionInterface $con the connection to use |
||
557 | * @return int The number of affected rows (if supported by underlying database driver). This includes CASCADE-related rows |
||
558 | * if supported by native driver or if emulated using Propel. |
||
559 | * @throws PropelException Any exceptions caught during processing will be |
||
560 | * rethrown wrapped into a PropelException. |
||
561 | */ |
||
562 | View Code Duplication | public function delete(ConnectionInterface $con = null) |
|
586 | |||
587 | } // ChannelQuery |
||
588 |
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.