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 PlayerQuery 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 PlayerQuery, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 98 | abstract class PlayerQuery extends ModelCriteria |
||
| 99 | { |
||
| 100 | protected $entityNotFoundExceptionClass = '\\Propel\\Runtime\\Exception\\EntityNotFoundException'; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Initializes internal state of \eXpansion\Framework\PlayersBundle\Model\Base\PlayerQuery object. |
||
| 104 | * |
||
| 105 | * @param string $dbName The database name |
||
| 106 | * @param string $modelName The phpName of a model, e.g. 'Book' |
||
| 107 | * @param string $modelAlias The alias for the model in this query, e.g. 'b' |
||
| 108 | */ |
||
| 109 | public function __construct($dbName = 'expansion', $modelName = '\\eXpansion\\Framework\\PlayersBundle\\Model\\Player', $modelAlias = null) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Returns a new ChildPlayerQuery object. |
||
| 116 | * |
||
| 117 | * @param string $modelAlias The alias of a model in the query |
||
| 118 | * @param Criteria $criteria Optional Criteria to build the query from |
||
| 119 | * |
||
| 120 | * @return ChildPlayerQuery |
||
| 121 | */ |
||
| 122 | View Code Duplication | public static function create($modelAlias = null, Criteria $criteria = null) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Find object by primary key. |
||
| 140 | * Propel uses the instance pool to skip the database if the object exists. |
||
| 141 | * Go fast if the query is untouched. |
||
| 142 | * |
||
| 143 | * <code> |
||
| 144 | * $obj = $c->findPk(12, $con); |
||
| 145 | * </code> |
||
| 146 | * |
||
| 147 | * @param mixed $key Primary key to use for the query |
||
| 148 | * @param ConnectionInterface $con an optional connection object |
||
| 149 | * |
||
| 150 | * @return ChildPlayer|array|mixed the result, formatted by the current formatter |
||
| 151 | */ |
||
| 152 | View Code Duplication | public function findPk($key, ConnectionInterface $con = null) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Find object by primary key using raw SQL to go fast. |
||
| 182 | * Bypass doSelect() and the object formatter by using generated code. |
||
| 183 | * |
||
| 184 | * @param mixed $key Primary key to use for the query |
||
| 185 | * @param ConnectionInterface $con A connection object |
||
| 186 | * |
||
| 187 | * @throws \Propel\Runtime\Exception\PropelException |
||
| 188 | * |
||
| 189 | * @return ChildPlayer A model object, or null if the key is not found |
||
| 190 | */ |
||
| 191 | View Code Duplication | protected function findPkSimple($key, ConnectionInterface $con) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Find object by primary key. |
||
| 216 | * |
||
| 217 | * @param mixed $key Primary key to use for the query |
||
| 218 | * @param ConnectionInterface $con A connection object |
||
| 219 | * |
||
| 220 | * @return ChildPlayer|array|mixed the result, formatted by the current formatter |
||
| 221 | */ |
||
| 222 | View Code Duplication | protected function findPkComplex($key, ConnectionInterface $con) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Find objects by primary key |
||
| 235 | * <code> |
||
| 236 | * $objs = $c->findPks(array(12, 56, 832), $con); |
||
| 237 | * </code> |
||
| 238 | * @param array $keys Primary keys to use for the query |
||
| 239 | * @param ConnectionInterface $con an optional connection object |
||
| 240 | * |
||
| 241 | * @return ObjectCollection|array|mixed the list of results, formatted by the current formatter |
||
| 242 | */ |
||
| 243 | View Code Duplication | public function findPks($keys, ConnectionInterface $con = null) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Filter the query by primary key |
||
| 259 | * |
||
| 260 | * @param mixed $key Primary key to use for the query |
||
| 261 | * |
||
| 262 | * @return $this|ChildPlayerQuery The current query, for fluid interface |
||
| 263 | */ |
||
| 264 | public function filterByPrimaryKey($key) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Filter the query by a list of primary keys |
||
| 272 | * |
||
| 273 | * @param array $keys The list of primary key to use for the query |
||
| 274 | * |
||
| 275 | * @return $this|ChildPlayerQuery The current query, for fluid interface |
||
| 276 | */ |
||
| 277 | public function filterByPrimaryKeys($keys) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Filter the query on the id column |
||
| 285 | * |
||
| 286 | * Example usage: |
||
| 287 | * <code> |
||
| 288 | * $query->filterById(1234); // WHERE id = 1234 |
||
| 289 | * $query->filterById(array(12, 34)); // WHERE id IN (12, 34) |
||
| 290 | * $query->filterById(array('min' => 12)); // WHERE id > 12 |
||
| 291 | * </code> |
||
| 292 | * |
||
| 293 | * @param mixed $id The value to use as filter. |
||
| 294 | * Use scalar values for equality. |
||
| 295 | * Use array values for in_array() equivalent. |
||
| 296 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
| 297 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 298 | * |
||
| 299 | * @return $this|ChildPlayerQuery The current query, for fluid interface |
||
| 300 | */ |
||
| 301 | View Code Duplication | public function filterById($id = null, $comparison = null) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Filter the query on the login column |
||
| 326 | * |
||
| 327 | * Example usage: |
||
| 328 | * <code> |
||
| 329 | * $query->filterByLogin('fooValue'); // WHERE login = 'fooValue' |
||
| 330 | * $query->filterByLogin('%fooValue%', Criteria::LIKE); // WHERE login LIKE '%fooValue%' |
||
| 331 | * </code> |
||
| 332 | * |
||
| 333 | * @param string $login The value to use as filter. |
||
| 334 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 335 | * |
||
| 336 | * @return $this|ChildPlayerQuery The current query, for fluid interface |
||
| 337 | */ |
||
| 338 | View Code Duplication | public function filterByLogin($login = null, $comparison = null) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Filter the query on the nickname column |
||
| 351 | * |
||
| 352 | * Example usage: |
||
| 353 | * <code> |
||
| 354 | * $query->filterByNickname('fooValue'); // WHERE nickname = 'fooValue' |
||
| 355 | * $query->filterByNickname('%fooValue%', Criteria::LIKE); // WHERE nickname LIKE '%fooValue%' |
||
| 356 | * </code> |
||
| 357 | * |
||
| 358 | * @param string $nickname The value to use as filter. |
||
| 359 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 360 | * |
||
| 361 | * @return $this|ChildPlayerQuery The current query, for fluid interface |
||
| 362 | */ |
||
| 363 | View Code Duplication | public function filterByNickname($nickname = null, $comparison = null) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Filter the query on the nickname_stripped column |
||
| 376 | * |
||
| 377 | * Example usage: |
||
| 378 | * <code> |
||
| 379 | * $query->filterByNicknameStripped('fooValue'); // WHERE nickname_stripped = 'fooValue' |
||
| 380 | * $query->filterByNicknameStripped('%fooValue%', Criteria::LIKE); // WHERE nickname_stripped LIKE '%fooValue%' |
||
| 381 | * </code> |
||
| 382 | * |
||
| 383 | * @param string $nicknameStripped The value to use as filter. |
||
| 384 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 385 | * |
||
| 386 | * @return $this|ChildPlayerQuery The current query, for fluid interface |
||
| 387 | */ |
||
| 388 | View Code Duplication | public function filterByNicknameStripped($nicknameStripped = null, $comparison = null) |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Filter the query on the path column |
||
| 401 | * |
||
| 402 | * Example usage: |
||
| 403 | * <code> |
||
| 404 | * $query->filterByPath('fooValue'); // WHERE path = 'fooValue' |
||
| 405 | * $query->filterByPath('%fooValue%', Criteria::LIKE); // WHERE path LIKE '%fooValue%' |
||
| 406 | * </code> |
||
| 407 | * |
||
| 408 | * @param string $path The value to use as filter. |
||
| 409 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 410 | * |
||
| 411 | * @return $this|ChildPlayerQuery The current query, for fluid interface |
||
| 412 | */ |
||
| 413 | View Code Duplication | public function filterByPath($path = null, $comparison = null) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Filter the query on the wins column |
||
| 426 | * |
||
| 427 | * Example usage: |
||
| 428 | * <code> |
||
| 429 | * $query->filterByWins(1234); // WHERE wins = 1234 |
||
| 430 | * $query->filterByWins(array(12, 34)); // WHERE wins IN (12, 34) |
||
| 431 | * $query->filterByWins(array('min' => 12)); // WHERE wins > 12 |
||
| 432 | * </code> |
||
| 433 | * |
||
| 434 | * @param mixed $wins The value to use as filter. |
||
| 435 | * Use scalar values for equality. |
||
| 436 | * Use array values for in_array() equivalent. |
||
| 437 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
| 438 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 439 | * |
||
| 440 | * @return $this|ChildPlayerQuery The current query, for fluid interface |
||
| 441 | */ |
||
| 442 | View Code Duplication | public function filterByWins($wins = null, $comparison = null) |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Filter the query on the online_time column |
||
| 467 | * |
||
| 468 | * Example usage: |
||
| 469 | * <code> |
||
| 470 | * $query->filterByOnlineTime(1234); // WHERE online_time = 1234 |
||
| 471 | * $query->filterByOnlineTime(array(12, 34)); // WHERE online_time IN (12, 34) |
||
| 472 | * $query->filterByOnlineTime(array('min' => 12)); // WHERE online_time > 12 |
||
| 473 | * </code> |
||
| 474 | * |
||
| 475 | * @param mixed $onlineTime The value to use as filter. |
||
| 476 | * Use scalar values for equality. |
||
| 477 | * Use array values for in_array() equivalent. |
||
| 478 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
| 479 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 480 | * |
||
| 481 | * @return $this|ChildPlayerQuery The current query, for fluid interface |
||
| 482 | */ |
||
| 483 | View Code Duplication | public function filterByOnlineTime($onlineTime = null, $comparison = null) |
|
| 505 | |||
| 506 | /** |
||
| 507 | * Filter the query on the last_online column |
||
| 508 | * |
||
| 509 | * Example usage: |
||
| 510 | * <code> |
||
| 511 | * $query->filterByLastOnline('2011-03-14'); // WHERE last_online = '2011-03-14' |
||
| 512 | * $query->filterByLastOnline('now'); // WHERE last_online = '2011-03-14' |
||
| 513 | * $query->filterByLastOnline(array('max' => 'yesterday')); // WHERE last_online > '2011-03-13' |
||
| 514 | * </code> |
||
| 515 | * |
||
| 516 | * @param mixed $lastOnline The value to use as filter. |
||
| 517 | * Values can be integers (unix timestamps), DateTime objects, or strings. |
||
| 518 | * Empty strings are treated as NULL. |
||
| 519 | * Use scalar values for equality. |
||
| 520 | * Use array values for in_array() equivalent. |
||
| 521 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
| 522 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 523 | * |
||
| 524 | * @return $this|ChildPlayerQuery The current query, for fluid interface |
||
| 525 | */ |
||
| 526 | View Code Duplication | public function filterByLastOnline($lastOnline = null, $comparison = null) |
|
| 548 | |||
| 549 | /** |
||
| 550 | * Filter the query by a related \eXpansion\Bundle\LocalRecords\Model\Record object |
||
| 551 | * |
||
| 552 | * @param \eXpansion\Bundle\LocalRecords\Model\Record|ObjectCollection $record the related object to use as filter |
||
| 553 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 554 | * |
||
| 555 | * @return ChildPlayerQuery The current query, for fluid interface |
||
| 556 | */ |
||
| 557 | public function filterByRecord($record, $comparison = null) |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Adds a JOIN clause to the query using the Record relation |
||
| 574 | * |
||
| 575 | * @param string $relationAlias optional alias for the relation |
||
| 576 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
| 577 | * |
||
| 578 | * @return $this|ChildPlayerQuery The current query, for fluid interface |
||
| 579 | */ |
||
| 580 | View Code Duplication | public function joinRecord($relationAlias = null, $joinType = Criteria::LEFT_JOIN) |
|
| 603 | |||
| 604 | /** |
||
| 605 | * Use the Record relation Record object |
||
| 606 | * |
||
| 607 | * @see useQuery() |
||
| 608 | * |
||
| 609 | * @param string $relationAlias optional alias for the relation, |
||
| 610 | * to be used as main alias in the secondary query |
||
| 611 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
| 612 | * |
||
| 613 | * @return \eXpansion\Bundle\LocalRecords\Model\RecordQuery A secondary query class using the current class as primary query |
||
| 614 | */ |
||
| 615 | public function useRecordQuery($relationAlias = null, $joinType = Criteria::LEFT_JOIN) |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Exclude object from result |
||
| 624 | * |
||
| 625 | * @param ChildPlayer $player Object to remove from the list of results |
||
| 626 | * |
||
| 627 | * @return $this|ChildPlayerQuery The current query, for fluid interface |
||
| 628 | */ |
||
| 629 | public function prune($player = null) |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Deletes all rows from the player table. |
||
| 640 | * |
||
| 641 | * @param ConnectionInterface $con the connection to use |
||
| 642 | * @return int The number of affected rows (if supported by underlying database driver). |
||
| 643 | */ |
||
| 644 | View Code Duplication | public function doDeleteAll(ConnectionInterface $con = null) |
|
| 664 | |||
| 665 | /** |
||
| 666 | * Performs a DELETE on the database based on the current ModelCriteria |
||
| 667 | * |
||
| 668 | * @param ConnectionInterface $con the connection to use |
||
| 669 | * @return int The number of affected rows (if supported by underlying database driver). This includes CASCADE-related rows |
||
| 670 | * if supported by native driver or if emulated using Propel. |
||
| 671 | * @throws PropelException Any exceptions caught during processing will be |
||
| 672 | * rethrown wrapped into a PropelException. |
||
| 673 | */ |
||
| 674 | View Code Duplication | public function delete(ConnectionInterface $con = null) |
|
| 698 | |||
| 699 | } // PlayerQuery |
||
| 700 |
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.