Completed
Branch FET/reg-form-builder/extract-a... (6e8a58)
by
unknown
35:36 queued 25:38
created

StateOptions   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 96
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 16
loc 96
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A forLegacyFormInput() 11 11 2
A generateLegacyStateOptions() 5 25 5

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace EventEspresso\core\domain\services\registration\form\v1;
4
5
use EE_Answer;
6
use EE_Error;
7
use EE_Question;
8
use EE_Registration;
9
use EE_State;
10
use EEM_State;
11
use ReflectionException;
12
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)
40
    {
41
        $this->action      = $action;
42
        $this->state_model = $state_model;
43
        add_filter(
44
            'FHEE__EE_Question__generate_form_input__state_options',
45
            [$this, 'forLegacyFormInput'],
46
            10,
47
            4
48
        );
49
    }
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(
64
        array $states_list = null,
65
        EE_Question $question = null,
66
        EE_Registration $registration = null,
67
        EE_Answer $answer = null
68
    ): array {
69
        if (! isset($this->state_options[ $this->action ])) {
70
            $this->generateLegacyStateOptions($question, $registration, $answer);
71
        }
72
        return $this->state_options[ $this->action ];
73
    }
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(
84
        EE_Question $question = null,
85
        EE_Registration $registration = null,
86
        EE_Answer $answer = null
87
    ) {
88
        $state_options = ['' => ['' => '']];
89
        $states        = $this->action === 'process_reg_step'
90
            ? $this->state_model->get_all_states()
91
            : $this->state_model->get_all_active_states();
92
        if (! empty($states)) {
93 View Code Duplication
            foreach ($states as $state) {
0 ignored issues
show
Bug introduced by
The expression $states of type array<integer,object<EE_Base_Class>>|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. 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:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
94
                if ($state instanceof EE_State) {
95
                    $state_options[ $state->country()->name() ][ $state->ID() ] = $state->name();
96
                }
97
            }
98
        }
99
        $this->state_options[ $this->action ] = apply_filters(
100
            'FHEE__EE_SPCO_Reg_Step_Attendee_Information___generate_question_input__state_options',
101
            $state_options,
102
            $this,
103
            $registration,
104
            $question,
105
            $answer
106
        );
107
    }
108
}
109