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 |
||
| 13 | class StateOptions |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * the action being performed on the current step |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | public $action = ''; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var EEM_State |
||
| 24 | */ |
||
| 25 | public $state_model; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var [][] |
||
| 29 | */ |
||
| 30 | private $state_options = []; |
||
| 31 | |||
| 32 | |||
| 33 | /** |
||
| 34 | * CountryOptions constructor. |
||
| 35 | * |
||
| 36 | * @param string $action |
||
| 37 | * @param EEM_State $state_model |
||
| 38 | */ |
||
| 39 | public function __construct(string $action, EEM_State $state_model) |
||
| 50 | |||
| 51 | |||
| 52 | /** |
||
| 53 | * Gets the list of states for the form input |
||
| 54 | * |
||
| 55 | * @param array|null $states_list deprecated prop from an old hook |
||
| 56 | * @param EE_Question|null $question |
||
| 57 | * @param EE_Registration|null $registration |
||
| 58 | * @param EE_Answer|null $answer |
||
| 59 | * @return array 2d keys are state IDs, values are their names |
||
| 60 | * @throws EE_Error |
||
| 61 | * @throws ReflectionException |
||
| 62 | */ |
||
| 63 | View Code Duplication | public function forLegacyFormInput( |
|
| 74 | |||
| 75 | |||
| 76 | /** |
||
| 77 | * @param EE_Question|null $question |
||
| 78 | * @param EE_Registration|null $registration |
||
| 79 | * @param EE_Answer|null $answer |
||
| 80 | * @throws EE_Error |
||
| 81 | * @throws ReflectionException |
||
| 82 | */ |
||
| 83 | private function generateLegacyStateOptions( |
||
| 108 | } |
||
| 109 |
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.