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 |
||
7 | class ResourceController extends Controller |
||
8 | { |
||
9 | /** |
||
10 | * Display a listing of the resource. |
||
11 | * |
||
12 | * @return \Illuminate\Http\Response |
||
13 | */ |
||
14 | public function index() |
||
18 | /** |
||
19 | * Store a newly created resource in storage. |
||
20 | * |
||
21 | * @param \Illuminate\Http\Request $request |
||
|
|||
22 | * @return \Illuminate\Http\Response |
||
23 | */ |
||
24 | public function store() |
||
29 | /** |
||
30 | * Display the specified resource. |
||
31 | * |
||
32 | * @param int $id |
||
33 | * @return \Illuminate\Http\Response |
||
34 | */ |
||
35 | public function show($id) |
||
41 | /** |
||
42 | * Update the specified resource in storage. |
||
43 | * |
||
44 | * @param \Illuminate\Http\Request $request |
||
45 | * @param int $id |
||
46 | * @return \Illuminate\Http\Response |
||
47 | */ |
||
48 | View Code Duplication | public function update($id, Request $request) |
|
49 | { |
||
50 | return $this->respondWithItem($this->model, function ($model) use ($id, $request) { |
||
51 | $item = $model->findOrFail($id); |
||
52 | $item->fill($request->all()); |
||
53 | $item->save(); |
||
54 | return $item; |
||
55 | }); |
||
56 | } |
||
57 | /** |
||
58 | * Remove the specified resource from storage. |
||
59 | * |
||
60 | * @param int $id |
||
61 | * @return \Illuminate\Http\Response |
||
62 | */ |
||
63 | public function destroy($id) |
||
68 | } |
||
69 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.