Conditions | 26 |
Paths | 444 |
Total Lines | 107 |
Code Lines | 71 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
27 | public static function explode( $field ) { |
||
28 | $l10n = self::l10n( $field['kirki_config'] ); |
||
29 | $choices = self::background_choices(); |
||
30 | |||
31 | // Early exit if this is not a background field. |
||
32 | if ( 'background' !== $field['type'] ) { |
||
33 | return; |
||
34 | } |
||
35 | |||
36 | // No need to proceed any further if no defaults have been set. |
||
37 | // We build the array of fields based on what default values have been defined. |
||
38 | if ( ! isset( $field['default'] ) || ! is_array( $field['default'] ) ) { |
||
39 | return; |
||
40 | } |
||
41 | |||
42 | $fields = array(); |
||
43 | $i = 0; |
||
44 | foreach ( $field['default'] as $key => $value ) { |
||
45 | |||
46 | // No need to process the opacity, it is factored in the color control. |
||
47 | if ( 'opacity' === $key ) { |
||
48 | continue; |
||
49 | } |
||
50 | |||
51 | $key = esc_attr( $key ); |
||
52 | $setting = $key; |
||
53 | $tooltip = $field['tooltip']; |
||
54 | $description = isset( $l10n[ 'background-' . $key ] ) ? $l10n[ 'background-' . $key ] : ''; |
||
55 | $output_property = 'background-' . $key; |
||
56 | $label = ( 0 === $i ) ? $field['label'] : ''; |
||
57 | $type = 'select'; |
||
58 | $sanitize_callback = 'esc_attr'; |
||
59 | |||
60 | switch ( $key ) { |
||
61 | case 'color': |
||
62 | // Use 'color-alpha' instead of 'color' if default is an rgba value or if 'opacity' is set. |
||
63 | $type = ( false !== strpos( $field['default']['color'], 'rgba' ) ) ? 'color-alpha' : 'color'; |
||
64 | $type = ( isset( $field['default']['opacity'] ) ) ? 'color-alpha' : $type; |
||
65 | if ( isset( $field['default']['opacity'] ) && false === strpos( $value, 'rgb' ) ) { |
||
66 | $value = Kirki_Color::get_rgba( $value, $field['default']['opacity'] ); |
||
67 | } |
||
68 | $sanitize_callback = array( 'Kirki_Sanitize_Values', 'color' ); |
||
69 | break; |
||
70 | case 'image': |
||
71 | $type = 'image'; |
||
72 | $sanitize_callback = 'esc_url_raw'; |
||
73 | break; |
||
74 | case 'attach': |
||
75 | // Small hack so that background attachments properly work. |
||
76 | $output_property = 'background-attachment'; |
||
77 | $description = $l10n['background-attachment']; |
||
78 | break; |
||
79 | default: |
||
80 | $tooltip = ''; |
||
81 | break; |
||
82 | } |
||
83 | |||
84 | // If we're using options & option_name is set, then we need to modify the setting. |
||
85 | if ( ( isset( $field['option_type'] ) && 'option' === $field['option_type'] && isset( $field['option_name'] ) ) && ! empty( $field['option_name'] ) ) { |
||
86 | $property_setting = str_replace( ']', '', str_replace( $field['option_name'] . '[', '', $field['settings'] ) ); |
||
87 | $property_setting = esc_attr( $field['option_name'] ) . '[' . esc_attr( $property_setting ) . '_' . $setting . ']'; |
||
88 | } else { |
||
89 | $property_setting = esc_attr( $field['settings'] ) . '_' . $setting; |
||
90 | } |
||
91 | |||
92 | // Build the field's output element. |
||
93 | $output_element = $field['output']; |
||
94 | if ( ! empty( $field['output'] ) ) { |
||
95 | if ( is_array( $field['output'] ) ) { |
||
96 | if ( isset( $field['output']['element'] ) ) { |
||
97 | $output_element = $field['output']['element']; |
||
98 | } elseif ( isset( $field['output'][0] ) && isset( $field['output'][0]['element'] ) ) { |
||
99 | $output_element = $field['output'][0]['element']; |
||
100 | } |
||
101 | } |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Build the field. |
||
106 | * We're merging with the original field here, so any extra properties are inherited. |
||
107 | */ |
||
108 | $fields[ $property_setting ] = array_merge( $field, array( |
||
109 | 'type' => $type, |
||
110 | 'label' => $label, |
||
111 | 'settings' => $property_setting, |
||
112 | 'tooltip' => $tooltip, |
||
113 | 'section' => $field['section'], |
||
114 | 'priority' => $field['priority'], |
||
115 | 'required' => $field['required'], |
||
116 | 'description' => $description, |
||
117 | 'default' => $value, |
||
118 | 'id' => sanitize_key( str_replace( '[', '-', str_replace( ']', '', $property_setting ) ) ), |
||
119 | 'choices' => isset( $choices[ $key ] ) ? $choices[ $key ] : array(), |
||
120 | 'sanitize_callback' => $sanitize_callback, |
||
121 | 'output' => ( ! empty( $field['output'] ) ) ? array( |
||
122 | array( |
||
123 | 'element' => $output_element, |
||
124 | 'property' => $output_property, |
||
125 | ), |
||
126 | ) : array(), |
||
127 | ) ); |
||
128 | $i++; |
||
129 | } |
||
130 | |||
131 | return $fields; |
||
132 | |||
133 | } |
||
134 | |||
239 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.