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 |
||
11 | class BelongsTo extends Relationship |
||
12 | { |
||
13 | /** |
||
14 | * The foreign key of the parent model. |
||
15 | * |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $foreignKey; |
||
19 | |||
20 | /** |
||
21 | * The associated key on the parent model. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $otherKey; |
||
26 | |||
27 | /** |
||
28 | * The name of the relationship. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $relation; |
||
33 | |||
34 | /** |
||
35 | * Indicate if the parent entity hold the key for the relation. |
||
36 | * |
||
37 | * @var boolean |
||
38 | */ |
||
39 | protected static $ownForeignKey = true; |
||
40 | |||
41 | /** |
||
42 | * Create a new belongs to relationship instance. |
||
43 | * |
||
44 | * @param Mapper $mapper |
||
45 | * @param Mappable $parent |
||
46 | * @param string $foreignKey |
||
47 | * @param string $otherKey |
||
48 | * @param string $relation |
||
49 | */ |
||
50 | public function __construct(Mapper $mapper, $parent, $foreignKey, $otherKey, $relation) |
||
51 | { |
||
52 | $this->otherKey = $otherKey; |
||
53 | $this->relation = $relation; |
||
54 | $this->foreignKey = $foreignKey; |
||
55 | |||
56 | parent::__construct($mapper, $parent); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @param $related |
||
61 | * @return mixed |
||
62 | */ |
||
63 | public function attachTo($related) |
||
67 | |||
68 | /** |
||
69 | * @param $related |
||
70 | * @return Mappable |
||
71 | */ |
||
72 | public function detachFrom($related) |
||
76 | |||
77 | /** |
||
78 | * Get the results of the relationship. |
||
79 | * |
||
80 | * @param $relation |
||
81 | * |
||
82 | * @return \Analogue\ORM\Entity |
||
83 | */ |
||
84 | public function getResults($relation) |
||
92 | |||
93 | /** |
||
94 | * Set the base constraints on the relation query. |
||
95 | * |
||
96 | * @return void |
||
97 | */ |
||
98 | public function addConstraints() |
||
109 | |||
110 | /** |
||
111 | * Add the constraints for a relationship count query. |
||
112 | * |
||
113 | * @param Query $query |
||
114 | * @param Query $parent |
||
115 | * @return Query |
||
116 | */ |
||
117 | public function getRelationCountQuery(Query $query, Query $parent) |
||
125 | |||
126 | /** |
||
127 | * Set the constraints for an eager load of the relation. |
||
128 | * |
||
129 | * @param array $entities |
||
130 | * @return void |
||
131 | */ |
||
132 | public function addEagerConstraints(array $entities) |
||
141 | |||
142 | /** |
||
143 | * Gather the keys from an array of related models. |
||
144 | * |
||
145 | * @param array $entities |
||
146 | * @return array |
||
147 | */ |
||
148 | protected function getEagerModelKeys(array $entities) |
||
172 | |||
173 | /** |
||
174 | * Initialize the relation on a set of models. |
||
175 | * |
||
176 | * @param array $entities |
||
177 | * @param string $relation |
||
178 | * @return array |
||
179 | */ |
||
180 | public function initRelation(array $entities, $relation) |
||
189 | |||
190 | /** |
||
191 | * Match the eagerly loaded results to their parents. |
||
192 | * |
||
193 | * @param array $entities |
||
194 | * @param EntityCollection $results |
||
195 | * @param string $relation |
||
196 | * @return array |
||
197 | */ |
||
198 | public function match(array $entities, EntityCollection $results, $relation) |
||
227 | |||
228 | public function sync(array $entities) |
||
240 | |||
241 | /** |
||
242 | * Associate the model instance to the given parent. |
||
243 | * |
||
244 | * @param mixed $entity |
||
245 | * @return void |
||
246 | */ |
||
247 | public function associate($entity) |
||
251 | |||
252 | /** |
||
253 | * Dissociate previously associated model from the given parent. |
||
254 | * |
||
255 | * @return Mappable |
||
256 | */ |
||
257 | public function dissociate() |
||
266 | |||
267 | /** |
||
268 | * Get the foreign key of the relationship. |
||
269 | * |
||
270 | * @return string |
||
271 | */ |
||
272 | public function getForeignKey() |
||
276 | |||
277 | /** |
||
278 | * Get the foreign key value pair for a related object |
||
279 | * |
||
280 | * @param mixed $related |
||
281 | * |
||
282 | * @return array |
||
283 | */ |
||
284 | View Code Duplication | public function getForeignKeyValuePair($related) |
|
298 | |||
299 | /** |
||
300 | * Get the fully qualified foreign key of the relationship. |
||
301 | * |
||
302 | * @return string |
||
303 | */ |
||
304 | public function getQualifiedForeignKey() |
||
308 | |||
309 | /** |
||
310 | * Get the associated key of the relationship. |
||
311 | * |
||
312 | * @return string |
||
313 | */ |
||
314 | public function getOtherKey() |
||
318 | |||
319 | /** |
||
320 | * Get the fully qualified associated key of the relationship. |
||
321 | * |
||
322 | * @return string |
||
323 | */ |
||
324 | public function getQualifiedOtherKeyName() |
||
328 | } |
||
329 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.