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 |
||
25 | class JqadmController extends AdminController |
||
26 | { |
||
27 | use AuthorizesRequests; |
||
28 | |||
29 | |||
30 | /** |
||
31 | * Returns the JS file content |
||
32 | * |
||
33 | * @return \Illuminate\Http\Response Response object containing the generated output |
||
34 | */ |
||
35 | public function fileAction() |
||
73 | |||
74 | |||
75 | /** |
||
76 | * Returns the HTML code for a copy of a resource object |
||
77 | * |
||
78 | * @return string Generated output |
||
79 | */ |
||
80 | View Code Duplication | public function copyAction() |
|
94 | |||
95 | |||
96 | /** |
||
97 | * Returns the HTML code for a new resource object |
||
98 | * |
||
99 | * @return string Generated output |
||
100 | */ |
||
101 | View Code Duplication | public function createAction() |
|
115 | |||
116 | |||
117 | /** |
||
118 | * Deletes the resource object or a list of resource objects |
||
119 | * |
||
120 | * @return string Generated output |
||
121 | */ |
||
122 | View Code Duplication | public function deleteAction() |
|
123 | { |
||
124 | if( config( 'shop.authorize', true ) ) { |
||
125 | $this->authorize( 'admin', [JqadmController::class, ['admin', 'editor']] ); |
||
126 | } |
||
127 | |||
128 | $cntl = $this->createAdmin(); |
||
129 | |||
130 | if( ( $html = $cntl->delete() ) == '' ) { |
||
131 | return $cntl->getView()->response(); |
||
132 | } |
||
133 | |||
134 | return $this->getHtml( $html ); |
||
135 | } |
||
136 | |||
137 | |||
138 | /** |
||
139 | * Exports the data for a resource object |
||
140 | * |
||
141 | * @return string Generated output |
||
142 | */ |
||
143 | View Code Duplication | public function exportAction() |
|
157 | |||
158 | |||
159 | /** |
||
160 | * Returns the HTML code for the requested resource object |
||
161 | * |
||
162 | * @return string Generated output |
||
163 | */ |
||
164 | View Code Duplication | public function getAction() |
|
178 | |||
179 | |||
180 | /** |
||
181 | * Saves a new resource object |
||
182 | * |
||
183 | * @return string Generated output |
||
184 | */ |
||
185 | View Code Duplication | public function saveAction() |
|
186 | { |
||
187 | if( config( 'shop.authorize', true ) ) { |
||
188 | $this->authorize( 'admin', [JqadmController::class, ['admin', 'editor']] ); |
||
189 | } |
||
190 | |||
191 | $cntl = $this->createAdmin(); |
||
192 | |||
193 | if( ( $html = $cntl->save() ) == '' ) { |
||
194 | return $cntl->getView()->response(); |
||
195 | } |
||
196 | |||
197 | return $this->getHtml( $html ); |
||
198 | } |
||
199 | |||
200 | |||
201 | /** |
||
202 | * Returns the HTML code for a list of resource objects |
||
203 | * |
||
204 | * @return string Generated output |
||
205 | */ |
||
206 | View Code Duplication | public function searchAction() |
|
220 | |||
221 | |||
222 | /** |
||
223 | * Returns the resource controller |
||
224 | * |
||
225 | * @return \Aimeos\Admin\JQAdm\Iface JQAdm client |
||
226 | */ |
||
227 | protected function createAdmin() |
||
228 | { |
||
229 | $site = Route::input( 'site', Input::get( 'site', 'default' ) ); |
||
230 | $lang = Input::get( 'lang', config( 'app.locale', 'en' ) ); |
||
231 | $resource = Route::input( 'resource' ); |
||
232 | |||
233 | $aimeos = app( '\Aimeos\Shop\Base\Aimeos' )->get(); |
||
234 | $paths = $aimeos->getCustomPaths( 'admin/jqadm/templates' ); |
||
235 | |||
236 | $context = app( '\Aimeos\Shop\Base\Context' )->get( false, 'backend' ); |
||
237 | $context->setI18n( app('\Aimeos\Shop\Base\I18n')->get( array( $lang, 'en' ) ) ); |
||
238 | $context->setLocale( app('\Aimeos\Shop\Base\Locale')->getBackend( $context, $site ) ); |
||
239 | |||
240 | $view = app( '\Aimeos\Shop\Base\View' )->create( $context, $paths, $lang ); |
||
241 | |||
242 | $view->aimeosType = 'Laravel'; |
||
243 | $view->aimeosVersion = app( '\Aimeos\Shop\Base\Aimeos' )->getVersion(); |
||
244 | $view->aimeosExtensions = implode( ',', $aimeos->getExtensions() ); |
||
245 | |||
246 | $context->setView( $view ); |
||
247 | |||
248 | return \Aimeos\Admin\JQAdm::create( $context, $aimeos, $resource ); |
||
249 | } |
||
250 | |||
251 | |||
252 | /** |
||
253 | * Returns the generated HTML code |
||
254 | * |
||
255 | * @param string $content Content from admin client |
||
256 | * @return \Illuminate\Contracts\View\View View for rendering the output |
||
257 | */ |
||
258 | protected function getHtml( $content ) |
||
263 | } |
||
264 |
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: