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 namespace Anomaly\Streams\Platform\Model; |
||
14 | class EloquentCollection extends Collection |
||
15 | { |
||
16 | |||
17 | use Hookable; |
||
18 | |||
19 | /** |
||
20 | * Return the item IDs. |
||
21 | * |
||
22 | * @return array |
||
23 | */ |
||
24 | public function ids() |
||
28 | |||
29 | /** |
||
30 | * Return a collection of decorated items. |
||
31 | * |
||
32 | * @return static |
||
33 | */ |
||
34 | public function decorated() |
||
46 | |||
47 | /** |
||
48 | * Pad to the specified size with a value. |
||
49 | * |
||
50 | * @param $size |
||
51 | * @param null $value |
||
52 | * @return $this |
||
53 | */ |
||
54 | public function pad($size, $value = null) |
||
70 | |||
71 | /** |
||
72 | * Find a model by key. |
||
73 | * |
||
74 | * @param $key |
||
75 | * @param $value |
||
76 | * @return EloquentModel |
||
77 | */ |
||
78 | public function findBy($key, $value) |
||
86 | |||
87 | /** |
||
88 | * An alias for slice. |
||
89 | * |
||
90 | * @param $offset |
||
91 | * @return $this |
||
92 | */ |
||
93 | public function skip($offset) |
||
97 | |||
98 | /** |
||
99 | * Return undecorated items. |
||
100 | * |
||
101 | * @return static|$this |
||
102 | */ |
||
103 | public function undecorate() |
||
107 | |||
108 | /** |
||
109 | * Map to get. |
||
110 | * |
||
111 | * @param $name |
||
112 | * @return mixed |
||
113 | */ |
||
114 | public function __get($name) |
||
126 | |||
127 | /** |
||
128 | * Map to get. |
||
129 | * |
||
130 | * @param string $method |
||
131 | * @param array $parameters |
||
132 | */ |
||
133 | View Code Duplication | public function __call($method, $parameters) |
|
141 | } |
||
142 |
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.