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 StoreFormRequest extends FormRequest |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Determine if the user is authorized to make this request. |
||
| 12 | * |
||
| 13 | * @return bool |
||
| 14 | */ |
||
| 15 | public function authorize() |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Get the validation rules that apply to the request. |
||
| 22 | * |
||
| 23 | * @return array |
||
| 24 | */ |
||
| 25 | View Code Duplication | public function rules() |
|
| 32 | |||
| 33 | /** |
||
| 34 | * Get custom attributes for validator errors. |
||
| 35 | * |
||
| 36 | * @return array |
||
| 37 | */ |
||
| 38 | public function attributes() |
||
| 42 | } |
||
| 43 |
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: