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:
| 1 | <?php  | 
            ||
| 16 | class EntityCache  | 
            ||
| 17 | { | 
            ||
| 18 | /**  | 
            ||
| 19 | * Entity's raw attributes/relationships  | 
            ||
| 20 | *  | 
            ||
| 21 | * @var array  | 
            ||
| 22 | */  | 
            ||
| 23 | protected $cache = [];  | 
            ||
| 24 | |||
| 25 | /**  | 
            ||
| 26 | * Entity Map for the current Entity Type  | 
            ||
| 27 | * @var \Analogue\ORM\EntityMap  | 
            ||
| 28 | */  | 
            ||
| 29 | protected $entityMap;  | 
            ||
| 30 | |||
| 31 | /**  | 
            ||
| 32 | * Wrapper factory  | 
            ||
| 33 | *  | 
            ||
| 34 | * @var \Analogue\ORM\System\Wrappers\Factory  | 
            ||
| 35 | */  | 
            ||
| 36 | protected $factory;  | 
            ||
| 37 | |||
| 38 | /**  | 
            ||
| 39 | * Associative array containing list of pivot attributes per relationship  | 
            ||
| 40 | * so we don't have to call relationship method on refresh.  | 
            ||
| 41 | *  | 
            ||
| 42 | * @var array  | 
            ||
| 43 | */  | 
            ||
| 44 | protected $pivotAttributes = [];  | 
            ||
| 45 | |||
| 46 | /**  | 
            ||
| 47 | * EntityCache constructor.  | 
            ||
| 48 | * @param EntityMap $entityMap  | 
            ||
| 49 | */  | 
            ||
| 50 | public function __construct(EntityMap $entityMap)  | 
            ||
| 51 |     { | 
            ||
| 52 | $this->entityMap = $entityMap;  | 
            ||
| 53 | |||
| 54 | $this->factory = new Factory;  | 
            ||
| 55 | }  | 
            ||
| 56 | |||
| 57 | /**  | 
            ||
| 58 | * Add an array of key=>attributes representing  | 
            ||
| 59 | * the initial state of loaded entities.  | 
            ||
| 60 | *  | 
            ||
| 61 | * @param array $entities  | 
            ||
| 62 | */  | 
            ||
| 63 | public function add(array $entities)  | 
            ||
| 64 |     { | 
            ||
| 65 |         if (count($this->cache) == 0) { | 
            ||
| 66 | $this->cache = $entities;  | 
            ||
| 67 |         } else { | 
            ||
| 68 | $this->mergeCacheResults($entities);  | 
            ||
| 69 | }  | 
            ||
| 70 | }  | 
            ||
| 71 | |||
| 72 | /**  | 
            ||
| 73 | * Retrieve initial attributes for a single entity  | 
            ||
| 74 | *  | 
            ||
| 75 | * @param string $id  | 
            ||
| 76 | * @return array  | 
            ||
| 77 | */  | 
            ||
| 78 | public function get($id)  | 
            ||
| 79 |     { | 
            ||
| 80 |         if ($this->has($id)) { | 
            ||
| 81 | return $this->cache[$id];  | 
            ||
| 82 |         } else { | 
            ||
| 83 | return [];  | 
            ||
| 84 | }  | 
            ||
| 85 | }  | 
            ||
| 86 | |||
| 87 | /**  | 
            ||
| 88 | * Check if a record for this id exists.  | 
            ||
| 89 | *  | 
            ||
| 90 | * @param string $id  | 
            ||
| 91 | * @return boolean  | 
            ||
| 92 | */  | 
            ||
| 93 | public function has($id)  | 
            ||
| 94 |     { | 
            ||
| 95 | return array_key_exists($id, $this->cache);  | 
            ||
| 96 | }  | 
            ||
| 97 | |||
| 98 | /**  | 
            ||
| 99 | * Combine new result set with existing attributes in  | 
            ||
| 100 | * cache.  | 
            ||
| 101 | *  | 
            ||
| 102 | * @param array $entities  | 
            ||
| 103 | * @return void  | 
            ||
| 104 | */  | 
            ||
| 105 | protected function mergeCacheResults($entities)  | 
            ||
| 106 |     { | 
            ||
| 107 |         foreach ($entities as $key => $entity) { | 
            ||
| 108 | $this->cache[$key] = $entity;  | 
            ||
| 109 | }  | 
            ||
| 110 | }  | 
            ||
| 111 | |||
| 112 | /**  | 
            ||
| 113 | * Cache Relation's query result for an entity  | 
            ||
| 114 | *  | 
            ||
| 115 | * @param mixed $parent  | 
            ||
| 116 | * @param string $relation name of the relation  | 
            ||
| 117 | * @param mixed $results results of the relationship's query  | 
            ||
| 118 | * @param Relationship $relationship  | 
            ||
| 119 | * @throws MappingException  | 
            ||
| 120 | * @return void  | 
            ||
| 121 | */  | 
            ||
| 122 | public function cacheLoadedRelationResult($parent, $relation, $results, Relationship $relationship)  | 
            ||
| 142 | |||
| 143 | /**  | 
            ||
| 144 | * Create a cachedRelationship instance which will hold related entity's hash and pivot attributes, if any.  | 
            ||
| 145 | *  | 
            ||
| 146 | * @param string $parentKey  | 
            ||
| 147 | * @param string $relation  | 
            ||
| 148 | * @param array $result  | 
            ||
| 149 | * @param Relationship $relationship  | 
            ||
| 150 | * @throws MappingException  | 
            ||
| 151 | * @return CachedRelationship  | 
            ||
| 152 | */  | 
            ||
| 153 | protected function getCachedRelationship($parentKey, $relation, $result, Relationship $relationship)  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 154 |     { | 
            ||
| 155 | $pivotColumns = $relationship->getPivotAttributes();  | 
            ||
| 156 | |||
| 157 |         if (!array_key_exists($relation, $this->pivotAttributes)) { | 
            ||
| 158 | $this->pivotAttributes[$relation] = $pivotColumns;  | 
            ||
| 159 | }  | 
            ||
| 160 | |||
| 161 | $wrapper = $this->factory->make($result);  | 
            ||
| 162 | |||
| 163 | $hash = $this->getEntityHash($wrapper);  | 
            ||
| 164 | |||
| 165 |         if (count($pivotColumns) > 0) { | 
            ||
| 166 | $pivotAttributes = [];  | 
            ||
| 167 |             foreach ($pivotColumns as $column) { | 
            ||
| 168 |                 $pivot = $wrapper->getEntityAttribute('pivot'); | 
            ||
| 169 | |||
| 170 | $pivotWrapper = $this->factory->make($pivot);  | 
            ||
| 171 | |||
| 172 | $pivotAttributes[$column] = $pivotWrapper->getEntityAttribute($column);  | 
            ||
| 173 | }  | 
            ||
| 174 | |||
| 175 | $cachedRelationship = new CachedRelationship($hash, $pivotAttributes);  | 
            ||
| 176 |         } else { | 
            ||
| 177 | $cachedRelationship = new CachedRelationship($hash);  | 
            ||
| 178 | }  | 
            ||
| 179 | |||
| 180 | return $cachedRelationship;  | 
            ||
| 181 | }  | 
            ||
| 182 | |||
| 183 | /**  | 
            ||
| 184 | * Cache a many relationship  | 
            ||
| 185 | *  | 
            ||
| 186 | * @param $parentKey  | 
            ||
| 187 | * @param string $relation  | 
            ||
| 188 | * @param EntityCollection $results  | 
            ||
| 189 | * @param Relationship $relationship  | 
            ||
| 190 | * @throws MappingException  | 
            ||
| 191 | */  | 
            ||
| 192 | protected function cacheManyRelationResults($parentKey, $relation, $results, Relationship $relationship)  | 
            ||
| 193 |     { | 
            ||
| 194 | $this->cache[$parentKey][$relation] = [];  | 
            ||
| 195 | |||
| 196 |         foreach ($results as $result) { | 
            ||
| 197 | $cachedRelationship = $this->getCachedRelationship($parentKey, $relation, $result, $relationship);  | 
            ||
| 198 | |||
| 199 | $relatedHash = $cachedRelationship->getHash();  | 
            ||
| 200 | |||
| 201 | $this->cache[$parentKey][$relation][$relatedHash] = $cachedRelationship;  | 
            ||
| 202 | }  | 
            ||
| 203 | }  | 
            ||
| 204 | |||
| 205 | /**  | 
            ||
| 206 | * Cache a single relationship  | 
            ||
| 207 | *  | 
            ||
| 208 | * @param $parentKey  | 
            ||
| 209 | * @param string $relation  | 
            ||
| 210 | * @param Mappable $result  | 
            ||
| 211 | * @param Relationship $relationship  | 
            ||
| 212 | * @throws MappingException  | 
            ||
| 213 | */  | 
            ||
| 214 | protected function cacheSingleRelationResult($parentKey, $relation, $result, Relationship $relationship)  | 
            ||
| 218 | |||
| 219 | /**  | 
            ||
| 220 | * Get Entity's Hash  | 
            ||
| 221 | *  | 
            ||
| 222 | * @param $entity  | 
            ||
| 223 | * @throws MappingException  | 
            ||
| 224 | * @return string  | 
            ||
| 225 | */  | 
            ||
| 226 | View Code Duplication | protected function getEntityHash(InternallyMappable $entity)  | 
            |
| 236 | |||
| 237 | /**  | 
            ||
| 238 | * Refresh the cache record for an aggregated entity after a write operation  | 
            ||
| 239 | * @param Aggregate $entity  | 
            ||
| 240 | */  | 
            ||
| 241 | public function refresh(Aggregate $entity)  | 
            ||
| 242 |     { | 
            ||
| 243 | $this->cache[$entity->getEntityId()] = $this->transform($entity);  | 
            ||
| 244 | }  | 
            ||
| 245 | |||
| 246 | /**  | 
            ||
| 247 | * Transform an Aggregated Entity into a cache record  | 
            ||
| 248 | *  | 
            ||
| 249 | * @param Aggregate $aggregatedEntity  | 
            ||
| 250 | * @throws MappingException  | 
            ||
| 251 | * @return array  | 
            ||
| 252 | */  | 
            ||
| 253 | protected function transform(Aggregate $aggregatedEntity)  | 
            ||
| 296 | |||
| 297 | /**  | 
            ||
| 298 | * Get pivot attributes for a relation  | 
            ||
| 299 | *  | 
            ||
| 300 | * @param string $relation  | 
            ||
| 301 | * @param InternallyMappable $entity  | 
            ||
| 302 | * @return array  | 
            ||
| 303 | */  | 
            ||
| 304 | protected function getPivotValues($relation, InternallyMappable $entity)  | 
            ||
| 320 | |||
| 321 | /**  | 
            ||
| 322 | * Clear the entity Cache. Use with caution as it could result  | 
            ||
| 323 | * in impredictable behaviour if the cached entities are stored  | 
            ||
| 324 | * after the cache clear operation.  | 
            ||
| 325 | *  | 
            ||
| 326 | * @return void  | 
            ||
| 327 | */  | 
            ||
| 328 | public function clear()  | 
            ||
| 333 | }  | 
            ||
| 334 | 
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.