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 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 |
||
9 | class EntityRepository |
||
10 | { |
||
11 | /** |
||
12 | * REST Client. |
||
13 | * |
||
14 | * @var RestClient |
||
15 | */ |
||
16 | protected $restClient; |
||
17 | |||
18 | /** |
||
19 | * SDK Client. |
||
20 | * |
||
21 | * @var SdkClient |
||
22 | */ |
||
23 | protected $sdk; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $entityName; |
||
29 | |||
30 | /** |
||
31 | * classMetadataCache |
||
32 | * |
||
33 | * @var ClassMetadata |
||
34 | * @access private |
||
35 | */ |
||
36 | private $classMetadataCache; |
||
37 | |||
38 | /** |
||
39 | * unitOfWork |
||
40 | * |
||
41 | * @var UnitOfWork |
||
42 | * @access private |
||
43 | */ |
||
44 | private $unitOfWork; |
||
45 | |||
46 | /** |
||
47 | * EntityRepository constructor |
||
48 | * |
||
49 | * @param SdkClient $sdkClient The client to connect to the datasource with |
||
50 | * @param RestClient $restClient The client to process the http requests |
||
51 | * @param string $entityName The entity to work with |
||
52 | */ |
||
53 | public function __construct(SdkClient $sdkClient, RestClient $restClient, UnitOfWork $unitOfWork, $entityName) |
||
60 | |||
61 | /** |
||
62 | * find - finds one item of the entity based on the @REST\Id field in the entity |
||
63 | * |
||
64 | * @param string $id id of the element to fetch |
||
65 | * @param array $queryParams query parameters to add to the query |
||
66 | * @access public |
||
67 | * @return object |
||
68 | */ |
||
69 | public function find($id, $queryParams = []) |
||
91 | |||
92 | /** |
||
93 | * findAll |
||
94 | * |
||
95 | * @access public |
||
96 | * @return array |
||
97 | */ |
||
98 | public function findAll() |
||
129 | |||
130 | /** |
||
131 | * remove |
||
132 | * |
||
133 | * @param object $model |
||
134 | * @access public |
||
135 | * @return void |
||
136 | * |
||
137 | * @TODO STILL NEEDS TO BE CONVERTED TO ENTITY MODEL |
||
138 | */ |
||
139 | public function remove($model) |
||
147 | |||
148 | /** |
||
149 | * update |
||
150 | * |
||
151 | * @param object $model |
||
152 | * @access public |
||
153 | * @return void |
||
154 | */ |
||
155 | public function update($model, $serializationContext = [], $queryParams = []) |
||
178 | |||
179 | /** |
||
180 | * persist |
||
181 | * |
||
182 | * @param object $model |
||
183 | * @access public |
||
184 | * @return void |
||
185 | */ |
||
186 | public function persist($model, $serializationContext = [], $queryParams = []) |
||
209 | |||
210 | /** |
||
211 | * Adds support for magic finders. |
||
212 | * |
||
213 | * @param string $method |
||
214 | * @param mixed $arguments |
||
215 | * |
||
216 | * @return array|object The found entity/entities. |
||
217 | */ |
||
218 | public function __call($method, $arguments) |
||
293 | |||
294 | /** |
||
295 | * fetchFromCache |
||
296 | * |
||
297 | * @access protected |
||
298 | * @param string $key |
||
299 | * @return object|false |
||
300 | */ |
||
301 | protected function fetchFromCache($key) |
||
316 | |||
317 | /** |
||
318 | * saveToCache |
||
319 | * |
||
320 | * @access protected |
||
321 | * @return object |
||
322 | */ |
||
323 | protected function saveToCache($key, $value) |
||
337 | |||
338 | /** |
||
339 | * removeFromCache |
||
340 | * |
||
341 | * @param string $key |
||
342 | * @access private |
||
343 | * @return boolean true if no cache or cache successfully cleared, false otherwise |
||
344 | */ |
||
345 | protected function removeFromCache($key) |
||
359 | |||
360 | /** |
||
361 | * addQueryParameter |
||
362 | * |
||
363 | * @param string $path path to call |
||
364 | * @param array $params query parameters to add |
||
365 | * @access private |
||
366 | * @return string |
||
367 | */ |
||
368 | protected function addQueryParameter($path, $params = []) |
||
376 | |||
377 | /** |
||
378 | * convertQueryParameters |
||
379 | * |
||
380 | * @param array $queryParameters |
||
381 | * @access private |
||
382 | * @return array |
||
383 | */ |
||
384 | private function convertQueryParameters($queryParameters) |
||
414 | |||
415 | /** |
||
416 | * normalizeCacheKey |
||
417 | * |
||
418 | * @access private |
||
419 | * @return string |
||
420 | */ |
||
421 | private function normalizeCacheKey($key) |
||
425 | |||
426 | private function getClassMetadata() |
||
436 | } |
||
437 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.