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 |
||
| 10 | class EntityRepository |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * REST Client. |
||
| 14 | * |
||
| 15 | * @var RestClient |
||
| 16 | */ |
||
| 17 | protected $restClient; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * SDK Client. |
||
| 21 | * |
||
| 22 | * @var SdkClient |
||
| 23 | */ |
||
| 24 | protected $sdk; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $entityName; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * classMetadataCache |
||
| 33 | * |
||
| 34 | * @var ClassMetadata |
||
| 35 | * @access private |
||
| 36 | */ |
||
| 37 | private $classMetadataCache; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * unitOfWork |
||
| 41 | * |
||
| 42 | * @var UnitOfWork |
||
| 43 | * @access private |
||
| 44 | */ |
||
| 45 | private $unitOfWork; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * EntityRepository constructor |
||
| 49 | * |
||
| 50 | * @param SdkClient $sdkClient The client to connect to the datasource with |
||
| 51 | * @param RestClient $restClient The client to process the http requests |
||
| 52 | * @param string $entityName The entity to work with |
||
| 53 | */ |
||
| 54 | public function __construct(SdkClient $sdkClient, RestClient $restClient, UnitOfWork $unitOfWork, $entityName) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * find - finds one item of the entity based on the @REST\Id field in the entity |
||
| 64 | * |
||
| 65 | * @param string $id id of the element to fetch |
||
| 66 | * @param array $queryParams query parameters to add to the query |
||
| 67 | * @access public |
||
| 68 | * @return object |
||
| 69 | */ |
||
| 70 | public function find($id, $queryParams = []) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * findAll |
||
| 95 | * |
||
| 96 | * @access public |
||
| 97 | * @return array |
||
| 98 | */ |
||
| 99 | public function findAll() |
||
| 100 | { |
||
| 101 | $mapping = $this->sdk->getMapping(); |
||
| 102 | $key = $this->getClassMetadata()->getKey(); |
||
| 103 | $prefix = $mapping->getIdPrefix(); |
||
| 104 | $path = empty($prefix) ? '/' . $key : $prefix . '/' . $key; |
||
| 105 | |||
| 106 | $entityListFromCache = $this->fetchFromCache($path); |
||
| 107 | |||
| 108 | // if entityList is found in cache, return it |
||
| 109 | if ($entityListFromCache !== false) { |
||
| 110 | return $entityListFromCache; |
||
| 111 | } |
||
| 112 | |||
| 113 | $data = $this->restClient->get($path); |
||
| 114 | |||
| 115 | $hydrator = $this->sdk->getModelHydrator(); |
||
| 116 | $entityList = $hydrator->hydrateList($data, $this->entityName); |
||
| 117 | |||
| 118 | // cache entity list |
||
| 119 | $this->saveToCache($path, $entityList); |
||
| 120 | |||
| 121 | // then cache each entity from list |
||
| 122 | View Code Duplication | foreach ($entityList as $entity) { |
|
| 123 | $identifier = $entity->{$this->getClassMetadata()->getIdGetter()}(); |
||
| 124 | $this->unitOfWork->registerClean($identifier, $entity); |
||
| 125 | $this->saveToCache($identifier, $entity); |
||
| 126 | } |
||
| 127 | |||
| 128 | return $entityList; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * remove |
||
| 133 | * |
||
| 134 | * @param object $model |
||
| 135 | * @access public |
||
| 136 | * @return void |
||
| 137 | * |
||
| 138 | * @TODO STILL NEEDS TO BE CONVERTED TO ENTITY MODEL |
||
| 139 | */ |
||
| 140 | public function remove($model) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * update |
||
| 151 | * |
||
| 152 | * @param object $model |
||
| 153 | * @access public |
||
| 154 | * @return void |
||
| 155 | */ |
||
| 156 | public function update($model, $serializationContext = [], $queryParams = []) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * persist |
||
| 177 | * |
||
| 178 | * @param object $model |
||
| 179 | * @access public |
||
| 180 | * @return void |
||
| 181 | */ |
||
| 182 | public function persist($model, $serializationContext = [], $queryParams = []) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Adds support for magic finders. |
||
| 200 | * |
||
| 201 | * @param string $method |
||
| 202 | * @param mixed $arguments |
||
| 203 | * |
||
| 204 | * @return array|object The found entity/entities. |
||
| 205 | */ |
||
| 206 | public function __call($method, $arguments) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * fetchFromCache |
||
| 284 | * |
||
| 285 | * @access protected |
||
| 286 | * @param string $key |
||
| 287 | * @return object|false |
||
| 288 | */ |
||
| 289 | protected function fetchFromCache($key) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * saveToCache |
||
| 307 | * |
||
| 308 | * @access protected |
||
| 309 | * @return object |
||
| 310 | */ |
||
| 311 | protected function saveToCache($key, $value) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * removeFromCache |
||
| 328 | * |
||
| 329 | * @param string $key |
||
| 330 | * @access private |
||
| 331 | * @return boolean true if no cache or cache successfully cleared, false otherwise |
||
| 332 | */ |
||
| 333 | protected function removeFromCache($key) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * addQueryParameter |
||
| 350 | * |
||
| 351 | * @param string $path path to call |
||
| 352 | * @param array $params query parameters to add |
||
| 353 | * @access private |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | protected function addQueryParameter($path, $params = []) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * convertQueryParameters |
||
| 367 | * |
||
| 368 | * @param array $queryParameters |
||
| 369 | * @access private |
||
| 370 | * @return array |
||
| 371 | */ |
||
| 372 | private function convertQueryParameters($queryParameters) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * normalizeCacheKey |
||
| 405 | * |
||
| 406 | * @access private |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | private function normalizeCacheKey($key) |
||
| 413 | |||
| 414 | private function getClassMetadata() |
||
| 424 | } |
||
| 425 |
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.