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 |
||
9 | class CreateRequest extends FormRequest |
||
10 | { |
||
11 | private $primaryRules = [ |
||
12 | 'title' => 'required|string', |
||
13 | 'testimony' => 'required|string', |
||
14 | ]; |
||
15 | |||
16 | /** |
||
17 | * Gets the rules for a ministry with a premium subscription. |
||
18 | * |
||
19 | * @return array an set of premium attributes |
||
20 | */ |
||
21 | private function getPremiumRules(): array |
||
28 | |||
29 | /** |
||
30 | * Gets the rules for a ministry with a premuim+ subscription. |
||
31 | * |
||
32 | * @return array |
||
33 | */ |
||
34 | private function getPremiumPlusRules(): array |
||
41 | |||
42 | /** |
||
43 | * Determine if the user is authorized to make this request. |
||
44 | * |
||
45 | * @return bool |
||
46 | */ |
||
47 | public function authorize() |
||
56 | |||
57 | /** |
||
58 | * Get the validation rules that apply to the request. |
||
59 | * |
||
60 | * @return array |
||
61 | */ |
||
62 | View Code Duplication | public function rules() |
|
74 | |||
75 | public function failedAuthorization() |
||
79 | |||
80 | /** |
||
81 | * Converts image string array to usable string in the validation. |
||
82 | * |
||
83 | * @return void |
||
84 | */ |
||
85 | public function prepareForValidation() |
||
93 | } |
||
94 |
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: