Complex classes like QueryService 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 QueryService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class QueryService |
||
| 27 | { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var VisitorInterface |
||
| 31 | */ |
||
| 32 | private $visitor; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var Manager |
||
| 36 | */ |
||
| 37 | private $restrictionManager; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var integer |
||
| 41 | */ |
||
| 42 | private $paginationDefaultLimit; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var Request |
||
| 46 | */ |
||
| 47 | private $request; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var Builder |
||
| 51 | */ |
||
| 52 | private $queryBuilder; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var DocumentRepository |
||
| 56 | */ |
||
| 57 | private $repository; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param VisitorInterface $visitor visitor |
||
| 61 | * @param Manager $restrictionManager restriction manager |
||
| 62 | * @param integer $paginationDefaultLimit default pagination limit |
||
| 63 | */ |
||
| 64 | public function __construct( |
||
| 73 | |||
| 74 | /** |
||
| 75 | * public function that returns an array of records (or just one record if that's requested) |
||
| 76 | * based on the Request passed to it. |
||
| 77 | * sets all necessary stuff on the querybuilder and the request |
||
| 78 | * |
||
| 79 | * @param Request $request request |
||
| 80 | * @param DocumentRepository $repository repository |
||
| 81 | * |
||
| 82 | * @throws \Doctrine\ODM\MongoDB\MongoDBException |
||
| 83 | * |
||
| 84 | * @return array|null|object either array of records or the record |
||
| 85 | * |
||
| 86 | */ |
||
| 87 | public function getWithRequest(Request &$request, DocumentRepository $repository) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * if a single document has been requested, this returns the document id. if it returns null, |
||
| 151 | * then we return multiple documents |
||
| 152 | * |
||
| 153 | * @return string|null either document id or null |
||
| 154 | */ |
||
| 155 | private function getDocumentId() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * apply all stuff from the rql query (if any) to the local querybuilder |
||
| 162 | * |
||
| 163 | * @return void |
||
| 164 | */ |
||
| 165 | private function applyRqlQuery() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * returns the correct rql query for the request, including optional specified restrictions |
||
| 205 | * in the service definition (via restrictionManager) |
||
| 206 | * |
||
| 207 | * @return Query the query |
||
| 208 | */ |
||
| 209 | private function getRqlQuery() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Check if collection has search indexes in DB |
||
| 243 | * |
||
| 244 | * @return bool |
||
| 245 | */ |
||
| 246 | private function hasSearchIndex() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * get the pagination page size |
||
| 274 | * |
||
| 275 | * @return int page size |
||
| 276 | */ |
||
| 277 | private function getPaginationPageSize() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * gets the pagination skip |
||
| 296 | * |
||
| 297 | * @return int skip |
||
| 298 | */ |
||
| 299 | private function getPaginationSkip() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * gets the limit node |
||
| 312 | * |
||
| 313 | * @return bool|LimitNode the node or false |
||
| 314 | */ |
||
| 315 | private function getPaginationLimitNode() |
||
| 326 | } |
||
| 327 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: