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) |
||
94 | { |
||
95 | parent::__construct($dbName, $modelName, $modelAlias); |
||
96 | } |
||
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 | public static function create($modelAlias = null, Criteria $criteria = null) |
||
107 | { |
||
108 | if ($criteria instanceof ChildChannelQuery) { |
||
109 | return $criteria; |
||
110 | } |
||
111 | $query = new ChildChannelQuery(); |
||
112 | if (null !== $modelAlias) { |
||
113 | $query->setModelAlias($modelAlias); |
||
114 | } |
||
115 | if ($criteria instanceof Criteria) { |
||
116 | $query->mergeWith($criteria); |
||
117 | } |
||
118 | |||
119 | return $query; |
||
120 | } |
||
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 | public function findPk($key, ConnectionInterface $con = null) |
||
137 | { |
||
138 | if ($key === null) { |
||
139 | return null; |
||
140 | } |
||
141 | if ((null !== ($obj = ChannelTableMap::getInstanceFromPool(null === $key || is_scalar($key) || is_callable([$key, '__toString']) ? (string) $key : $key))) && !$this->formatter) { |
||
142 | // the object is already in the instance pool |
||
143 | return $obj; |
||
144 | } |
||
145 | if ($con === null) { |
||
146 | $con = Propel::getServiceContainer()->getReadConnection(ChannelTableMap::DATABASE_NAME); |
||
147 | } |
||
148 | $this->basePreSelect($con); |
||
149 | if ($this->formatter || $this->modelAlias || $this->with || $this->select |
||
150 | || $this->selectColumns || $this->asColumns || $this->selectModifiers |
||
151 | || $this->map || $this->having || $this->joins) { |
||
152 | return $this->findPkComplex($key, $con); |
||
153 | } else { |
||
154 | return $this->findPkSimple($key, $con); |
||
155 | } |
||
156 | } |
||
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 | protected function findPkSimple($key, ConnectionInterface $con) |
||
170 | { |
||
171 | $sql = 'SELECT id, instance_name, name FROM channel WHERE id = :p0'; |
||
172 | try { |
||
173 | $stmt = $con->prepare($sql); |
||
174 | $stmt->bindValue(':p0', $key, PDO::PARAM_INT); |
||
175 | $stmt->execute(); |
||
176 | } catch (Exception $e) { |
||
177 | Propel::log($e->getMessage(), Propel::LOG_ERR); |
||
178 | throw new PropelException(sprintf('Unable to execute SELECT statement [%s]', $sql), 0, $e); |
||
179 | } |
||
180 | $obj = null; |
||
181 | if ($row = $stmt->fetch(\PDO::FETCH_NUM)) { |
||
182 | /** @var ChildChannel $obj */ |
||
183 | $obj = new ChildChannel(); |
||
184 | $obj->hydrate($row); |
||
185 | ChannelTableMap::addInstanceToPool($obj, null === $key || is_scalar($key) || is_callable([$key, '__toString']) ? (string) $key : $key); |
||
186 | } |
||
187 | $stmt->closeCursor(); |
||
188 | |||
189 | return $obj; |
||
190 | } |
||
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 | protected function findPkComplex($key, ConnectionInterface $con) |
||
201 | { |
||
202 | // As the query uses a PK condition, no limit(1) is necessary. |
||
203 | $criteria = $this->isKeepQuery() ? clone $this : $this; |
||
204 | $dataFetcher = $criteria |
||
205 | ->filterByPrimaryKey($key) |
||
206 | ->doSelect($con); |
||
207 | |||
208 | return $criteria->getFormatter()->init($criteria)->formatOne($dataFetcher); |
||
209 | } |
||
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 | public function findPks($keys, ConnectionInterface $con = null) |
||
222 | { |
||
223 | if (null === $con) { |
||
224 | $con = Propel::getServiceContainer()->getReadConnection($this->getDbName()); |
||
225 | } |
||
226 | $this->basePreSelect($con); |
||
227 | $criteria = $this->isKeepQuery() ? clone $this : $this; |
||
228 | $dataFetcher = $criteria |
||
229 | ->filterByPrimaryKeys($keys) |
||
230 | ->doSelect($con); |
||
231 | |||
232 | return $criteria->getFormatter()->init($criteria)->format($dataFetcher); |
||
233 | } |
||
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) |
||
243 | { |
||
244 | |||
245 | return $this->addUsingAlias(ChannelTableMap::COL_ID, $key, Criteria::EQUAL); |
||
246 | } |
||
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) |
||
256 | { |
||
257 | |||
258 | return $this->addUsingAlias(ChannelTableMap::COL_ID, $keys, Criteria::IN); |
||
259 | } |
||
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 | public function filterById($id = null, $comparison = null) |
||
280 | { |
||
281 | if (is_array($id)) { |
||
282 | $useMinMax = false; |
||
283 | if (isset($id['min'])) { |
||
284 | $this->addUsingAlias(ChannelTableMap::COL_ID, $id['min'], Criteria::GREATER_EQUAL); |
||
285 | $useMinMax = true; |
||
286 | } |
||
287 | if (isset($id['max'])) { |
||
288 | $this->addUsingAlias(ChannelTableMap::COL_ID, $id['max'], Criteria::LESS_EQUAL); |
||
289 | $useMinMax = true; |
||
290 | } |
||
291 | if ($useMinMax) { |
||
292 | return $this; |
||
293 | } |
||
294 | if (null === $comparison) { |
||
295 | $comparison = Criteria::IN; |
||
296 | } |
||
297 | } |
||
298 | |||
299 | return $this->addUsingAlias(ChannelTableMap::COL_ID, $id, $comparison); |
||
300 | } |
||
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 | public function filterByInstanceName($instanceName = null, $comparison = null) |
||
318 | { |
||
319 | if (null === $comparison) { |
||
320 | if (is_array($instanceName)) { |
||
321 | $comparison = Criteria::IN; |
||
322 | } elseif (preg_match('/[\%\*]/', $instanceName)) { |
||
323 | $instanceName = str_replace('*', '%', $instanceName); |
||
324 | $comparison = Criteria::LIKE; |
||
325 | } |
||
326 | } |
||
327 | |||
328 | return $this->addUsingAlias(ChannelTableMap::COL_INSTANCE_NAME, $instanceName, $comparison); |
||
329 | } |
||
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 | public function filterByName($name = null, $comparison = null) |
||
347 | { |
||
348 | if (null === $comparison) { |
||
349 | if (is_array($name)) { |
||
350 | $comparison = Criteria::IN; |
||
351 | } elseif (preg_match('/[\%\*]/', $name)) { |
||
352 | $name = str_replace('*', '%', $name); |
||
353 | $comparison = Criteria::LIKE; |
||
354 | } |
||
355 | } |
||
356 | |||
357 | return $this->addUsingAlias(ChannelTableMap::COL_NAME, $name, $comparison); |
||
358 | } |
||
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 | public function filterByInstance($instance, $comparison = null) |
||
371 | { |
||
372 | if ($instance instanceof \Jalle19\StatusManager\Database\Instance) { |
||
373 | return $this |
||
374 | ->addUsingAlias(ChannelTableMap::COL_INSTANCE_NAME, $instance->getName(), $comparison); |
||
375 | } elseif ($instance instanceof ObjectCollection) { |
||
376 | if (null === $comparison) { |
||
377 | $comparison = Criteria::IN; |
||
378 | } |
||
379 | |||
380 | return $this |
||
381 | ->addUsingAlias(ChannelTableMap::COL_INSTANCE_NAME, $instance->toKeyValue('PrimaryKey', 'Name'), $comparison); |
||
382 | } else { |
||
383 | throw new PropelException('filterByInstance() only accepts arguments of type \Jalle19\StatusManager\Database\Instance or Collection'); |
||
384 | } |
||
385 | } |
||
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 | public function joinInstance($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
||
396 | { |
||
397 | $tableMap = $this->getTableMap(); |
||
398 | $relationMap = $tableMap->getRelation('Instance'); |
||
399 | |||
400 | // create a ModelJoin object for this join |
||
401 | $join = new ModelJoin(); |
||
402 | $join->setJoinType($joinType); |
||
403 | $join->setRelationMap($relationMap, $this->useAliasInSQL ? $this->getModelAlias() : null, $relationAlias); |
||
404 | if ($previousJoin = $this->getPreviousJoin()) { |
||
405 | $join->setPreviousJoin($previousJoin); |
||
406 | } |
||
407 | |||
408 | // add the ModelJoin to the current object |
||
409 | if ($relationAlias) { |
||
410 | $this->addAlias($relationAlias, $relationMap->getRightTable()->getName()); |
||
411 | $this->addJoinObject($join, $relationAlias); |
||
412 | } else { |
||
413 | $this->addJoinObject($join, 'Instance'); |
||
414 | } |
||
415 | |||
416 | return $this; |
||
417 | } |
||
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) |
||
431 | { |
||
432 | return $this |
||
433 | ->joinInstance($relationAlias, $joinType) |
||
434 | ->useQuery($relationAlias ? $relationAlias : 'Instance', '\Jalle19\StatusManager\Database\InstanceQuery'); |
||
435 | } |
||
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 | public function filterBySubscription($subscription, $comparison = null) |
||
446 | { |
||
447 | if ($subscription instanceof \Jalle19\StatusManager\Database\Subscription) { |
||
448 | return $this |
||
449 | ->addUsingAlias(ChannelTableMap::COL_ID, $subscription->getChannelId(), $comparison); |
||
450 | } elseif ($subscription instanceof ObjectCollection) { |
||
451 | return $this |
||
452 | ->useSubscriptionQuery() |
||
453 | ->filterByPrimaryKeys($subscription->getPrimaryKeys()) |
||
454 | ->endUse(); |
||
455 | } else { |
||
456 | throw new PropelException('filterBySubscription() only accepts arguments of type \Jalle19\StatusManager\Database\Subscription or Collection'); |
||
457 | } |
||
458 | } |
||
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 | public function joinSubscription($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
||
469 | { |
||
470 | $tableMap = $this->getTableMap(); |
||
471 | $relationMap = $tableMap->getRelation('Subscription'); |
||
472 | |||
473 | // create a ModelJoin object for this join |
||
474 | $join = new ModelJoin(); |
||
475 | $join->setJoinType($joinType); |
||
476 | $join->setRelationMap($relationMap, $this->useAliasInSQL ? $this->getModelAlias() : null, $relationAlias); |
||
477 | if ($previousJoin = $this->getPreviousJoin()) { |
||
478 | $join->setPreviousJoin($previousJoin); |
||
479 | } |
||
480 | |||
481 | // add the ModelJoin to the current object |
||
482 | if ($relationAlias) { |
||
483 | $this->addAlias($relationAlias, $relationMap->getRightTable()->getName()); |
||
484 | $this->addJoinObject($join, $relationAlias); |
||
485 | } else { |
||
486 | $this->addJoinObject($join, 'Subscription'); |
||
487 | } |
||
488 | |||
489 | return $this; |
||
490 | } |
||
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) |
||
504 | { |
||
505 | return $this |
||
506 | ->joinSubscription($relationAlias, $joinType) |
||
507 | ->useQuery($relationAlias ? $relationAlias : 'Subscription', '\Jalle19\StatusManager\Database\SubscriptionQuery'); |
||
508 | } |
||
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) |
||
518 | { |
||
519 | if ($channel) { |
||
520 | $this->addUsingAlias(ChannelTableMap::COL_ID, $channel->getId(), Criteria::NOT_EQUAL); |
||
521 | } |
||
522 | |||
523 | return $this; |
||
524 | } |
||
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 | public function doDeleteAll(ConnectionInterface $con = null) |
||
533 | { |
||
534 | if (null === $con) { |
||
535 | $con = Propel::getServiceContainer()->getWriteConnection(ChannelTableMap::DATABASE_NAME); |
||
536 | } |
||
537 | |||
538 | // use transaction because $criteria could contain info |
||
539 | // for more than one table or we could emulating ON DELETE CASCADE, etc. |
||
540 | return $con->transaction(function () use ($con) { |
||
541 | $affectedRows = 0; // initialize var to track total num of affected rows |
||
542 | $affectedRows += parent::doDeleteAll($con); |
||
543 | // Because this db requires some delete cascade/set null emulation, we have to |
||
544 | // clear the cached instance *after* the emulation has happened (since |
||
545 | // instances get re-added by the select statement contained therein). |
||
546 | ChannelTableMap::clearInstancePool(); |
||
547 | ChannelTableMap::clearRelatedInstancePool(); |
||
548 | |||
549 | return $affectedRows; |
||
550 | }); |
||
551 | } |
||
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 | public function delete(ConnectionInterface $con = null) |
||
563 | { |
||
564 | if (null === $con) { |
||
565 | $con = Propel::getServiceContainer()->getWriteConnection(ChannelTableMap::DATABASE_NAME); |
||
566 | } |
||
567 | |||
568 | $criteria = $this; |
||
569 | |||
570 | // Set the correct dbName |
||
571 | $criteria->setDbName(ChannelTableMap::DATABASE_NAME); |
||
572 | |||
573 | // use transaction because $criteria could contain info |
||
574 | // for more than one table or we could emulating ON DELETE CASCADE, etc. |
||
575 | return $con->transaction(function () use ($con, $criteria) { |
||
576 | $affectedRows = 0; // initialize var to track total num of affected rows |
||
577 | |||
578 | ChannelTableMap::removeInstanceFromPool($criteria); |
||
579 | |||
580 | $affectedRows += ModelCriteria::delete($con); |
||
581 | ChannelTableMap::clearRelatedInstancePool(); |
||
582 | |||
583 | return $affectedRows; |
||
584 | }); |
||
585 | } |
||
586 | |||
587 | } // ChannelQuery |
||
588 |