Kirki_Output_Field_Typography::process_output()   F
last analyzed

Complexity

Conditions 22
Paths 896

Size

Total Lines 69
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 22
eloc 43
nc 896
nop 2
dl 0
loc 69
rs 0.1444
c 2
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * Handles CSS output for typography fields.
4
 *
5
 * @package     Kirki
6
 * @subpackage  Controls
7
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
8
 * @license    https://opensource.org/licenses/MIT
9
 * @since       2.2.0
10
 */
11
12
/**
13
 * Output overrides.
14
 */
15
class Kirki_Output_Field_Typography extends Kirki_Output {
16
17
	/**
18
	 * Processes a single item from the `output` array.
19
	 *
20
	 * @access protected
21
	 * @param array $output The `output` item.
22
	 * @param array $value  The field's value.
23
	 */
24
	protected function process_output( $output, $value ) {
25
		$output['media_query'] = ( isset( $output['media_query'] ) ) ? $output['media_query'] : 'global';
26
		$output['element']     = ( isset( $output['element'] ) ) ? $output['element'] : 'body';
27
		$output['prefix']      = ( isset( $output['prefix'] ) ) ? $output['prefix'] : '';
28
		$output['suffix']      = ( isset( $output['suffix'] ) ) ? $output['suffix'] : '';
29
30
		$value = Kirki_Field_Typography::sanitize( $value );
31
32
		$properties = array(
33
			'font-family',
34
			'font-size',
35
			'variant',
36
			'font-weight',
37
			'font-style',
38
			'letter-spacing',
39
			'word-spacing',
40
			'line-height',
41
			'text-align',
42
			'text-transform',
43
			'text-decoration',
44
			'color',
45
		);
46
47
		foreach ( $properties as $property ) {
48
49
			// Early exit if the value is not in the defaults.
50
			if ( ! isset( $this->field['default'][ $property ] ) ) {
51
				continue;
52
			}
53
54
			// Early exit if the value is not saved in the values.
55
			if ( ! isset( $value[ $property ] ) || ! $value[ $property ] ) {
56
				continue;
57
			}
58
59
			// Early exit if we use "choice" but not for this property.
60
			if ( isset( $output['choice'] ) && $output['choice'] !== $property ) {
61
				continue;
62
			}
63
64
			// Take care of variants.
65
			if ( 'variant' === $property && isset( $value['variant'] ) && ! empty( $value['variant'] ) ) {
66
67
				// Get the font_weight.
68
				$font_weight = str_replace( 'italic', '', $value['variant'] );
69
				$font_weight = ( in_array( $font_weight, array( '', 'regular' ), true ) ) ? '400' : $font_weight;
70
71
				// Is this italic?
72
				$is_italic = ( false !== strpos( $value['variant'], 'italic' ) );
73
				$this->styles[ $output['media_query'] ][ $output['element'] ]['font-weight'] = $font_weight;
74
				if ( $is_italic ) {
75
					$this->styles[ $output['media_query'] ][ $output['element'] ]['font-style'] = 'italic';
76
				}
77
				continue;
78
			}
79
80
			$property_value = $this->process_property_value( $property, $value[ $property ] );
81
			if ( 'font-family' === $property ) {
82
				$value['font-backup'] = ( isset( $value['font-backup'] ) ) ? $value['font-backup'] : '';
83
				$property_value       = $this->process_property_value(
84
					$property, array(
0 ignored issues
show
Coding Style introduced by aristath
For multi-line function calls, each argument should be on a separate line.

For a function calls that spawns multiple lines, the coding style suggests to split arguments to separate lines like this:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
);
Loading history...
85
						$value['font-family'],
86
						$value['font-backup'],
87
					)
88
				);
89
			}
90
			$property       = ( isset( $output['choice'] ) && isset( $output['property'] ) ) ? $output['property'] : $property;
91
			$property_value = ( is_array( $property_value ) && isset( $property_value[0] ) ) ? $property_value[0] : $property_value;
92
			$this->styles[ $output['media_query'] ][ $output['element'] ][ $property ] = $output['prefix'] . $property_value . $output['suffix'];
93
		}
94
	}
95
}
96