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 |
||
| 10 | class HasManyThrough extends Relationship |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * The distance parent Entity instance. |
||
| 14 | * |
||
| 15 | * @var \Analogue\ORM\Entity |
||
| 16 | */ |
||
| 17 | protected $farParent; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The far parent map instance |
||
| 21 | * |
||
| 22 | * @var \Analogue\ORM\EntityMap |
||
| 23 | */ |
||
| 24 | protected $farParentMap; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The near key on the relationship. |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $firstKey; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The far key on the relationship. |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $secondKey; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Create a new has many relationship instance. |
||
| 42 | * |
||
| 43 | * @param Mapper $mapper |
||
| 44 | * @param \Analogue\ORM\Mappable $farParent |
||
| 45 | * @param \Analogue\ORM\EntityMap $parentMap |
||
| 46 | * @param string $firstKey |
||
| 47 | * @param string $secondKey |
||
| 48 | * @throws \Analogue\ORM\Exceptions\MappingException |
||
| 49 | */ |
||
| 50 | public function __construct(Mapper $mapper, $farParent, $parentMap, $firstKey, $secondKey) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param $related |
||
| 64 | * @return mixed |
||
| 65 | */ |
||
| 66 | public function attachTo($related) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param $related |
||
| 73 | * @return mixed |
||
| 74 | */ |
||
| 75 | public function detachFrom($related) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Set the base constraints on the relation query. |
||
| 82 | * |
||
| 83 | * @return void |
||
| 84 | */ |
||
| 85 | public function addConstraints() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Add the constraints for a relationship count query. |
||
| 104 | * |
||
| 105 | * @param Query $query |
||
| 106 | * @param Query $parent |
||
| 107 | * @return Query |
||
| 108 | */ |
||
| 109 | public function getRelationCountQuery(Query $query, Query $parent) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Set the join clause on the query. |
||
| 124 | * |
||
| 125 | * @param null|Query $query |
||
| 126 | * @return void |
||
| 127 | */ |
||
| 128 | protected function setJoin(Query $query = null) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Set the constraints for an eager load of the relation. |
||
| 139 | * |
||
| 140 | * @param array $entities |
||
| 141 | * @return void |
||
| 142 | */ |
||
| 143 | public function addEagerConstraints(array $entities) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Initialize the relation on a set of entities. |
||
| 152 | * |
||
| 153 | * @param \Analogue\ORM\Entity[] $entities |
||
| 154 | * @param string $relation |
||
| 155 | * @return \Analogue\ORM\Entity[] |
||
| 156 | */ |
||
| 157 | public function initRelation(array $entities, $relation) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Match the eagerly loaded results to their parents. |
||
| 168 | * |
||
| 169 | * @param \Analogue\ORM\Entity[] $entities |
||
| 170 | * @param EntityCollection $results |
||
| 171 | * @param string $relation |
||
| 172 | * @return \Analogue\ORM\Entity[] |
||
| 173 | */ |
||
| 174 | public function match(array $entities, EntityCollection $results, $relation) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Build model dictionary keyed by the relation's foreign key. |
||
| 202 | * |
||
| 203 | * @param EntityCollection $results |
||
| 204 | * @return array |
||
| 205 | */ |
||
| 206 | View Code Duplication | protected function buildDictionary(EntityCollection $results) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Get the results of the relationship. |
||
| 224 | * |
||
| 225 | * @param $relation |
||
| 226 | * @return EntityCollection |
||
| 227 | */ |
||
| 228 | public function getResults($relation) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Execute the query as a "select" statement. |
||
| 239 | * |
||
| 240 | * @param array $columns |
||
| 241 | * @return EntityCollection |
||
| 242 | */ |
||
| 243 | public function get($columns = ['*']) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Set the select clause for the relation query. |
||
| 264 | * |
||
| 265 | * @param array $columns |
||
| 266 | * @return BelongsToMany |
||
| 267 | */ |
||
| 268 | protected function getSelectColumns(array $columns = ['*']) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Get a paginator for the "select" statement. |
||
| 279 | * |
||
| 280 | * @param int $perPage |
||
| 281 | * @param array $columns |
||
| 282 | * @return \Illuminate\Pagination\LengthAwarePaginator |
||
| 283 | */ |
||
| 284 | public function paginate($perPage = null, $columns = ['*']) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Get the key name of the parent model. |
||
| 293 | * |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | protected function getQualifiedParentKeyName() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Get the key for comparing against the parent key in "has" query. |
||
| 303 | * |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | public function getHasCompareKey() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Run synchronization content if needed by the |
||
| 313 | * relation type. |
||
| 314 | * |
||
| 315 | * @param array $actualContent |
||
| 316 | * @return void |
||
| 317 | */ |
||
| 318 | public function sync(array $entities) |
||
| 322 | } |
||
| 323 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.