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

CountryOptions   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 108
Duplicated Lines 10.19 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 11
loc 108
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A forLegacyFormInput() 11 11 2
B generateLegacyCountryOptions() 0 31 7

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_Country;
7
use EE_Error;
8
use EE_Question;
9
use EE_Registration;
10
use EEM_Answer;
11
use EEM_Country;
12
use ReflectionException;
13
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)
47
    {
48
        $this->action        = $action;
49
        $this->answer_model  = $answer_model;
50
        $this->country_model = $country_model;
51
        add_filter(
52
            'FHEE__EE_Question__generate_form_input__country_options',
53
            [$this, 'forLegacyFormInput'],
54
            10,
55
            4
56
        );
57
    }
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(
72
        array $countries_list = null,
73
        EE_Question $question = null,
74
        EE_Registration $registration = null,
75
        EE_Answer $answer = null
76
    ): array {
77
        if (! isset($this->country_options[ $this->action ])) {
78
            $this->generateLegacyCountryOptions($question, $registration);
79
        }
80
        return $this->country_options[ $this->action ];
81
    }
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)
91
    {
92
        // get possibly cached list of countries
93
        $countries = $this->action === 'process_reg_step'
94
            ? $this->country_model->get_all_countries()
95
            : $this->country_model->get_all_active_countries();
96
        // start with an empty option
97
        $country_options = ['' => ''];
98
        if (! empty($countries)) {
99
            foreach ($countries as $country) {
0 ignored issues
show
Bug introduced by
The expression $countries 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...
100
                if ($country instanceof EE_Country) {
101
                    $country_options[ $country->ID() ] = $country->name();
102
                }
103
            }
104
        }
105
        if ($question instanceof EE_Question && $registration instanceof EE_Registration) {
106
            $answer = $this->answer_model->get_one(
107
                [['QST_ID' => $question->ID(), 'REG_ID' => $registration->ID()]]
108
            );
109
        } else {
110
            $answer = EE_Answer::new_instance();
111
        }
112
        $this->country_options[ $this->action ] = apply_filters(
113
            'FHEE__EE_SPCO_Reg_Step_Attendee_Information___generate_question_input__country_options',
114
            $country_options,
115
            $this,
116
            $registration,
117
            $question,
118
            $answer
119
        );
120
    }
121
}
122