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 |
||
14 | class CountryOptions |
||
15 | { |
||
16 | /** |
||
17 | * the action being performed on the current step |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | public $action = ''; |
||
22 | |||
23 | /** |
||
24 | * @var EEM_Answer |
||
25 | */ |
||
26 | public $answer_model; |
||
27 | |||
28 | /** |
||
29 | * @var EEM_Country |
||
30 | */ |
||
31 | public $country_model; |
||
32 | |||
33 | /** |
||
34 | * @var [][] |
||
35 | */ |
||
36 | private $country_options = []; |
||
37 | |||
38 | |||
39 | /** |
||
40 | * CountryOptions constructor. |
||
41 | * |
||
42 | * @param string $action |
||
43 | * @param EEM_Answer $answer_model |
||
44 | * @param EEM_Country $country_model |
||
45 | */ |
||
46 | public function __construct(string $action, EEM_Answer $answer_model, EEM_Country $country_model) |
||
58 | |||
59 | |||
60 | /** |
||
61 | * Gets the list of countries for the form input |
||
62 | * |
||
63 | * @param array|null $countries_list deprecated prop from an old hook |
||
64 | * @param EE_Question|null $question |
||
65 | * @param EE_Registration|null $registration |
||
66 | * @param EE_Answer|null $answer deprecated prop from an old hook |
||
67 | * @return array 2d keys are country IDs, values are their names |
||
68 | * @throws EE_Error |
||
69 | * @throws ReflectionException |
||
70 | */ |
||
71 | View Code Duplication | public function forLegacyFormInput( |
|
82 | |||
83 | |||
84 | /** |
||
85 | * @param EE_Question|null $question |
||
86 | * @param EE_Registration|null $registration |
||
87 | * @throws EE_Error |
||
88 | * @throws ReflectionException |
||
89 | */ |
||
90 | private function generateLegacyCountryOptions(EE_Question $question = null, EE_Registration $registration = null) |
||
121 | } |
||
122 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.