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 |
||
12 | class ThreadsController extends Controller |
||
13 | { |
||
14 | /** |
||
15 | * ThreadsController constructor. |
||
16 | */ |
||
17 | public function __construct() |
||
24 | |||
25 | /** |
||
26 | * Display a listing of the resource. |
||
27 | * |
||
28 | * @return \Illuminate\Http\Response |
||
29 | */ |
||
30 | public function index($slug=null) |
||
61 | |||
62 | /** |
||
63 | * Show the form for creating a new resource. |
||
64 | * |
||
65 | * @return \Illuminate\Http\Response |
||
66 | */ |
||
67 | public function create() |
||
74 | |||
75 | /** |
||
76 | * Store a newly created resource in storage. |
||
77 | * |
||
78 | * @param \Illuminate\Http\Request $request |
||
79 | * @return \Illuminate\Http\Response |
||
80 | */ |
||
81 | public function store(Request $request) |
||
106 | |||
107 | /** |
||
108 | * Display the specified resource. |
||
109 | * |
||
110 | * @param \App\Thread $thread |
||
111 | * @return \Illuminate\Http\Response |
||
112 | */ |
||
113 | public function show($channelId, Thread $thread) |
||
124 | |||
125 | /** |
||
126 | * Show the form for editing the specified resource. |
||
127 | * |
||
128 | * @param \App\Thread $thread |
||
129 | * @return \Illuminate\Http\Response |
||
130 | */ |
||
131 | public function edit(Thread $thread) |
||
135 | |||
136 | /** |
||
137 | * Update the specified resource in storage. |
||
138 | * |
||
139 | * @param \Illuminate\Http\Request $request |
||
140 | * @param \App\Thread $thread |
||
141 | * @return \Illuminate\Http\Response |
||
142 | */ |
||
143 | public function update(Channel $channel, Thread $thread, Request $request) |
||
158 | |||
159 | /** |
||
160 | * Remove the specified resource from storage. |
||
161 | * |
||
162 | * @param \App\Thread $thread |
||
163 | * @return \Illuminate\Http\Response |
||
164 | */ |
||
165 | public function destroy(Request $request, Channel $channel, Thread $thread) |
||
174 | |||
175 | private function slugify($string){ |
||
185 | |||
186 | |||
187 | private function generate_string($strength = 4) { |
||
197 | } |
||
198 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: