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 |
||
8 | class EloquentRepository extends AbstractRepository |
||
9 | { |
||
10 | /** |
||
11 | * $converter. |
||
12 | * |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $compiler = EloquentCompiler::class; |
||
16 | |||
17 | /** |
||
18 | * __construct. |
||
19 | * |
||
20 | * @param \Illuminate\Database\Eloquent\Model $model |
||
21 | */ |
||
22 | 25 | public function __construct(Model $model) |
|
26 | |||
27 | /** |
||
28 | * get. |
||
29 | * |
||
30 | * @param \Recca0120\Repository\Criteria|array $criteria |
||
31 | * @param int $limit |
||
32 | * @param int $offset |
||
33 | * @return \Illuminate\Support\Collection |
||
34 | */ |
||
35 | 5 | View Code Duplication | public function get($criteria = [], $columns = ['*'], $limit = null, $offset = null) |
49 | |||
50 | /** |
||
51 | * paginate. |
||
52 | * |
||
53 | * @param mixed $criteria |
||
54 | * @param string $perPage |
||
55 | * @param int $pageName |
||
56 | * @param int $page |
||
57 | * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator |
||
58 | */ |
||
59 | 3 | public function paginate($criteria = [], $perPage = null, $columns = ['*'], $pageName = 'page', $page = null) |
|
65 | |||
66 | /** |
||
67 | * simplePaginate. |
||
68 | * |
||
69 | * @param mixed $criteria |
||
70 | * @param string $perPage |
||
71 | * @param int $pageName |
||
72 | * @param int $page |
||
73 | * @return \Illuminate\Contracts\Pagination\Paginator |
||
74 | */ |
||
75 | 1 | public function simplePaginate($criteria = [], $perPage = null, $columns = ['*'], $pageName = 'page', $page = null) |
|
81 | |||
82 | /** |
||
83 | * first. |
||
84 | * |
||
85 | * @param \Recca0120\Repository\Criteria|array $criteria |
||
86 | * @return mixed |
||
87 | */ |
||
88 | 2 | public function first($criteria = [], $columns = ['*']) |
|
92 | |||
93 | /** |
||
94 | * find. |
||
95 | * |
||
96 | * @param int $id |
||
97 | * @return mixed |
||
98 | */ |
||
99 | 4 | View Code Duplication | public function find($id, $columns = ['*']) |
106 | |||
107 | /** |
||
108 | * newInstance. |
||
109 | * |
||
110 | * @param array $attributes |
||
111 | * @return \Illuminate\Database\Eloquent\Model |
||
112 | */ |
||
113 | 4 | View Code Duplication | public function newInstance($attributes = []) |
120 | |||
121 | /** |
||
122 | * create. |
||
123 | * |
||
124 | * @param array $attributes |
||
125 | * @param bool $forceFill |
||
126 | * @return mixed |
||
127 | */ |
||
128 | 2 | View Code Duplication | public function create($attributes, $forceFill = false) |
136 | |||
137 | /** |
||
138 | * update. |
||
139 | * |
||
140 | * @param array $attributes |
||
141 | * @param int $id |
||
142 | * @param bool $forceFill |
||
143 | * @return mixed |
||
144 | */ |
||
145 | 2 | View Code Duplication | public function update($attributes, $id, $forceFill = false) |
153 | |||
154 | /** |
||
155 | * delete. |
||
156 | * |
||
157 | * @param int $id |
||
158 | * @return bool |
||
159 | */ |
||
160 | 1 | public function delete($id) |
|
164 | |||
165 | /** |
||
166 | * destroy. |
||
167 | * |
||
168 | * @param \Recca0120\Repository\Criteria|array $criteria |
||
169 | * @return int |
||
170 | */ |
||
171 | public function destroy($criteria = []) |
||
175 | |||
176 | /** |
||
177 | * chunk. |
||
178 | * |
||
179 | * @param Criteria|array $criteria |
||
180 | * @param int $count |
||
181 | * @param callable $callback |
||
182 | * @return \Illuminate\Support\Collection |
||
183 | */ |
||
184 | 2 | public function chunk($criteria, $count, callable $callback) |
|
188 | } |
||
189 |
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.