Complex classes like EntryRepository 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 EntryRepository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class EntryRepository extends EntityRepository |
||
13 | { |
||
14 | /** |
||
15 | * Retrieves all entries for a user. |
||
16 | * |
||
17 | * @param int $userId |
||
18 | * |
||
19 | * @return QueryBuilder |
||
20 | */ |
||
21 | public function getBuilderForAllByUser($userId) |
||
27 | |||
28 | /** |
||
29 | * Retrieves unread entries for a user. |
||
30 | * |
||
31 | * @param int $userId |
||
32 | * |
||
33 | * @return QueryBuilder |
||
34 | */ |
||
35 | public function getBuilderForUnreadByUser($userId) |
||
42 | |||
43 | /** |
||
44 | * Retrieves read entries for a user. |
||
45 | * |
||
46 | * @param int $userId |
||
47 | * |
||
48 | * @return QueryBuilder |
||
49 | */ |
||
50 | public function getBuilderForArchiveByUser($userId) |
||
57 | |||
58 | /** |
||
59 | * Retrieves starred entries for a user. |
||
60 | * |
||
61 | * @param int $userId |
||
62 | * |
||
63 | * @return QueryBuilder |
||
64 | */ |
||
65 | public function getBuilderForStarredByUser($userId) |
||
72 | |||
73 | /** |
||
74 | * Retrieves entries filtered with a search term for a user. |
||
75 | * |
||
76 | * @param int $userId |
||
77 | * @param string $term |
||
78 | * @param string $currentRoute |
||
79 | * |
||
80 | * @return QueryBuilder |
||
81 | */ |
||
82 | public function getBuilderForSearchByUser($userId, $term, $currentRoute) |
||
103 | |||
104 | /** |
||
105 | * Retrieve a sorted list of untagged entries for a user. |
||
106 | * |
||
107 | * @param int $userId |
||
108 | * |
||
109 | * @return QueryBuilder |
||
110 | */ |
||
111 | public function getBuilderForUntaggedByUser($userId) |
||
116 | |||
117 | /** |
||
118 | * Retrieve untagged entries for a user. |
||
119 | * |
||
120 | * @param int $userId |
||
121 | * |
||
122 | * @return QueryBuilder |
||
123 | */ |
||
124 | public function getRawBuilderForUntaggedByUser($userId) |
||
130 | |||
131 | /** |
||
132 | * Find Entries. |
||
133 | * |
||
134 | * @param int $userId |
||
135 | * @param bool $isArchived |
||
136 | * @param bool $isStarred |
||
137 | * @param bool $isPublic |
||
138 | * @param string $sort |
||
139 | * @param string $order |
||
140 | * @param int $since |
||
141 | * @param string $tags |
||
142 | * |
||
143 | * @return Pagerfanta |
||
144 | */ |
||
145 | public function findEntries($userId, $isArchived = null, $isStarred = null, $isPublic = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '') |
||
198 | |||
199 | /** |
||
200 | * Fetch an entry with a tag. Only used for tests. |
||
201 | * |
||
202 | * @param int $userId |
||
203 | * |
||
204 | * @return array |
||
205 | */ |
||
206 | public function findOneWithTags($userId) |
||
217 | |||
218 | /** |
||
219 | * Find distinct language for a given user. |
||
220 | * Used to build the filter language list. |
||
221 | * |
||
222 | * @param int $userId User id |
||
223 | * |
||
224 | * @return array |
||
225 | */ |
||
226 | public function findDistinctLanguageByUser($userId) |
||
244 | |||
245 | /** |
||
246 | * Used only in test case to get the right entry associated to the right user. |
||
247 | * |
||
248 | * @param string $username |
||
249 | * |
||
250 | * @return Entry |
||
251 | */ |
||
252 | public function findOneByUsernameAndNotArchived($username) |
||
262 | |||
263 | /** |
||
264 | * Remove a tag from all user entries. |
||
265 | * |
||
266 | * We need to loop on each entry attached to the given tag to remove it, since Doctrine doesn't know EntryTag entity because it's a ManyToMany relation. |
||
267 | * It could be faster with one query but I don't know how to retrieve the table name `entry_tag` which can have a prefix: |
||
268 | * |
||
269 | * DELETE et FROM entry_tag et WHERE et.entry_id IN ( SELECT e.id FROM entry e WHERE e.user_id = :userId ) AND et.tag_id = :tagId |
||
270 | * |
||
271 | * @param int $userId |
||
272 | * @param Tag $tag |
||
273 | */ |
||
274 | public function removeTag($userId, Tag $tag) |
||
288 | |||
289 | /** |
||
290 | * Remove tags from all user entries. |
||
291 | * |
||
292 | * @param int $userId |
||
293 | * @param Array<Tag> $tags |
||
294 | */ |
||
295 | public function removeTags($userId, $tags) |
||
301 | |||
302 | /** |
||
303 | * Find all entries that are attached to a give tag id. |
||
304 | * |
||
305 | * @param int $userId |
||
306 | * @param int $tagId |
||
307 | * |
||
308 | * @return array |
||
309 | */ |
||
310 | public function findAllByTagId($userId, $tagId) |
||
318 | |||
319 | /** |
||
320 | * Find an entry by its url and its owner. |
||
321 | * If it exists, return the entry otherwise return false. |
||
322 | * |
||
323 | * @param $url |
||
324 | * @param $userId |
||
325 | * |
||
326 | * @return Entry|bool |
||
327 | */ |
||
328 | public function findByUrlAndUserId($url, $userId) |
||
342 | |||
343 | /** |
||
344 | * Count all entries for a user. |
||
345 | * |
||
346 | * @param int $userId |
||
347 | * |
||
348 | * @return int |
||
349 | */ |
||
350 | public function countAllEntriesByUser($userId) |
||
359 | |||
360 | /** |
||
361 | * Remove all entries for a user id. |
||
362 | * Used when a user want to reset all informations. |
||
363 | * |
||
364 | * @param int $userId |
||
365 | */ |
||
366 | public function removeAllByUserId($userId) |
||
373 | |||
374 | public function removeArchivedByUserId($userId) |
||
381 | |||
382 | /** |
||
383 | * Get id and url from all entries |
||
384 | * Used for the clean-duplicates command. |
||
385 | */ |
||
386 | public function findAllEntriesIdAndUrlByUserId($userId) |
||
394 | |||
395 | /** |
||
396 | * @param int $userId |
||
397 | * |
||
398 | * @return array |
||
399 | */ |
||
400 | public function findAllEntriesIdByUserId($userId = null) |
||
411 | |||
412 | /** |
||
413 | * Find all entries by url and owner. |
||
414 | * |
||
415 | * @param $url |
||
416 | * @param $userId |
||
417 | * |
||
418 | * @return array |
||
419 | */ |
||
420 | public function findAllByUrlAndUserId($url, $userId) |
||
428 | |||
429 | /** |
||
430 | * Return a query builder to be used by other getBuilderFor* method. |
||
431 | * |
||
432 | * @param int $userId |
||
433 | * |
||
434 | * @return QueryBuilder |
||
435 | */ |
||
436 | private function getQueryBuilderByUser($userId) |
||
441 | |||
442 | /** |
||
443 | * Return a sorted query builder to be used by other getBuilderFor* method. |
||
444 | * |
||
445 | * @param int $userId |
||
446 | * @param string $sortBy |
||
447 | * @param string $direction |
||
448 | * |
||
449 | * @return QueryBuilder |
||
450 | */ |
||
451 | private function getSortedQueryBuilderByUser($userId, $sortBy = 'createdAt', $direction = 'desc') |
||
455 | |||
456 | /** |
||
457 | * Return the given QueryBuilder with an orderBy() call. |
||
458 | * |
||
459 | * @param QueryBuilder $qb |
||
460 | * @param string $sortBy |
||
461 | * @param string $direction |
||
462 | * |
||
463 | * @return QueryBuilder |
||
464 | */ |
||
465 | private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc') |
||
470 | } |
||
471 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.