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 |
||
13 | class PostController extends Controller |
||
14 | { |
||
15 | /** |
||
16 | * Instantiate a new controller instance. |
||
17 | * |
||
18 | * @return void |
||
|
|||
19 | */ |
||
20 | public function __construct() |
||
24 | |||
25 | /** |
||
26 | * Display a listing of the resource. |
||
27 | * |
||
28 | * @return \Illuminate\Http\Response |
||
29 | */ |
||
30 | public function index() |
||
38 | |||
39 | /** |
||
40 | * Show the form for creating a new resource. |
||
41 | * |
||
42 | * @return \Illuminate\Http\Response |
||
43 | */ |
||
44 | View Code Duplication | public function create() |
|
54 | |||
55 | /** |
||
56 | * Store a newly created resource in storage. |
||
57 | * |
||
58 | * @param \Chriscreates\Blog\Requests\ValidatePostRequest $request |
||
59 | * @return \Illuminate\Http\JsonResponse |
||
60 | */ |
||
61 | public function store(ValidatePostRequest $request) : JsonResponse |
||
76 | |||
77 | /** |
||
78 | * Show the form for editing the specified resource. |
||
79 | * |
||
80 | * @param \Chriscreates\Blog\Post $post |
||
81 | * @return \Illuminate\Http\Response |
||
82 | */ |
||
83 | View Code Duplication | public function edit(Post $post) |
|
93 | |||
94 | /** |
||
95 | * Update the specified resource in storage. |
||
96 | * |
||
97 | * @param \Chriscreates\Blog\Requests\ValidatePostRequest $request |
||
98 | * @param \Chriscreates\Blog\Post $post |
||
99 | * @return \Illuminate\Http\JsonResponse |
||
100 | */ |
||
101 | public function update(ValidatePostRequest $request, Post $post) : JsonResponse |
||
118 | |||
119 | /** |
||
120 | * Remove the specified resource from storage. |
||
121 | * |
||
122 | * @param \Chriscreates\Blog\Post $post |
||
123 | * @return \Illuminate\Http\JsonResponse |
||
124 | */ |
||
125 | public function destroy(Post $post) : JsonResponse |
||
133 | } |
||
134 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.