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) |
||
62 | |||
63 | /** |
||
64 | * @param $related |
||
65 | * @return mixed |
||
66 | */ |
||
67 | public function attachTo($related) |
||
71 | |||
72 | /** |
||
73 | * @param $related |
||
74 | * @return mixed |
||
75 | */ |
||
76 | public function detachFrom($related) |
||
80 | |||
81 | /** |
||
82 | * Set the base constraints on the relation query. |
||
83 | * |
||
84 | * @return void |
||
85 | */ |
||
86 | public function addConstraints() |
||
102 | |||
103 | /** |
||
104 | * Add the constraints for a relationship count query. |
||
105 | * |
||
106 | * @param Query $query |
||
107 | * @param Query $parent |
||
108 | * @return Query |
||
109 | */ |
||
110 | public function getRelationCountQuery(Query $query, Query $parent) |
||
122 | |||
123 | /** |
||
124 | * Set the join clause on the query. |
||
125 | * |
||
126 | * @param null|Query $query |
||
127 | * @return void |
||
128 | */ |
||
129 | protected function setJoin(Query $query = null) |
||
137 | |||
138 | /** |
||
139 | * Set the constraints for an eager load of the relation. |
||
140 | * |
||
141 | * @param array $entities |
||
142 | * @return void |
||
143 | */ |
||
144 | public function addEagerConstraints(array $entities) |
||
150 | |||
151 | /** |
||
152 | * Initialize the relation on a set of entities. |
||
153 | * |
||
154 | * @param \Analogue\ORM\Entity[] $entities |
||
155 | * @param string $relation |
||
156 | * @return \Analogue\ORM\Entity[] |
||
157 | */ |
||
158 | public function initRelation(array $entities, $relation) |
||
166 | |||
167 | /** |
||
168 | * Match the eagerly loaded results to their parents. |
||
169 | * |
||
170 | * @param \Analogue\ORM\Entity[] $entities |
||
171 | * @param EntityCollection $results |
||
172 | * @param string $relation |
||
173 | * @return \Analogue\ORM\Entity[] |
||
174 | */ |
||
175 | public function match(array $entities, EntityCollection $results, $relation) |
||
200 | |||
201 | /** |
||
202 | * Build model dictionary keyed by the relation's foreign key. |
||
203 | * |
||
204 | * @param EntityCollection $results |
||
205 | * @return array |
||
206 | */ |
||
207 | View Code Duplication | protected function buildDictionary(EntityCollection $results) |
|
222 | |||
223 | /** |
||
224 | * Get the results of the relationship. |
||
225 | * |
||
226 | * @param $relation |
||
227 | * @return EntityCollection |
||
228 | */ |
||
229 | public function getResults($relation) |
||
237 | |||
238 | /** |
||
239 | * Execute the query as a "select" statement. |
||
240 | * |
||
241 | * @param array $columns |
||
242 | * @return EntityCollection |
||
243 | */ |
||
244 | public function get($columns = ['*']) |
||
262 | |||
263 | /** |
||
264 | * Set the select clause for the relation query. |
||
265 | * |
||
266 | * @param array $columns |
||
267 | * @return BelongsToMany |
||
268 | */ |
||
269 | protected function getSelectColumns(array $columns = ['*']) |
||
277 | |||
278 | /** |
||
279 | * Get a paginator for the "select" statement. |
||
280 | * |
||
281 | * @param int $perPage |
||
282 | * @param array $columns |
||
283 | * @return \Illuminate\Pagination\LengthAwarePaginator |
||
284 | */ |
||
285 | public function paginate($perPage = null, $columns = ['*']) |
||
291 | |||
292 | /** |
||
293 | * Get the key name of the parent model. |
||
294 | * |
||
295 | * @return string |
||
296 | */ |
||
297 | protected function getQualifiedParentKeyName() |
||
301 | |||
302 | /** |
||
303 | * Get the key for comparing against the parent key in "has" query. |
||
304 | * |
||
305 | * @return string |
||
306 | */ |
||
307 | public function getHasCompareKey() |
||
311 | |||
312 | /** |
||
313 | * Run synchronization content if needed by the |
||
314 | * relation type. |
||
315 | * |
||
316 | * @param array $actualContent |
||
317 | * @return void |
||
318 | */ |
||
319 | public function sync(array $entities) |
||
323 | } |
||
324 |
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.