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 RecordQuery 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 RecordQuery, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 108 | abstract class RecordQuery extends ModelCriteria |
||
| 109 | { |
||
| 110 | protected $entityNotFoundExceptionClass = '\\Propel\\Runtime\\Exception\\EntityNotFoundException'; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Initializes internal state of \eXpansion\Bundle\LocalRecords\Model\Base\RecordQuery object. |
||
| 114 | * |
||
| 115 | * @param string $dbName The database name |
||
| 116 | * @param string $modelName The phpName of a model, e.g. 'Book' |
||
| 117 | * @param string $modelAlias The alias for the model in this query, e.g. 'b' |
||
| 118 | */ |
||
| 119 | public function __construct($dbName = 'expansion', $modelName = '\\eXpansion\\Bundle\\LocalRecords\\Model\\Record', $modelAlias = null) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Returns a new ChildRecordQuery object. |
||
| 126 | * |
||
| 127 | * @param string $modelAlias The alias of a model in the query |
||
| 128 | * @param Criteria $criteria Optional Criteria to build the query from |
||
| 129 | * |
||
| 130 | * @return ChildRecordQuery |
||
| 131 | */ |
||
| 132 | View Code Duplication | public static function create($modelAlias = null, Criteria $criteria = null) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Find object by primary key. |
||
| 150 | * Propel uses the instance pool to skip the database if the object exists. |
||
| 151 | * Go fast if the query is untouched. |
||
| 152 | * |
||
| 153 | * <code> |
||
| 154 | * $obj = $c->findPk(12, $con); |
||
| 155 | * </code> |
||
| 156 | * |
||
| 157 | * @param mixed $key Primary key to use for the query |
||
| 158 | * @param ConnectionInterface $con an optional connection object |
||
| 159 | * |
||
| 160 | * @return ChildRecord|array|mixed the result, formatted by the current formatter |
||
| 161 | */ |
||
| 162 | View Code Duplication | public function findPk($key, ConnectionInterface $con = null) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Find object by primary key using raw SQL to go fast. |
||
| 192 | * Bypass doSelect() and the object formatter by using generated code. |
||
| 193 | * |
||
| 194 | * @param mixed $key Primary key to use for the query |
||
| 195 | * @param ConnectionInterface $con A connection object |
||
| 196 | * |
||
| 197 | * @throws \Propel\Runtime\Exception\PropelException |
||
| 198 | * |
||
| 199 | * @return ChildRecord A model object, or null if the key is not found |
||
| 200 | */ |
||
| 201 | View Code Duplication | protected function findPkSimple($key, ConnectionInterface $con) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Find object by primary key. |
||
| 226 | * |
||
| 227 | * @param mixed $key Primary key to use for the query |
||
| 228 | * @param ConnectionInterface $con A connection object |
||
| 229 | * |
||
| 230 | * @return ChildRecord|array|mixed the result, formatted by the current formatter |
||
| 231 | */ |
||
| 232 | View Code Duplication | protected function findPkComplex($key, ConnectionInterface $con) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Find objects by primary key |
||
| 245 | * <code> |
||
| 246 | * $objs = $c->findPks(array(12, 56, 832), $con); |
||
| 247 | * </code> |
||
| 248 | * @param array $keys Primary keys to use for the query |
||
| 249 | * @param ConnectionInterface $con an optional connection object |
||
| 250 | * |
||
| 251 | * @return ObjectCollection|array|mixed the list of results, formatted by the current formatter |
||
| 252 | */ |
||
| 253 | View Code Duplication | public function findPks($keys, ConnectionInterface $con = null) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Filter the query by primary key |
||
| 269 | * |
||
| 270 | * @param mixed $key Primary key to use for the query |
||
| 271 | * |
||
| 272 | * @return $this|ChildRecordQuery The current query, for fluid interface |
||
| 273 | */ |
||
| 274 | public function filterByPrimaryKey($key) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Filter the query by a list of primary keys |
||
| 282 | * |
||
| 283 | * @param array $keys The list of primary key to use for the query |
||
| 284 | * |
||
| 285 | * @return $this|ChildRecordQuery The current query, for fluid interface |
||
| 286 | */ |
||
| 287 | public function filterByPrimaryKeys($keys) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Filter the query on the id column |
||
| 295 | * |
||
| 296 | * Example usage: |
||
| 297 | * <code> |
||
| 298 | * $query->filterById(1234); // WHERE id = 1234 |
||
| 299 | * $query->filterById(array(12, 34)); // WHERE id IN (12, 34) |
||
| 300 | * $query->filterById(array('min' => 12)); // WHERE id > 12 |
||
| 301 | * </code> |
||
| 302 | * |
||
| 303 | * @param mixed $id The value to use as filter. |
||
| 304 | * Use scalar values for equality. |
||
| 305 | * Use array values for in_array() equivalent. |
||
| 306 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
| 307 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 308 | * |
||
| 309 | * @return $this|ChildRecordQuery The current query, for fluid interface |
||
| 310 | */ |
||
| 311 | View Code Duplication | public function filterById($id = null, $comparison = null) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Filter the query on the mapUid column |
||
| 336 | * |
||
| 337 | * Example usage: |
||
| 338 | * <code> |
||
| 339 | * $query->filterByMapuid('fooValue'); // WHERE mapUid = 'fooValue' |
||
| 340 | * $query->filterByMapuid('%fooValue%', Criteria::LIKE); // WHERE mapUid LIKE '%fooValue%' |
||
| 341 | * </code> |
||
| 342 | * |
||
| 343 | * @param string $mapuid The value to use as filter. |
||
| 344 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 345 | * |
||
| 346 | * @return $this|ChildRecordQuery The current query, for fluid interface |
||
| 347 | */ |
||
| 348 | View Code Duplication | public function filterByMapuid($mapuid = null, $comparison = null) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Filter the query on the nbLaps column |
||
| 361 | * |
||
| 362 | * Example usage: |
||
| 363 | * <code> |
||
| 364 | * $query->filterByNblaps(1234); // WHERE nbLaps = 1234 |
||
| 365 | * $query->filterByNblaps(array(12, 34)); // WHERE nbLaps IN (12, 34) |
||
| 366 | * $query->filterByNblaps(array('min' => 12)); // WHERE nbLaps > 12 |
||
| 367 | * </code> |
||
| 368 | * |
||
| 369 | * @param mixed $nblaps The value to use as filter. |
||
| 370 | * Use scalar values for equality. |
||
| 371 | * Use array values for in_array() equivalent. |
||
| 372 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
| 373 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 374 | * |
||
| 375 | * @return $this|ChildRecordQuery The current query, for fluid interface |
||
| 376 | */ |
||
| 377 | View Code Duplication | public function filterByNblaps($nblaps = null, $comparison = null) |
|
| 399 | |||
| 400 | /** |
||
| 401 | * Filter the query on the score column |
||
| 402 | * |
||
| 403 | * Example usage: |
||
| 404 | * <code> |
||
| 405 | * $query->filterByScore(1234); // WHERE score = 1234 |
||
| 406 | * $query->filterByScore(array(12, 34)); // WHERE score IN (12, 34) |
||
| 407 | * $query->filterByScore(array('min' => 12)); // WHERE score > 12 |
||
| 408 | * </code> |
||
| 409 | * |
||
| 410 | * @param mixed $score The value to use as filter. |
||
| 411 | * Use scalar values for equality. |
||
| 412 | * Use array values for in_array() equivalent. |
||
| 413 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
| 414 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 415 | * |
||
| 416 | * @return $this|ChildRecordQuery The current query, for fluid interface |
||
| 417 | */ |
||
| 418 | View Code Duplication | public function filterByScore($score = null, $comparison = null) |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Filter the query on the nbFinish column |
||
| 443 | * |
||
| 444 | * Example usage: |
||
| 445 | * <code> |
||
| 446 | * $query->filterByNbfinish(1234); // WHERE nbFinish = 1234 |
||
| 447 | * $query->filterByNbfinish(array(12, 34)); // WHERE nbFinish IN (12, 34) |
||
| 448 | * $query->filterByNbfinish(array('min' => 12)); // WHERE nbFinish > 12 |
||
| 449 | * </code> |
||
| 450 | * |
||
| 451 | * @param mixed $nbfinish The value to use as filter. |
||
| 452 | * Use scalar values for equality. |
||
| 453 | * Use array values for in_array() equivalent. |
||
| 454 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
| 455 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 456 | * |
||
| 457 | * @return $this|ChildRecordQuery The current query, for fluid interface |
||
| 458 | */ |
||
| 459 | View Code Duplication | public function filterByNbfinish($nbfinish = null, $comparison = null) |
|
| 481 | |||
| 482 | /** |
||
| 483 | * Filter the query on the avgScore column |
||
| 484 | * |
||
| 485 | * Example usage: |
||
| 486 | * <code> |
||
| 487 | * $query->filterByAvgscore(1234); // WHERE avgScore = 1234 |
||
| 488 | * $query->filterByAvgscore(array(12, 34)); // WHERE avgScore IN (12, 34) |
||
| 489 | * $query->filterByAvgscore(array('min' => 12)); // WHERE avgScore > 12 |
||
| 490 | * </code> |
||
| 491 | * |
||
| 492 | * @param mixed $avgscore The value to use as filter. |
||
| 493 | * Use scalar values for equality. |
||
| 494 | * Use array values for in_array() equivalent. |
||
| 495 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
| 496 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 497 | * |
||
| 498 | * @return $this|ChildRecordQuery The current query, for fluid interface |
||
| 499 | */ |
||
| 500 | View Code Duplication | public function filterByAvgscore($avgscore = null, $comparison = null) |
|
| 522 | |||
| 523 | /** |
||
| 524 | * Filter the query on the checkpoints column |
||
| 525 | * |
||
| 526 | * Example usage: |
||
| 527 | * <code> |
||
| 528 | * $query->filterByCheckpoints('fooValue'); // WHERE checkpoints = 'fooValue' |
||
| 529 | * $query->filterByCheckpoints('%fooValue%', Criteria::LIKE); // WHERE checkpoints LIKE '%fooValue%' |
||
| 530 | * </code> |
||
| 531 | * |
||
| 532 | * @param string $checkpoints The value to use as filter. |
||
| 533 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 534 | * |
||
| 535 | * @return $this|ChildRecordQuery The current query, for fluid interface |
||
| 536 | */ |
||
| 537 | View Code Duplication | public function filterByCheckpoints($checkpoints = null, $comparison = null) |
|
| 547 | |||
| 548 | /** |
||
| 549 | * Filter the query on the player_id column |
||
| 550 | * |
||
| 551 | * Example usage: |
||
| 552 | * <code> |
||
| 553 | * $query->filterByPlayerId(1234); // WHERE player_id = 1234 |
||
| 554 | * $query->filterByPlayerId(array(12, 34)); // WHERE player_id IN (12, 34) |
||
| 555 | * $query->filterByPlayerId(array('min' => 12)); // WHERE player_id > 12 |
||
| 556 | * </code> |
||
| 557 | * |
||
| 558 | * @see filterByPlayer() |
||
| 559 | * |
||
| 560 | * @param mixed $playerId The value to use as filter. |
||
| 561 | * Use scalar values for equality. |
||
| 562 | * Use array values for in_array() equivalent. |
||
| 563 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
| 564 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 565 | * |
||
| 566 | * @return $this|ChildRecordQuery The current query, for fluid interface |
||
| 567 | */ |
||
| 568 | View Code Duplication | public function filterByPlayerId($playerId = null, $comparison = null) |
|
| 590 | |||
| 591 | /** |
||
| 592 | * Filter the query on the created_at column |
||
| 593 | * |
||
| 594 | * Example usage: |
||
| 595 | * <code> |
||
| 596 | * $query->filterByCreatedAt('2011-03-14'); // WHERE created_at = '2011-03-14' |
||
| 597 | * $query->filterByCreatedAt('now'); // WHERE created_at = '2011-03-14' |
||
| 598 | * $query->filterByCreatedAt(array('max' => 'yesterday')); // WHERE created_at > '2011-03-13' |
||
| 599 | * </code> |
||
| 600 | * |
||
| 601 | * @param mixed $createdAt The value to use as filter. |
||
| 602 | * Values can be integers (unix timestamps), DateTime objects, or strings. |
||
| 603 | * Empty strings are treated as NULL. |
||
| 604 | * Use scalar values for equality. |
||
| 605 | * Use array values for in_array() equivalent. |
||
| 606 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
| 607 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 608 | * |
||
| 609 | * @return $this|ChildRecordQuery The current query, for fluid interface |
||
| 610 | */ |
||
| 611 | View Code Duplication | public function filterByCreatedAt($createdAt = null, $comparison = null) |
|
| 633 | |||
| 634 | /** |
||
| 635 | * Filter the query on the updated_at column |
||
| 636 | * |
||
| 637 | * Example usage: |
||
| 638 | * <code> |
||
| 639 | * $query->filterByUpdatedAt('2011-03-14'); // WHERE updated_at = '2011-03-14' |
||
| 640 | * $query->filterByUpdatedAt('now'); // WHERE updated_at = '2011-03-14' |
||
| 641 | * $query->filterByUpdatedAt(array('max' => 'yesterday')); // WHERE updated_at > '2011-03-13' |
||
| 642 | * </code> |
||
| 643 | * |
||
| 644 | * @param mixed $updatedAt The value to use as filter. |
||
| 645 | * Values can be integers (unix timestamps), DateTime objects, or strings. |
||
| 646 | * Empty strings are treated as NULL. |
||
| 647 | * Use scalar values for equality. |
||
| 648 | * Use array values for in_array() equivalent. |
||
| 649 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
| 650 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 651 | * |
||
| 652 | * @return $this|ChildRecordQuery The current query, for fluid interface |
||
| 653 | */ |
||
| 654 | View Code Duplication | public function filterByUpdatedAt($updatedAt = null, $comparison = null) |
|
| 676 | |||
| 677 | /** |
||
| 678 | * Filter the query by a related \eXpansion\Framework\PlayersBundle\Model\Player object |
||
| 679 | * |
||
| 680 | * @param \eXpansion\Framework\PlayersBundle\Model\Player|ObjectCollection $player The related object(s) to use as filter |
||
| 681 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 682 | * |
||
| 683 | * @throws \Propel\Runtime\Exception\PropelException |
||
| 684 | * |
||
| 685 | * @return ChildRecordQuery The current query, for fluid interface |
||
| 686 | */ |
||
| 687 | public function filterByPlayer($player, $comparison = null) |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Adds a JOIN clause to the query using the Player relation |
||
| 706 | * |
||
| 707 | * @param string $relationAlias optional alias for the relation |
||
| 708 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
| 709 | * |
||
| 710 | * @return $this|ChildRecordQuery The current query, for fluid interface |
||
| 711 | */ |
||
| 712 | View Code Duplication | public function joinPlayer($relationAlias = null, $joinType = Criteria::LEFT_JOIN) |
|
| 735 | |||
| 736 | /** |
||
| 737 | * Use the Player relation Player object |
||
| 738 | * |
||
| 739 | * @see useQuery() |
||
| 740 | * |
||
| 741 | * @param string $relationAlias optional alias for the relation, |
||
| 742 | * to be used as main alias in the secondary query |
||
| 743 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
| 744 | * |
||
| 745 | * @return \eXpansion\Framework\PlayersBundle\Model\PlayerQuery A secondary query class using the current class as primary query |
||
| 746 | */ |
||
| 747 | public function usePlayerQuery($relationAlias = null, $joinType = Criteria::LEFT_JOIN) |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Exclude object from result |
||
| 756 | * |
||
| 757 | * @param ChildRecord $record Object to remove from the list of results |
||
| 758 | * |
||
| 759 | * @return $this|ChildRecordQuery The current query, for fluid interface |
||
| 760 | */ |
||
| 761 | public function prune($record = null) |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Deletes all rows from the record table. |
||
| 772 | * |
||
| 773 | * @param ConnectionInterface $con the connection to use |
||
| 774 | * @return int The number of affected rows (if supported by underlying database driver). |
||
| 775 | */ |
||
| 776 | View Code Duplication | public function doDeleteAll(ConnectionInterface $con = null) |
|
| 796 | |||
| 797 | /** |
||
| 798 | * Performs a DELETE on the database based on the current ModelCriteria |
||
| 799 | * |
||
| 800 | * @param ConnectionInterface $con the connection to use |
||
| 801 | * @return int The number of affected rows (if supported by underlying database driver). This includes CASCADE-related rows |
||
| 802 | * if supported by native driver or if emulated using Propel. |
||
| 803 | * @throws PropelException Any exceptions caught during processing will be |
||
| 804 | * rethrown wrapped into a PropelException. |
||
| 805 | */ |
||
| 806 | View Code Duplication | public function delete(ConnectionInterface $con = null) |
|
| 830 | |||
| 831 | // timestampable behavior |
||
| 832 | |||
| 833 | /** |
||
| 834 | * Filter by the latest updated |
||
| 835 | * |
||
| 836 | * @param int $nbDays Maximum age of the latest update in days |
||
| 837 | * |
||
| 838 | * @return $this|ChildRecordQuery The current query, for fluid interface |
||
| 839 | */ |
||
| 840 | public function recentlyUpdated($nbDays = 7) |
||
| 844 | |||
| 845 | /** |
||
| 846 | * Order by update date desc |
||
| 847 | * |
||
| 848 | * @return $this|ChildRecordQuery The current query, for fluid interface |
||
| 849 | */ |
||
| 850 | public function lastUpdatedFirst() |
||
| 854 | |||
| 855 | /** |
||
| 856 | * Order by update date asc |
||
| 857 | * |
||
| 858 | * @return $this|ChildRecordQuery The current query, for fluid interface |
||
| 859 | */ |
||
| 860 | public function firstUpdatedFirst() |
||
| 864 | |||
| 865 | /** |
||
| 866 | * Order by create date desc |
||
| 867 | * |
||
| 868 | * @return $this|ChildRecordQuery The current query, for fluid interface |
||
| 869 | */ |
||
| 870 | public function lastCreatedFirst() |
||
| 874 | |||
| 875 | /** |
||
| 876 | * Filter by the latest created |
||
| 877 | * |
||
| 878 | * @param int $nbDays Maximum age of in days |
||
| 879 | * |
||
| 880 | * @return $this|ChildRecordQuery The current query, for fluid interface |
||
| 881 | */ |
||
| 882 | public function recentlyCreated($nbDays = 7) |
||
| 886 | |||
| 887 | /** |
||
| 888 | * Order by create date asc |
||
| 889 | * |
||
| 890 | * @return $this|ChildRecordQuery The current query, for fluid interface |
||
| 891 | */ |
||
| 892 | public function firstCreatedFirst() |
||
| 896 | |||
| 897 | } // RecordQuery |
||
| 898 |
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.