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 QueryBasedMatcher 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 QueryBasedMatcher, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | abstract class QueryBasedMatcher extends RepositoryMatcher |
||
| 14 | { |
||
| 15 | const MATCH_CONTENT_ID = 'content_id'; |
||
| 16 | const MATCH_LOCATION_ID = 'location_id'; |
||
| 17 | const MATCH_CONTENT_REMOTE_ID = 'content_remote_id'; |
||
| 18 | const MATCH_LOCATION_REMOTE_ID = 'location_remote_id'; |
||
| 19 | const MATCH_ATTRIBUTE = 'attribute'; |
||
| 20 | const MATCH_CONTENT_TYPE_ID = 'contenttype_id'; |
||
| 21 | const MATCH_CONTENT_TYPE_IDENTIFIER = 'contenttype_identifier'; |
||
| 22 | const MATCH_CREATION_DATE = 'creation_date'; |
||
| 23 | const MATCH_GROUP = 'group'; |
||
| 24 | const MATCH_MODIFICATION_DATE = 'modification_date'; |
||
| 25 | const MATCH_OBJECT_STATE = 'object_state'; |
||
| 26 | const MATCH_OWNER = 'owner'; |
||
| 27 | const MATCH_PARENT_LOCATION_ID = 'parent_location_id'; |
||
| 28 | const MATCH_PARENT_LOCATION_REMOTE_ID = 'parent_location_remote_id'; |
||
| 29 | const MATCH_SECTION = 'section'; |
||
| 30 | const MATCH_SUBTREE = 'subtree'; |
||
| 31 | const MATCH_VISIBILITY = 'visibility'; |
||
| 32 | |||
| 33 | // useful f.e. when talking to Solr, which defaults to java integers for max nr of items for queries |
||
| 34 | const INT_MAX_16BIT = 2147483647; |
||
| 35 | |||
| 36 | static protected $operatorsMap = array( |
||
| 37 | 'eq' => Operator::EQ, |
||
| 38 | 'gt' => Operator::GT, |
||
| 39 | 'gte' => Operator::GTE, |
||
| 40 | 'lt' => Operator::LT, |
||
| 41 | 'lte' => Operator::LTE, |
||
| 42 | 'in' => Operator::IN, |
||
| 43 | 'between' => Operator::BETWEEN, |
||
| 44 | 'like' => Operator::LIKE, |
||
| 45 | 'contains' => Operator::CONTAINS, |
||
| 46 | Operator::EQ => Operator::EQ, |
||
| 47 | Operator::GT => Operator::GT, |
||
| 48 | Operator::GTE => Operator::GTE, |
||
| 49 | Operator::LT => Operator::LT, |
||
| 50 | Operator::LTE => Operator::LTE, |
||
| 51 | ); |
||
| 52 | |||
| 53 | /** @var KeyMatcherInterface $groupMatcher */ |
||
| 54 | protected $groupMatcher; |
||
| 55 | /** @var KeyMatcherInterface $sectionMatcher */ |
||
| 56 | protected $sectionMatcher; |
||
| 57 | /** @var KeyMatcherInterface $stateMatcher */ |
||
| 58 | protected $stateMatcher; |
||
| 59 | /** @var KeyMatcherInterface $userMatcher */ |
||
| 60 | protected $userMatcher; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param Repository $repository |
||
| 64 | * @param KeyMatcherInterface $groupMatcher |
||
| 65 | * @param KeyMatcherInterface $sectionMatcher |
||
| 66 | * @param KeyMatcherInterface $stateMatcher |
||
| 67 | * @param KeyMatcherInterface $userMatcher |
||
| 68 | * @todo inject the services needed, not the whole repository |
||
| 69 | */ |
||
| 70 | public function __construct(Repository $repository, KeyMatcherInterface $groupMatcher = null, |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param $key |
||
| 83 | * @param $values |
||
| 84 | * @return mixed should it be \eZ\Publish\API\Repository\Values\Content\Query\CriterionInterface ? |
||
| 85 | * @throws \Exception for unsupported keys |
||
| 86 | */ |
||
| 87 | protected function getQueryCriterion($key, $values) |
||
| 224 | } |
||
| 225 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.