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 PageController extends Controller |
||
9 | { |
||
10 | /** |
||
11 | * Return all projects. |
||
12 | * |
||
13 | * @return \Illuminate\Http\JsonResponse |
||
14 | */ |
||
15 | public function index() |
||
19 | |||
20 | /** |
||
21 | * Show an individual page in the manager. |
||
22 | * |
||
23 | * @param string $slug Slug of the page to show. |
||
24 | * |
||
25 | * @return \Illuminate\Http\Response |
||
26 | */ |
||
27 | View Code Duplication | public function show($slug) |
|
42 | |||
43 | /** |
||
44 | * Return the project create page. |
||
45 | * |
||
46 | * @return \Illuminate\Http\Response |
||
47 | */ |
||
48 | public function create() |
||
52 | |||
53 | /** |
||
54 | * Add a new project to the portfolio. |
||
55 | * |
||
56 | * @param AddProjectRequest $request Form request. |
||
57 | * |
||
58 | * @return \Illuminate\Http\Response |
||
59 | */ |
||
60 | public function store(AddProjectRequest $request) |
||
70 | |||
71 | /** |
||
72 | * Return the project edit form view. |
||
73 | * |
||
74 | * @param string $slug Slug for the project to edit. |
||
75 | * |
||
76 | * @return \Illuminate\Http\Response |
||
77 | */ |
||
78 | public function edit($slug) |
||
92 | |||
93 | /** |
||
94 | * Update a project. |
||
95 | * |
||
96 | * @param \Illuminate\Http\Request $request Request data. |
||
97 | * @param string $slug Slug of project to update. |
||
98 | * |
||
99 | * @return \Illuminate\Http\Response |
||
100 | */ |
||
101 | public function update(Request $request, $slug) |
||
117 | |||
118 | /** |
||
119 | * Remove a project from the portfolio. |
||
120 | * |
||
121 | * @param \Illuminate\Http\Request $request Request data. |
||
122 | * @param string $slug Slug of project to remove. |
||
123 | * |
||
124 | * @return \Illuminate\Http\Response |
||
125 | */ |
||
126 | public function destroy(Request $request, $slug) |
||
142 | } |
||
143 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.