Conditions | 30 |
Paths | 74 |
Total Lines | 61 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
134 | public static function sanitize( $value ) { |
||
135 | if ( ! is_array( $value ) ) { |
||
136 | return array(); |
||
137 | } |
||
138 | |||
139 | foreach ( $value as $key => $val ) { |
||
140 | switch ( $key ) { |
||
141 | case 'font-family': |
||
142 | $value['font-family'] = sanitize_text_field( $val ); |
||
143 | break; |
||
144 | case 'font-weight': |
||
145 | if ( isset( $value['variant'] ) ) { |
||
146 | break; |
||
147 | } |
||
148 | $value['variant'] = $val; |
||
149 | if ( isset( $value['font-style'] ) && 'italic' === $value['font-style'] ) { |
||
150 | $value['variant'] = ( '400' !== $val || 400 !== $val ) ? $value['variant'] . 'italic' : 'italic'; |
||
151 | } |
||
152 | break; |
||
153 | case 'variant': |
||
154 | |||
155 | // Use 'regular' instead of 400 for font-variant. |
||
156 | $value['variant'] = ( 400 === $val || '400' === $val ) ? 'regular' : $val; |
||
157 | |||
158 | // Get font-weight from variant. |
||
159 | $value['font-weight'] = filter_var( $value['variant'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); |
||
160 | $value['font-weight'] = ( 'regular' === $value['variant'] || 'italic' === $value['variant'] ) ? 400 : absint( $value['font-weight'] ); |
||
161 | |||
162 | // Get font-style from variant. |
||
163 | if ( ! isset( $value['font-style'] ) ) { |
||
164 | $value['font-style'] = ( false === strpos( $value['variant'], 'italic' ) ) ? 'normal' : 'italic'; |
||
165 | } |
||
166 | break; |
||
167 | case 'font-size': |
||
168 | case 'letter-spacing': |
||
169 | case 'word-spacing': |
||
170 | case 'line-height': |
||
171 | $value[ $key ] = '' === trim( $value[ $key ] ) ? '' : sanitize_text_field( $val ); |
||
172 | break; |
||
173 | case 'text-align': |
||
174 | if ( ! in_array( $val, array( '', 'inherit', 'left', 'center', 'right', 'justify' ), true ) ) { |
||
175 | $value['text-align'] = ''; |
||
176 | } |
||
177 | break; |
||
178 | case 'text-transform': |
||
179 | if ( ! in_array( $val, array( '', 'none', 'capitalize', 'uppercase', 'lowercase', 'initial', 'inherit' ), true ) ) { |
||
180 | $value['text-transform'] = ''; |
||
181 | } |
||
182 | break; |
||
183 | case 'text-decoration': |
||
184 | if ( ! in_array( $val, array( '', 'none', 'underline', 'overline', 'line-through', 'initial', 'inherit' ), true ) ) { |
||
185 | $value['text-transform'] = ''; |
||
186 | } |
||
187 | break; |
||
188 | case 'color': |
||
189 | $value['color'] = '' === $value['color'] ? '' : ariColor::newColor( $val )->toCSS( 'hex' ); |
||
190 | break; |
||
191 | } |
||
192 | } |
||
193 | |||
194 | return $value; |
||
195 | } |
||
218 |