Conditions | 33 |
Paths | > 20000 |
Total Lines | 76 |
Code Lines | 40 |
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 |
||
130 | public static function sanitize( $value ) { |
||
131 | |||
132 | if ( ! is_array( $value ) ) { |
||
133 | return array(); |
||
134 | } |
||
135 | |||
136 | // Escape the font-family. |
||
137 | if ( isset( $value['font-family'] ) ) { |
||
138 | $value['font-family'] = esc_attr( $value['font-family'] ); |
||
139 | } |
||
140 | |||
141 | // Get variant from font-weight and font-style. |
||
142 | if ( ! isset( $value['variant'] ) && isset( $value['font-weight'] ) ) { |
||
143 | $value['variant'] = $value['font-weight']; |
||
144 | if ( isset( $value['font-style'] ) && 'italic' === $value['font-style'] ) { |
||
145 | $value['variant'] = ( '400' !== $value['font-weight'] || 400 !== $value['font-weight'] ) ? $value['variant'] . 'italic' : 'italic'; |
||
146 | } |
||
147 | } |
||
148 | |||
149 | // Use 'regular' instead of 400 for font-variant. |
||
150 | if ( isset( $value['variant'] ) ) { |
||
151 | $value['variant'] = ( 400 === $value['variant'] || '400' === $value['variant'] ) ? 'regular' : $value['variant']; |
||
152 | } |
||
153 | |||
154 | // Get font-weight from variant. |
||
155 | if ( isset( $value['variant'] ) ) { |
||
156 | $value['font-weight'] = filter_var( $value['variant'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); |
||
157 | $value['font-weight'] = absint( $value['font-weight'] ); |
||
158 | |||
159 | if ( 'regular' === $value['variant'] || 'italic' === $value['variant'] ) { |
||
160 | $value['font-weight'] = 400; |
||
161 | } |
||
162 | } |
||
163 | |||
164 | // Get font-style from variant. |
||
165 | if ( ! isset( $value['font-style'] ) && isset( $value['variant'] ) ) { |
||
166 | $value['font-style'] = ( false === strpos( $value['variant'], 'italic' ) ) ? 'normal' : 'italic'; |
||
167 | } |
||
168 | |||
169 | // Make sure the saved value is "subsets" (plural) and not "subset". |
||
170 | // This is for compatibility with older versions. |
||
171 | if ( isset( $value['subset'] ) ) { |
||
172 | if ( ! empty( $value['subset'] ) && ! isset( $value['subsets'] ) || empty( $value['subset'] ) ) { |
||
173 | $value['subsets'] = $value['subset']; |
||
174 | } |
||
175 | unset( $value['subset'] ); |
||
176 | } |
||
177 | |||
178 | // Make sure we're using a valid subset. |
||
179 | if ( isset( $value['subsets'] ) ) { |
||
180 | $valid_subsets = Kirki_Fonts::get_google_font_subsets(); |
||
181 | $subsets_ok = array(); |
||
182 | if ( is_array( $value['subsets'] ) ) { |
||
183 | foreach ( $value['subsets'] as $subset ) { |
||
184 | if ( array_key_exists( $subset, $valid_subsets ) ) { |
||
185 | $subsets_ok[] = $subset; |
||
186 | } |
||
187 | } |
||
188 | $value['subsets'] = $subsets_ok; |
||
189 | } |
||
190 | } |
||
191 | |||
192 | foreach ( $value as $key => $subvalue ) { |
||
193 | |||
194 | if ( in_array( $key, array( 'font-size', 'letter-spacing', 'word-spacing', 'line-height' ), true ) ) { |
||
195 | $value[ $key ] = Kirki_Sanitize_Values::css_dimension( $value[ $key ] ); |
||
196 | } elseif ( 'text-align' === $key && ! in_array( $value['text-align'], array( 'inherit', 'left', 'center', 'right', 'justify' ), true ) ) { |
||
197 | $value['text-align'] = 'inherit'; |
||
198 | } elseif ( 'text-transform' === $key && ! in_array( $value['text-transform'], array( 'none', 'capitalize', 'uppercase', 'lowercase', 'initial', 'inherit' ), true ) ) { |
||
199 | $value['text-transform'] = 'none'; |
||
200 | } elseif ( 'color' === $key ) { |
||
201 | $value['color'] = ariColor::newColor( $value['color'] )->toCSS( 'hex' ); |
||
202 | } |
||
203 | } |
||
204 | return $value; |
||
205 | } |
||
206 | |||
232 |