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 Query 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 Query, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Query implements QueryInterface |
||
| 25 | { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * An inner join. |
||
| 29 | */ |
||
| 30 | const JCR_JOIN_TYPE_INNER = '{http://www.jcp.org/jcr/1.0}joinTypeInner'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * A left-outer join. |
||
| 34 | */ |
||
| 35 | const JCR_JOIN_TYPE_LEFT_OUTER = '{http://www.jcp.org/jcr/1.0}joinTypeLeftOuter'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * A right-outer join. |
||
| 39 | */ |
||
| 40 | const JCR_JOIN_TYPE_RIGHT_OUTER = '{http://www.jcp.org/jcr/1.0}joinTypeRightOuter'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Charset of strings in QOM |
||
| 44 | */ |
||
| 45 | const CHARSET = 'utf-8'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $sourceFieldName; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $type; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface |
||
| 59 | */ |
||
| 60 | protected $persistenceManager; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var \TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory |
||
| 64 | */ |
||
| 65 | protected $qomFactory; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var \TYPO3\CMS\Extbase\Persistence\Generic\Qom\SourceInterface |
||
| 69 | */ |
||
| 70 | protected $source; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var ConstraintInterface |
||
| 74 | */ |
||
| 75 | protected $constraint; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var \TYPO3\CMS\Extbase\Persistence\Generic\Qom\Statement |
||
| 79 | */ |
||
| 80 | protected $statement; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | protected $orderings = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var int |
||
| 89 | */ |
||
| 90 | protected $limit; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var int |
||
| 94 | */ |
||
| 95 | protected $offset; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Apply DISTINCT upon property. |
||
| 99 | * |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | protected $distinct; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * The query settings. |
||
| 106 | * |
||
| 107 | * @var \Fab\Vidi\Persistence\QuerySettings |
||
| 108 | * @inject |
||
| 109 | */ |
||
| 110 | protected $querySettings; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Constructs a query object working on the given class name |
||
| 114 | * |
||
| 115 | * @param string $type |
||
| 116 | */ |
||
| 117 | public function __construct($type) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Injects the persistence manager, used to fetch the CR session |
||
| 124 | * |
||
| 125 | * @param \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager |
||
| 126 | * @return void |
||
| 127 | */ |
||
| 128 | public function injectPersistenceManager(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Injects the Query Object Model Factory |
||
| 135 | * |
||
| 136 | * @param \TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory $qomFactory |
||
| 137 | * @return void |
||
| 138 | */ |
||
| 139 | public function injectQomFactory(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory $qomFactory) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Sets the Query Settings. These Query settings must match the settings expected by |
||
| 146 | * the specific Storage Backend. |
||
| 147 | * |
||
| 148 | * @param \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $querySettings The Query Settings |
||
| 149 | * @return void |
||
| 150 | * @api This method is not part of FLOW3 API |
||
| 151 | */ |
||
| 152 | public function setQuerySettings(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $querySettings) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Returns the Query Settings. |
||
| 159 | * |
||
| 160 | * @throws \Exception |
||
| 161 | * @return \Fab\Vidi\Persistence\QuerySettings $querySettings The Query Settings |
||
| 162 | * @api This method is not part of FLOW3 API |
||
| 163 | */ |
||
| 164 | public function getQuerySettings() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Returns the type this query cares for. |
||
| 189 | * |
||
| 190 | * @return string |
||
| 191 | * @api |
||
| 192 | */ |
||
| 193 | public function getType() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Sets the source to fetch the result from |
||
| 200 | * |
||
| 201 | * @param \TYPO3\CMS\Extbase\Persistence\Generic\Qom\SourceInterface $source |
||
| 202 | */ |
||
| 203 | public function setSource(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\SourceInterface $source) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Returns the selectorn name or an empty string, if the source is not a selector |
||
| 210 | * TODO This has to be checked at another place |
||
| 211 | * |
||
| 212 | * @return string The selector name |
||
| 213 | */ |
||
| 214 | protected function getSelectorName() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Gets the node-tuple source for this query. |
||
| 225 | * |
||
| 226 | * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\SourceInterface the node-tuple source; non-null |
||
| 227 | */ |
||
| 228 | public function getSource() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Executes the query against the database and returns the result |
||
| 238 | * |
||
| 239 | * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface|array The query result object or an array if $this->getQuerySettings()->getReturnRawQueryResult() is true |
||
| 240 | * @api |
||
| 241 | */ |
||
| 242 | public function execute($returnRawQueryResult = false) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Sets the property names to order the result by. Expected like this: |
||
| 251 | * array( |
||
| 252 | * 'foo' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING, |
||
| 253 | * 'bar' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING |
||
| 254 | * ) |
||
| 255 | * where 'foo' and 'bar' are property names. |
||
| 256 | * |
||
| 257 | * @param array $orderings The property names to order by |
||
| 258 | * @return QueryInterface |
||
| 259 | * @api |
||
| 260 | */ |
||
| 261 | public function setOrderings(array $orderings) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Returns the property names to order the result by. Like this: |
||
| 269 | * array( |
||
| 270 | * 'foo' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING, |
||
| 271 | * 'bar' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING |
||
| 272 | * ) |
||
| 273 | * |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | public function getOrderings() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Sets the maximum size of the result set to limit. Returns $this to allow |
||
| 283 | * for chaining (fluid interface) |
||
| 284 | * |
||
| 285 | * @param integer $limit |
||
| 286 | * @throws \InvalidArgumentException |
||
| 287 | * @return QueryInterface |
||
| 288 | * @api |
||
| 289 | */ |
||
| 290 | public function setLimit($limit) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Resets a previously set maximum size of the result set. Returns $this to allow |
||
| 301 | * for chaining (fluid interface) |
||
| 302 | * |
||
| 303 | * @return QueryInterface |
||
| 304 | * @api |
||
| 305 | */ |
||
| 306 | public function unsetLimit() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Returns the maximum size of the result set to limit. |
||
| 314 | * |
||
| 315 | * @return integer |
||
| 316 | * @api |
||
| 317 | */ |
||
| 318 | public function getLimit() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Sets the start offset of the result set to offset. Returns $this to |
||
| 325 | * allow for chaining (fluid interface) |
||
| 326 | * |
||
| 327 | * @param integer $offset |
||
| 328 | * @throws \InvalidArgumentException |
||
| 329 | * @return QueryInterface |
||
| 330 | * @api |
||
| 331 | */ |
||
| 332 | public function setOffset($offset) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Returns the start offset of the result set. |
||
| 343 | * |
||
| 344 | * @return integer |
||
| 345 | * @api |
||
| 346 | */ |
||
| 347 | public function getOffset() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * The constraint used to limit the result set. Returns $this to allow |
||
| 354 | * for chaining (fluid interface) |
||
| 355 | * |
||
| 356 | * @param ConstraintInterface $constraint |
||
| 357 | * @return QueryInterface |
||
| 358 | * @api |
||
| 359 | */ |
||
| 360 | public function matching($constraint) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Gets the constraint for this query. |
||
| 368 | * |
||
| 369 | * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface the constraint, or null if none |
||
| 370 | * @api |
||
| 371 | */ |
||
| 372 | public function getConstraint() |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Performs a logical conjunction of the given constraints. The method takes one or more contraints and concatenates them with a boolean AND. |
||
| 379 | * It also scepts a single array of constraints to be concatenated. |
||
| 380 | * |
||
| 381 | * @param mixed $constraint1 The first of multiple constraints or an array of constraints. |
||
| 382 | * @throws InvalidNumberOfConstraintsException |
||
| 383 | * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\AndInterface |
||
| 384 | * @api |
||
| 385 | */ |
||
| 386 | View Code Duplication | public function logicalAnd($constraint1) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Performs a logical disjunction of the two given constraints |
||
| 406 | * |
||
| 407 | * @param mixed $constraint1 The first of multiple constraints or an array of constraints. |
||
| 408 | * @throws InvalidNumberOfConstraintsException |
||
| 409 | * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\OrInterface |
||
| 410 | * @api |
||
| 411 | */ |
||
| 412 | View Code Duplication | public function logicalOr($constraint1) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Performs a logical negation of the given constraint |
||
| 432 | * |
||
| 433 | * @param ConstraintInterface $constraint Constraint to negate |
||
| 434 | * @throws \RuntimeException |
||
| 435 | * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\NotInterface |
||
| 436 | * @api |
||
| 437 | */ |
||
| 438 | public function logicalNot(ConstraintInterface $constraint) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Returns an equals criterion used for matching objects against a query |
||
| 445 | * |
||
| 446 | * @param string $propertyName The name of the property to compare against |
||
| 447 | * @param mixed $operand The value to compare with |
||
| 448 | * @param boolean $caseSensitive Whether the equality test should be done case-sensitive |
||
| 449 | * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\ComparisonInterface |
||
| 450 | * @api |
||
| 451 | */ |
||
| 452 | public function equals($propertyName, $operand, $caseSensitive = true) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Returns a like criterion used for matching objects against a query |
||
| 464 | * |
||
| 465 | * @param string $propertyName The name of the property to compare against |
||
| 466 | * @param mixed $operand The value to compare with |
||
| 467 | * @param boolean $caseSensitive Whether the matching should be done case-sensitive |
||
| 468 | * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\ComparisonInterface |
||
| 469 | * @api |
||
| 470 | */ |
||
| 471 | public function like($propertyName, $operand, $caseSensitive = true) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Returns a "contains" criterion used for matching objects against a query. |
||
| 478 | * It matches if the multivalued property contains the given operand. |
||
| 479 | * |
||
| 480 | * @param string $propertyName The name of the (multivalued) property to compare against |
||
| 481 | * @param mixed $operand The value to compare with |
||
| 482 | * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\ComparisonInterface |
||
| 483 | * @api |
||
| 484 | */ |
||
| 485 | public function contains($propertyName, $operand) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Returns an "in" criterion used for matching objects against a query. It |
||
| 492 | * matches if the property's value is contained in the multivalued operand. |
||
| 493 | * |
||
| 494 | * @param string $propertyName The name of the property to compare against |
||
| 495 | * @param mixed $operand The value to compare with, multivalued |
||
| 496 | * @throws UnexpectedTypeException |
||
| 497 | * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\ComparisonInterface |
||
| 498 | * @api |
||
| 499 | */ |
||
| 500 | public function in($propertyName, $operand) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Returns a less than criterion used for matching objects against a query |
||
| 510 | * |
||
| 511 | * @param string $propertyName The name of the property to compare against |
||
| 512 | * @param mixed $operand The value to compare with |
||
| 513 | * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\ComparisonInterface |
||
| 514 | * @api |
||
| 515 | */ |
||
| 516 | public function lessThan($propertyName, $operand) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Returns a less or equal than criterion used for matching objects against a query |
||
| 523 | * |
||
| 524 | * @param string $propertyName The name of the property to compare against |
||
| 525 | * @param mixed $operand The value to compare with |
||
| 526 | * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\ComparisonInterface |
||
| 527 | * @api |
||
| 528 | */ |
||
| 529 | public function lessThanOrEqual($propertyName, $operand) |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Returns a greater than criterion used for matching objects against a query |
||
| 536 | * |
||
| 537 | * @param string $propertyName The name of the property to compare against |
||
| 538 | * @param mixed $operand The value to compare with |
||
| 539 | * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\ComparisonInterface |
||
| 540 | * @api |
||
| 541 | */ |
||
| 542 | public function greaterThan($propertyName, $operand) |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Returns a greater than or equal criterion used for matching objects against a query |
||
| 549 | * |
||
| 550 | * @param string $propertyName The name of the property to compare against |
||
| 551 | * @param mixed $operand The value to compare with |
||
| 552 | * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\ComparisonInterface |
||
| 553 | * @api |
||
| 554 | */ |
||
| 555 | public function greaterThanOrEqual($propertyName, $operand) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Returns the query result count. |
||
| 562 | * |
||
| 563 | * @return integer The query result count |
||
| 564 | * @api |
||
| 565 | */ |
||
| 566 | public function count() |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Returns an "isEmpty" criterion used for matching objects against a query. |
||
| 575 | * It matches if the multivalued property contains no values or is null. |
||
| 576 | * |
||
| 577 | * @param string $propertyName The name of the multivalued property to compare against |
||
| 578 | * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\NotImplementedException |
||
| 579 | * @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException if used on a single-valued property |
||
| 580 | * @api |
||
| 581 | */ |
||
| 582 | public function isEmpty($propertyName) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * @return string |
||
| 589 | */ |
||
| 590 | public function getDistinct() |
||
| 594 | |||
| 595 | /** |
||
| 596 | * @param string $distinct |
||
| 597 | * @return $this |
||
| 598 | */ |
||
| 599 | public function setDistinct($distinct) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Sets the statement of this query. If you use this, you will lose the abstraction from a concrete storage |
||
| 607 | * backend (database). |
||
| 608 | * |
||
| 609 | * @param string|\TYPO3\CMS\Core\Database\PreparedStatement $statement The statement |
||
| 610 | * @param array $parameters An array of parameters. These will be bound to placeholders '?' in the $statement. |
||
| 611 | * @return QueryInterface |
||
| 612 | */ |
||
| 613 | public function statement($statement, array $parameters = array()) |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Returns the statement of this query. |
||
| 621 | * |
||
| 622 | * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\Statement |
||
| 623 | */ |
||
| 624 | public function getStatement() |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Returns whether the current mode is Backend. |
||
| 631 | * |
||
| 632 | * @return bool |
||
| 633 | */ |
||
| 634 | protected function isBackendMode() |
||
| 638 | |||
| 639 | /** |
||
| 640 | * @return string |
||
| 641 | */ |
||
| 642 | public function getSourceFieldName() |
||
| 646 | |||
| 647 | /** |
||
| 648 | * @param string $sourceFieldName |
||
| 649 | * @return $this |
||
| 650 | */ |
||
| 651 | public function setSourceFieldName($sourceFieldName) |
||
| 656 | |||
| 657 | /** |
||
| 658 | * @return object|\TYPO3\CMS\Extbase\Object\ObjectManager |
||
| 659 | */ |
||
| 660 | protected function getObjectManager() |
||
| 664 | |||
| 665 | } |
||
| 666 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.