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 BannersQuery 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 BannersQuery, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
100 | abstract class BannersQuery extends ModelCriteria |
||
101 | { |
||
102 | protected $entityNotFoundExceptionClass = '\\Propel\\Runtime\\Exception\\EntityNotFoundException'; |
||
103 | |||
104 | /** |
||
105 | * Initializes internal state of \xbanners\models\Base\BannersQuery object. |
||
106 | * |
||
107 | * @param string $dbName The database name |
||
108 | * @param string $modelName The phpName of a model, e.g. 'Book' |
||
109 | * @param string $modelAlias The alias for the model in this query, e.g. 'b' |
||
110 | */ |
||
111 | public function __construct($dbName = 'Shop', $modelName = '\\xbanners\\models\\Banners', $modelAlias = null) |
||
115 | |||
116 | /** |
||
117 | * Returns a new ChildBannersQuery object. |
||
118 | * |
||
119 | * @param string $modelAlias The alias of a model in the query |
||
120 | * @param Criteria $criteria Optional Criteria to build the query from |
||
121 | * |
||
122 | * @return ChildBannersQuery |
||
123 | */ |
||
124 | View Code Duplication | public static function create($modelAlias = null, Criteria $criteria = null) |
|
139 | |||
140 | /** |
||
141 | * Find object by primary key. |
||
142 | * Propel uses the instance pool to skip the database if the object exists. |
||
143 | * Go fast if the query is untouched. |
||
144 | * |
||
145 | * <code> |
||
146 | * $obj = $c->findPk(12, $con); |
||
147 | * </code> |
||
148 | * |
||
149 | * @param mixed $key Primary key to use for the query |
||
150 | * @param ConnectionInterface $con an optional connection object |
||
151 | * |
||
152 | * @return ChildBanners|array|mixed the result, formatted by the current formatter |
||
153 | */ |
||
154 | View Code Duplication | public function findPk($key, ConnectionInterface $con = null) |
|
155 | { |
||
156 | if ($key === null) { |
||
157 | return null; |
||
158 | } |
||
159 | |||
160 | if ($con === null) { |
||
161 | $con = Propel::getServiceContainer()->getReadConnection(BannersTableMap::DATABASE_NAME); |
||
162 | } |
||
163 | |||
164 | $this->basePreSelect($con); |
||
165 | |||
166 | if ( |
||
167 | $this->formatter || $this->modelAlias || $this->with || $this->select |
||
168 | || $this->selectColumns || $this->asColumns || $this->selectModifiers |
||
169 | || $this->map || $this->having || $this->joins |
||
170 | ) { |
||
171 | return $this->findPkComplex($key, $con); |
||
172 | } |
||
173 | |||
174 | if ((null !== ($obj = BannersTableMap::getInstanceFromPool(null === $key || is_scalar($key) || is_callable([$key, '__toString']) ? (string) $key : $key)))) { |
||
175 | // the object is already in the instance pool |
||
176 | return $obj; |
||
177 | } |
||
178 | |||
179 | return $this->findPkSimple($key, $con); |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Find object by primary key using raw SQL to go fast. |
||
184 | * Bypass doSelect() and the object formatter by using generated code. |
||
185 | * |
||
186 | * @param mixed $key Primary key to use for the query |
||
187 | * @param ConnectionInterface $con A connection object |
||
188 | * |
||
189 | * @throws \Propel\Runtime\Exception\PropelException |
||
190 | * |
||
191 | * @return ChildBanners A model object, or null if the key is not found |
||
192 | */ |
||
193 | View Code Duplication | protected function findPkSimple($key, ConnectionInterface $con) |
|
215 | |||
216 | /** |
||
217 | * Find object by primary key. |
||
218 | * |
||
219 | * @param mixed $key Primary key to use for the query |
||
220 | * @param ConnectionInterface $con A connection object |
||
221 | * |
||
222 | * @return ChildBanners|array|mixed the result, formatted by the current formatter |
||
223 | */ |
||
224 | protected function findPkComplex($key, ConnectionInterface $con) |
||
225 | { |
||
226 | // As the query uses a PK condition, no limit(1) is necessary. |
||
227 | $criteria = $this->isKeepQuery() ? clone $this : $this; |
||
228 | $dataFetcher = $criteria |
||
229 | ->filterByPrimaryKey($key) |
||
230 | ->doSelect($con); |
||
231 | |||
232 | return $criteria->getFormatter()->init($criteria)->formatOne($dataFetcher); |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Find objects by primary key |
||
237 | * <code> |
||
238 | * $objs = $c->findPks(array(12, 56, 832), $con); |
||
239 | * </code> |
||
240 | * @param array $keys Primary keys to use for the query |
||
241 | * @param ConnectionInterface $con an optional connection object |
||
242 | * |
||
243 | * @return ObjectCollection|array|mixed the list of results, formatted by the current formatter |
||
244 | */ |
||
245 | public function findPks($keys, ConnectionInterface $con = null) |
||
246 | { |
||
247 | if (null === $con) { |
||
248 | $con = Propel::getServiceContainer()->getReadConnection($this->getDbName()); |
||
249 | } |
||
250 | $this->basePreSelect($con); |
||
251 | $criteria = $this->isKeepQuery() ? clone $this : $this; |
||
252 | $dataFetcher = $criteria |
||
253 | ->filterByPrimaryKeys($keys) |
||
254 | ->doSelect($con); |
||
255 | |||
256 | return $criteria->getFormatter()->init($criteria)->format($dataFetcher); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Filter the query by primary key |
||
261 | * |
||
262 | * @param mixed $key Primary key to use for the query |
||
263 | * |
||
264 | * @return $this|ChildBannersQuery The current query, for fluid interface |
||
265 | */ |
||
266 | public function filterByPrimaryKey($key) |
||
271 | |||
272 | /** |
||
273 | * Filter the query by a list of primary keys |
||
274 | * |
||
275 | * @param array $keys The list of primary key to use for the query |
||
276 | * |
||
277 | * @return $this|ChildBannersQuery The current query, for fluid interface |
||
278 | */ |
||
279 | public function filterByPrimaryKeys($keys) |
||
284 | |||
285 | /** |
||
286 | * Filter the query on the id column |
||
287 | * |
||
288 | * Example usage: |
||
289 | * <code> |
||
290 | * $query->filterById(1234); // WHERE id = 1234 |
||
291 | * $query->filterById(array(12, 34)); // WHERE id IN (12, 34) |
||
292 | * $query->filterById(array('min' => 12)); // WHERE id > 12 |
||
293 | * </code> |
||
294 | * |
||
295 | * @param mixed $id The value to use as filter. |
||
296 | * Use scalar values for equality. |
||
297 | * Use array values for in_array() equivalent. |
||
298 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
299 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
300 | * |
||
301 | * @return $this|ChildBannersQuery The current query, for fluid interface |
||
302 | */ |
||
303 | View Code Duplication | public function filterById($id = null, $comparison = null) |
|
325 | |||
326 | /** |
||
327 | * Filter the query on the place column |
||
328 | * |
||
329 | * Example usage: |
||
330 | * <code> |
||
331 | * $query->filterByPlace('fooValue'); // WHERE place = 'fooValue' |
||
332 | * $query->filterByPlace('%fooValue%'); // WHERE place LIKE '%fooValue%' |
||
333 | * </code> |
||
334 | * |
||
335 | * @param string $place The value to use as filter. |
||
336 | * Accepts wildcards (* and % trigger a LIKE) |
||
337 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
338 | * |
||
339 | * @return $this|ChildBannersQuery The current query, for fluid interface |
||
340 | */ |
||
341 | View Code Duplication | public function filterByPlace($place = null, $comparison = null) |
|
342 | { |
||
343 | if (null === $comparison) { |
||
344 | if (is_array($place)) { |
||
345 | $comparison = Criteria::IN; |
||
346 | } elseif (preg_match('/[\%\*]/', $place)) { |
||
347 | $place = str_replace('*', '%', $place); |
||
348 | $comparison = Criteria::LIKE; |
||
349 | } |
||
350 | } |
||
351 | |||
352 | return $this->addUsingAlias(BannersTableMap::COL_PLACE, $place, $comparison); |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * Filter the query on the width column |
||
357 | * |
||
358 | * Example usage: |
||
359 | * <code> |
||
360 | * $query->filterByWidth(1234); // WHERE width = 1234 |
||
361 | * $query->filterByWidth(array(12, 34)); // WHERE width IN (12, 34) |
||
362 | * $query->filterByWidth(array('min' => 12)); // WHERE width > 12 |
||
363 | * </code> |
||
364 | * |
||
365 | * @param mixed $width The value to use as filter. |
||
366 | * Use scalar values for equality. |
||
367 | * Use array values for in_array() equivalent. |
||
368 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
369 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
370 | * |
||
371 | * @return $this|ChildBannersQuery The current query, for fluid interface |
||
372 | */ |
||
373 | View Code Duplication | public function filterByWidth($width = null, $comparison = null) |
|
395 | |||
396 | /** |
||
397 | * Filter the query on the height column |
||
398 | * |
||
399 | * Example usage: |
||
400 | * <code> |
||
401 | * $query->filterByHeight(1234); // WHERE height = 1234 |
||
402 | * $query->filterByHeight(array(12, 34)); // WHERE height IN (12, 34) |
||
403 | * $query->filterByHeight(array('min' => 12)); // WHERE height > 12 |
||
404 | * </code> |
||
405 | * |
||
406 | * @param mixed $height The value to use as filter. |
||
407 | * Use scalar values for equality. |
||
408 | * Use array values for in_array() equivalent. |
||
409 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
410 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
411 | * |
||
412 | * @return $this|ChildBannersQuery The current query, for fluid interface |
||
413 | */ |
||
414 | View Code Duplication | public function filterByHeight($height = null, $comparison = null) |
|
436 | |||
437 | /** |
||
438 | * Filter the query on the effects column |
||
439 | * |
||
440 | * Example usage: |
||
441 | * <code> |
||
442 | * $query->filterByEffects('fooValue'); // WHERE effects = 'fooValue' |
||
443 | * $query->filterByEffects('%fooValue%'); // WHERE effects LIKE '%fooValue%' |
||
444 | * </code> |
||
445 | * |
||
446 | * @param string $effects The value to use as filter. |
||
447 | * Accepts wildcards (* and % trigger a LIKE) |
||
448 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
449 | * |
||
450 | * @return $this|ChildBannersQuery The current query, for fluid interface |
||
451 | */ |
||
452 | View Code Duplication | public function filterByEffects($effects = null, $comparison = null) |
|
453 | { |
||
454 | if (null === $comparison) { |
||
455 | if (is_array($effects)) { |
||
456 | $comparison = Criteria::IN; |
||
457 | } elseif (preg_match('/[\%\*]/', $effects)) { |
||
458 | $effects = str_replace('*', '%', $effects); |
||
459 | $comparison = Criteria::LIKE; |
||
460 | } |
||
461 | } |
||
462 | |||
463 | return $this->addUsingAlias(BannersTableMap::COL_EFFECTS, $effects, $comparison); |
||
464 | } |
||
465 | |||
466 | /** |
||
467 | * Filter the query on the page_type column |
||
468 | * |
||
469 | * Example usage: |
||
470 | * <code> |
||
471 | * $query->filterByPageType('fooValue'); // WHERE page_type = 'fooValue' |
||
472 | * $query->filterByPageType('%fooValue%'); // WHERE page_type LIKE '%fooValue%' |
||
473 | * </code> |
||
474 | * |
||
475 | * @param string $pageType The value to use as filter. |
||
476 | * Accepts wildcards (* and % trigger a LIKE) |
||
477 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
478 | * |
||
479 | * @return $this|ChildBannersQuery The current query, for fluid interface |
||
480 | */ |
||
481 | View Code Duplication | public function filterByPageType($pageType = null, $comparison = null) |
|
482 | { |
||
483 | if (null === $comparison) { |
||
484 | if (is_array($pageType)) { |
||
485 | $comparison = Criteria::IN; |
||
486 | } elseif (preg_match('/[\%\*]/', $pageType)) { |
||
487 | $pageType = str_replace('*', '%', $pageType); |
||
488 | $comparison = Criteria::LIKE; |
||
489 | } |
||
490 | } |
||
491 | |||
492 | return $this->addUsingAlias(BannersTableMap::COL_PAGE_TYPE, $pageType, $comparison); |
||
493 | } |
||
494 | |||
495 | /** |
||
496 | * Filter the query by a related \xbanners\models\BannerImage object |
||
497 | * |
||
498 | * @param \xbanners\models\BannerImage|ObjectCollection $bannerImage the related object to use as filter |
||
499 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
500 | * |
||
501 | * @return ChildBannersQuery The current query, for fluid interface |
||
502 | */ |
||
503 | View Code Duplication | public function filterByBannerImage($bannerImage, $comparison = null) |
|
517 | |||
518 | /** |
||
519 | * Adds a JOIN clause to the query using the BannerImage relation |
||
520 | * |
||
521 | * @param string $relationAlias optional alias for the relation |
||
522 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
523 | * |
||
524 | * @return $this|ChildBannersQuery The current query, for fluid interface |
||
525 | */ |
||
526 | public function joinBannerImage($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
||
527 | { |
||
528 | $tableMap = $this->getTableMap(); |
||
529 | $relationMap = $tableMap->getRelation('BannerImage'); |
||
530 | |||
531 | // create a ModelJoin object for this join |
||
532 | $join = new ModelJoin(); |
||
533 | $join->setJoinType($joinType); |
||
534 | $join->setRelationMap($relationMap, $this->useAliasInSQL ? $this->getModelAlias() : null, $relationAlias); |
||
535 | if ($previousJoin = $this->getPreviousJoin()) { |
||
536 | $join->setPreviousJoin($previousJoin); |
||
537 | } |
||
538 | |||
539 | // add the ModelJoin to the current object |
||
540 | if ($relationAlias) { |
||
541 | $this->addAlias($relationAlias, $relationMap->getRightTable()->getName()); |
||
542 | $this->addJoinObject($join, $relationAlias); |
||
543 | } else { |
||
544 | $this->addJoinObject($join, 'BannerImage'); |
||
545 | } |
||
546 | |||
547 | return $this; |
||
548 | } |
||
549 | |||
550 | /** |
||
551 | * Use the BannerImage relation BannerImage object |
||
552 | * |
||
553 | * @see useQuery() |
||
554 | * |
||
555 | * @param string $relationAlias optional alias for the relation, |
||
556 | * to be used as main alias in the secondary query |
||
557 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
558 | * |
||
559 | * @return \xbanners\models\BannerImageQuery A secondary query class using the current class as primary query |
||
560 | */ |
||
561 | public function useBannerImageQuery($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
||
567 | |||
568 | /** |
||
569 | * Filter the query by a related \xbanners\models\BannersI18n object |
||
570 | * |
||
571 | * @param \xbanners\models\BannersI18n|ObjectCollection $bannersI18n the related object to use as filter |
||
572 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
573 | * |
||
574 | * @return ChildBannersQuery The current query, for fluid interface |
||
575 | */ |
||
576 | View Code Duplication | public function filterByBannersI18n($bannersI18n, $comparison = null) |
|
590 | |||
591 | /** |
||
592 | * Adds a JOIN clause to the query using the BannersI18n relation |
||
593 | * |
||
594 | * @param string $relationAlias optional alias for the relation |
||
595 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
596 | * |
||
597 | * @return $this|ChildBannersQuery The current query, for fluid interface |
||
598 | */ |
||
599 | public function joinBannersI18n($relationAlias = null, $joinType = 'LEFT JOIN') |
||
600 | { |
||
601 | $tableMap = $this->getTableMap(); |
||
602 | $relationMap = $tableMap->getRelation('BannersI18n'); |
||
603 | |||
604 | // create a ModelJoin object for this join |
||
605 | $join = new ModelJoin(); |
||
606 | $join->setJoinType($joinType); |
||
607 | $join->setRelationMap($relationMap, $this->useAliasInSQL ? $this->getModelAlias() : null, $relationAlias); |
||
608 | if ($previousJoin = $this->getPreviousJoin()) { |
||
609 | $join->setPreviousJoin($previousJoin); |
||
610 | } |
||
611 | |||
612 | // add the ModelJoin to the current object |
||
613 | if ($relationAlias) { |
||
614 | $this->addAlias($relationAlias, $relationMap->getRightTable()->getName()); |
||
615 | $this->addJoinObject($join, $relationAlias); |
||
616 | } else { |
||
617 | $this->addJoinObject($join, 'BannersI18n'); |
||
618 | } |
||
619 | |||
620 | return $this; |
||
621 | } |
||
622 | |||
623 | /** |
||
624 | * Use the BannersI18n relation BannersI18n object |
||
625 | * |
||
626 | * @see useQuery() |
||
627 | * |
||
628 | * @param string $relationAlias optional alias for the relation, |
||
629 | * to be used as main alias in the secondary query |
||
630 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
631 | * |
||
632 | * @return \xbanners\models\BannersI18nQuery A secondary query class using the current class as primary query |
||
633 | */ |
||
634 | public function useBannersI18nQuery($relationAlias = null, $joinType = 'LEFT JOIN') |
||
640 | |||
641 | /** |
||
642 | * Exclude object from result |
||
643 | * |
||
644 | * @param ChildBanners $banners Object to remove from the list of results |
||
645 | * |
||
646 | * @return $this|ChildBannersQuery The current query, for fluid interface |
||
647 | */ |
||
648 | public function prune($banners = null) |
||
656 | |||
657 | /** |
||
658 | * Deletes all rows from the banners table. |
||
659 | * |
||
660 | * @param ConnectionInterface $con the connection to use |
||
661 | * @return int The number of affected rows (if supported by underlying database driver). |
||
662 | */ |
||
663 | public function doDeleteAll(ConnectionInterface $con = null) |
||
664 | { |
||
665 | if (null === $con) { |
||
666 | $con = Propel::getServiceContainer()->getWriteConnection(BannersTableMap::DATABASE_NAME); |
||
667 | } |
||
668 | |||
669 | // use transaction because $criteria could contain info |
||
670 | // for more than one table or we could emulating ON DELETE CASCADE, etc. |
||
671 | return $con->transaction(function () use ($con) { |
||
672 | $affectedRows = 0; // initialize var to track total num of affected rows |
||
673 | $affectedRows += $this->doOnDeleteCascade($con); |
||
674 | $affectedRows += parent::doDeleteAll($con); |
||
675 | // Because this db requires some delete cascade/set null emulation, we have to |
||
676 | // clear the cached instance *after* the emulation has happened (since |
||
677 | // instances get re-added by the select statement contained therein). |
||
678 | BannersTableMap::clearInstancePool(); |
||
679 | BannersTableMap::clearRelatedInstancePool(); |
||
680 | |||
681 | return $affectedRows; |
||
682 | }); |
||
683 | } |
||
684 | |||
685 | /** |
||
686 | * Performs a DELETE on the database based on the current ModelCriteria |
||
687 | * |
||
688 | * @param ConnectionInterface $con the connection to use |
||
689 | * @return int The number of affected rows (if supported by underlying database driver). This includes CASCADE-related rows |
||
690 | * if supported by native driver or if emulated using Propel. |
||
691 | * @throws PropelException Any exceptions caught during processing will be |
||
692 | * rethrown wrapped into a PropelException. |
||
693 | */ |
||
694 | View Code Duplication | public function delete(ConnectionInterface $con = null) |
|
722 | |||
723 | /** |
||
724 | * This is a method for emulating ON DELETE CASCADE for DBs that don't support this |
||
725 | * feature (like MySQL or SQLite). |
||
726 | * |
||
727 | * This method is not very speedy because it must perform a query first to get |
||
728 | * the implicated records and then perform the deletes by calling those Query classes. |
||
729 | * |
||
730 | * This method should be used within a transaction if possible. |
||
731 | * |
||
732 | * @param ConnectionInterface $con |
||
733 | * @return int The number of affected rows (if supported by underlying database driver). |
||
734 | */ |
||
735 | protected function doOnDeleteCascade(ConnectionInterface $con) |
||
760 | |||
761 | // i18n behavior |
||
762 | |||
763 | /** |
||
764 | * Adds a JOIN clause to the query using the i18n relation |
||
765 | * |
||
766 | * @param string $locale Locale to use for the join condition, e.g. 'fr_FR' |
||
767 | * @param string $relationAlias optional alias for the relation |
||
768 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join'. Defaults to left join. |
||
769 | * |
||
770 | * @return ChildBannersQuery The current query, for fluid interface |
||
771 | */ |
||
772 | View Code Duplication | public function joinI18n($locale = 'ru', $relationAlias = null, $joinType = Criteria::LEFT_JOIN) |
|
780 | |||
781 | /** |
||
782 | * Adds a JOIN clause to the query and hydrates the related I18n object. |
||
783 | * Shortcut for $c->joinI18n($locale)->with() |
||
784 | * |
||
785 | * @param string $locale Locale to use for the join condition, e.g. 'fr_FR' |
||
786 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join'. Defaults to left join. |
||
787 | * |
||
788 | * @return $this|ChildBannersQuery The current query, for fluid interface |
||
789 | */ |
||
790 | View Code Duplication | public function joinWithI18n($locale = 'ru', $joinType = Criteria::LEFT_JOIN) |
|
799 | |||
800 | /** |
||
801 | * Use the I18n relation query object |
||
802 | * |
||
803 | * @see useQuery() |
||
804 | * |
||
805 | * @param string $locale Locale to use for the join condition, e.g. 'fr_FR' |
||
806 | * @param string $relationAlias optional alias for the relation |
||
807 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join'. Defaults to left join. |
||
808 | * |
||
809 | * @return ChildBannersI18nQuery A secondary query class using the current class as primary query |
||
810 | */ |
||
811 | public function useI18nQuery($locale = 'ru', $relationAlias = null, $joinType = Criteria::LEFT_JOIN) |
||
817 | |||
818 | } // BannersQuery |
||
819 |