Complex classes like EntityRepository 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 EntityRepository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class EntityRepository extends Doctrine\ORM\EntityRepository implements Persistence\QueryExecutor, Persistence\Queryable //, Persistence\ObjectFactory |
||
34 | { |
||
35 | |||
36 | public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
||
51 | |||
52 | |||
53 | |||
54 | public function findOneBy(array $criteria, array $orderBy = null) |
||
72 | |||
73 | |||
74 | /** |
||
75 | * @param array $criteria |
||
76 | * @return bool |
||
77 | */ |
||
78 | public function exists(array $criteria = []) |
||
91 | |||
92 | |||
93 | /** |
||
94 | * @param array $criteria |
||
95 | * @return int |
||
96 | */ |
||
97 | public function countBy(array $criteria = []) |
||
104 | |||
105 | |||
106 | |||
107 | /** |
||
108 | * @param array $criteria |
||
109 | * @return bool |
||
110 | */ |
||
111 | private function criteriaRequiresDql(array $criteria) |
||
121 | |||
122 | |||
123 | |||
124 | /** |
||
125 | * Fetches all records like $key => $value pairs |
||
126 | * |
||
127 | * @param array $criteria parameter can be skipped |
||
128 | * @param string $value mandatory |
||
129 | * @param array $orderBy parameter can be skipped |
||
130 | * @param string $key optional |
||
131 | * |
||
132 | * @throws QueryException |
||
133 | * @return array |
||
134 | */ |
||
135 | public function findPairs($criteria, $value = NULL, $orderBy = [], $key = NULL) |
||
169 | |||
170 | |||
171 | |||
172 | /** |
||
173 | * Fetches all records and returns an associative array indexed by key |
||
174 | * |
||
175 | * @param array $criteria |
||
176 | * @param string $key |
||
177 | * |
||
178 | * @throws \Exception|QueryException |
||
179 | * @return array |
||
180 | */ |
||
181 | public function findAssoc($criteria, $key = NULL) |
||
200 | |||
201 | |||
202 | |||
203 | /** |
||
204 | * @param string $sql |
||
205 | * @param Doctrine\ORM\Query\ResultSetMapping $rsm |
||
206 | * @return Doctrine\ORM\NativeQuery |
||
207 | */ |
||
208 | public function createNativeQuery($sql, Doctrine\ORM\Query\ResultSetMapping $rsm) |
||
212 | |||
213 | |||
214 | |||
215 | /** |
||
216 | * @param string $alias |
||
217 | * @param string $indexBy The index for the from. |
||
218 | * @return \Kdyby\Doctrine\QueryBuilder |
||
219 | */ |
||
220 | public function createQueryBuilder($alias = NULL, $indexBy = NULL) |
||
230 | |||
231 | |||
232 | |||
233 | /** |
||
234 | * @param string $dql |
||
235 | * |
||
236 | * @return \Doctrine\ORM\Query |
||
237 | */ |
||
238 | public function createQuery($dql = NULL) |
||
244 | |||
245 | |||
246 | |||
247 | /** |
||
248 | * @param \Kdyby\Persistence\Query|\Kdyby\Doctrine\QueryObject $queryObject |
||
249 | * @param int $hydrationMode |
||
250 | * @throws QueryException |
||
251 | * @return array|\Kdyby\Doctrine\ResultSet |
||
252 | */ |
||
253 | public function fetch(Persistence\Query $queryObject, $hydrationMode = AbstractQuery::HYDRATE_OBJECT) |
||
262 | |||
263 | |||
264 | |||
265 | /** |
||
266 | * @param \Kdyby\Persistence\Query|\Kdyby\Doctrine\QueryObject $queryObject |
||
267 | * |
||
268 | * @throws InvalidStateException |
||
269 | * @throws QueryException |
||
270 | * @return object |
||
271 | */ |
||
272 | public function fetchOne(Persistence\Query $queryObject) |
||
287 | |||
288 | |||
289 | |||
290 | /** |
||
291 | * @param integer|array $id |
||
292 | * @return \Doctrine\ORM\Proxy\Proxy |
||
293 | */ |
||
294 | public function getReference($id) |
||
298 | |||
299 | |||
300 | |||
301 | /** |
||
302 | * @param \Exception $e |
||
303 | * @param \Kdyby\Persistence\Query $queryObject |
||
304 | * |
||
305 | * @throws \Exception |
||
306 | */ |
||
307 | private function handleQueryException(\Exception $e, Persistence\Query $queryObject) |
||
313 | |||
314 | |||
315 | |||
316 | /** |
||
317 | * @param \Exception $e |
||
318 | * @param \Doctrine\ORM\Query $query |
||
319 | * @param string $message |
||
320 | */ |
||
321 | private function handleException(\Exception $e, Doctrine\ORM\Query $query = NULL, $message = NULL) |
||
329 | |||
330 | |||
331 | |||
332 | /** |
||
333 | * @return Mapping\ClassMetadata |
||
334 | */ |
||
335 | public function getClassMetadata() |
||
339 | |||
340 | |||
341 | |||
342 | /** |
||
343 | * @return EntityManager |
||
344 | */ |
||
345 | public function getEntityManager() |
||
349 | |||
350 | |||
351 | |||
352 | /** |
||
353 | * @param string $relation |
||
354 | * @return EntityRepository |
||
355 | */ |
||
356 | public function related($relation) |
||
363 | |||
364 | |||
365 | |||
366 | /*************************** Nette\Object ***************************/ |
||
367 | |||
368 | |||
369 | |||
370 | /** |
||
371 | * Access to reflection. |
||
372 | * |
||
373 | * @return \Nette\Reflection\ClassType |
||
374 | */ |
||
375 | public static function getReflection() |
||
379 | |||
380 | |||
381 | |||
382 | /** |
||
383 | * Call to undefined method. |
||
384 | * |
||
385 | * @param string $name |
||
386 | * @param array $args |
||
387 | * |
||
388 | * @throws \Nette\MemberAccessException |
||
389 | * @return mixed |
||
390 | */ |
||
391 | public function __call($name, $args) |
||
399 | |||
400 | |||
401 | |||
402 | /** |
||
403 | * Call to undefined static method. |
||
404 | * |
||
405 | * @param string $name |
||
406 | * @param array $args |
||
407 | * |
||
408 | * @throws \Nette\MemberAccessException |
||
409 | * @return mixed |
||
410 | */ |
||
411 | public static function __callStatic($name, $args) |
||
415 | |||
416 | |||
417 | |||
418 | /** |
||
419 | * Adding method to class. |
||
420 | * |
||
421 | * @param $name |
||
422 | * @param null $callback |
||
423 | * |
||
424 | * @throws \Nette\MemberAccessException |
||
425 | * @return callable|null |
||
426 | */ |
||
427 | public static function extensionMethod($name, $callback = NULL) |
||
440 | |||
441 | |||
442 | |||
443 | /** |
||
444 | * Returns property value. Do not call directly. |
||
445 | * |
||
446 | * @param string $name |
||
447 | * |
||
448 | * @throws \Nette\MemberAccessException |
||
449 | * @return mixed |
||
450 | */ |
||
451 | public function &__get($name) |
||
455 | |||
456 | |||
457 | |||
458 | /** |
||
459 | * Sets value of a property. Do not call directly. |
||
460 | * |
||
461 | * @param string $name |
||
462 | * @param mixed $value |
||
463 | * |
||
464 | * @throws \Nette\MemberAccessException |
||
465 | * @return void |
||
466 | */ |
||
467 | public function __set($name, $value) |
||
471 | |||
472 | |||
473 | |||
474 | /** |
||
475 | * Is property defined? |
||
476 | * |
||
477 | * @param string $name |
||
478 | * |
||
479 | * @return bool |
||
480 | */ |
||
481 | public function __isset($name) |
||
485 | |||
486 | |||
487 | |||
488 | /** |
||
489 | * Access to undeclared property. |
||
490 | * |
||
491 | * @param string $name |
||
492 | * |
||
493 | * @throws \Nette\MemberAccessException |
||
494 | * @return void |
||
495 | */ |
||
496 | public function __unset($name) |
||
500 | |||
501 | } |
||
502 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.