Completed
Branch FET/event-question-group-refac... (ba65fd)
by
unknown
55:24 queued 46:50
created

EE_State_Select_Input::valueFieldName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
use EventEspresso\core\exceptions\InvalidDataTypeException;
4
use EventEspresso\core\exceptions\InvalidInterfaceException;
5
6
/**
7
 * EE_State_Select_Input
8
 *
9
 * @package     Event Espresso
10
 * @subpackage
11
 * @author      Mike Nelson
12
 */
13
class EE_State_Select_Input extends EE_Select_Input
14
{
15
    /**
16
     * @var string the name of the EE_State field to use for option values in the HTML form input.
17
     */
18
    protected $valueFieldName;
19
20
    /**
21
     * @param EE_State[]|array|null $state_options. If a flat array of string is provided,
0 ignored issues
show
Documentation introduced by
There is no parameter named $state_options.. Did you maybe mean $state_options?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
22
     * $input_settings['value_field_name'] is ignored. If an array of states is passed, that field will be used for
23
     * the keys (which will become the option values). If null or empty is passed, all active states will be used,
24
     * and $input_settings['value_field_name'] will again be used.     *
25
     * @param array $input_settings same as parent, but also {
26
     *   @type string $value_field_name the name of the field to use
27
     *   for the HTML option values, ie, `STA_ID`, `STA_abbrev`, or `STA_name`.
28
     * }
29
     * @throws EE_Error
30
     * @throws InvalidArgumentException
31
     * @throws InvalidDataTypeException
32
     * @throws InvalidInterfaceException
33
     * @throws ReflectionException
34
     */
35
    public function __construct($state_options, $input_settings = array())
36
    {
37
        if (isset($input_settings['value_field_name'])) {
38
            $this->valueFieldName = $input_settings['value_field_name'];
39
            if (! EEM_State::instance()->has_field((string) $this->valueFieldName())) {
40
                throw new InvalidArgumentException(
41
                    sprintf(
42
                        esc_html__('An invalid state field "%1$s" was specified for the state input\'s option values.', 'event_espresso'),
43
                        $this->valueFieldName()
44
                    )
45
                );
46
            }
47
        } else {
48
            $this->valueFieldName = 'STA_ID';
49
        }
50
        $state_options = apply_filters(
51
            'FHEE__EE_State_Select_Input____construct__state_options',
52
            $this->get_state_answer_options($state_options),
53
            $this
54
        );
55
        $input_settings['html_class'] = isset($input_settings['html_class'])
56
            ? $input_settings['html_class'] . ' ee-state-select-js'
57
            : 'ee-state-select-js';
58
        parent::__construct($state_options, $input_settings);
59
    }
60
61
    /**
62
     * Returns the name of the state field used for the HTML option values.
63
     * @since $VID:$
64
     * @return string
65
     */
66
    public function valueFieldName()
67
    {
68
        return $this->valueFieldName;
69
    }
70
71
72
    /**
73
     * get_state_answer_options
74
     *
75
     * @param array $state_options
76
     * @return array
77
     * @throws EE_Error
78
     * @throws InvalidArgumentException
79
     * @throws ReflectionException
80
     * @throws InvalidDataTypeException
81
     * @throws InvalidInterfaceException
82
     */
83
    public function get_state_answer_options($state_options = null)
84
    {
85
        // if passed something that is NOT an array
86
        if (! is_array($state_options) || empty($state_options)) {
87
            // get possibly cached list of states
88
            $states = EEM_State::instance()->get_all_active_states();
89
        }
90
        if (is_array($state_options) && reset($state_options) instanceof EE_State) {
91
            $states = $state_options;
92
            $state_options = array();
93
        }
94
        if (! empty($states)) {
95
            // set the default
96
            $state_options[''][''] = '';
97 View Code Duplication
            foreach ($states as $state) {
0 ignored issues
show
Bug introduced by
The expression $states of type boolean|array 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...
98
                if ($state instanceof EE_State) {
99
                    $state_options[ $state->country()->name() ][ $state->get($this->valueFieldName()) ] = $state->name();
100
                }
101
            }
102
        }
103
        return $state_options;
104
    }
105
}
106