Completed
Push — develop ( 5e223c...186db0 )
by Aristeides
02:51
created

Kirki_Output_Field_Dimensions::process_output()   D

Complexity

Conditions 10
Paths 14

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 22
nc 14
nop 2
dl 0
loc 33
rs 4.8196
c 0
b 0
f 0

How to fix   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 dimensions fields.
4
 *
5
 * @package     Kirki
6
 * @subpackage  Controls
7
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
8
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
9
 * @since       2.2.0
10
 */
11
12
/**
13
 * Output overrides.
14
 */
15
class Kirki_Output_Field_Dimensions 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
26
		$output = wp_parse_args( $output, array(
27
			'element'     => '',
28
			'property'    => '',
29
			'media_query' => 'global',
30
			'prefix'      => '',
31
			'suffix'      => '',
32
		) );
33
34
		if ( is_array( $value ) ) {
35
			foreach ( $value as $key => $sub_value ) {
36
37
				$property = ( empty( $output['property'] ) ) ? $key : $output['property'] . '-' . $key;
38
				if ( isset( $output['choice'] ) && $output['property'] ) {
39
					if ( $key === $output['choice'] ) {
40
						$property = $output['property'];
41
					} else {
42
						continue;
43
					}
44
				}
45
				if ( false !== strpos( $output['property'], '%%' ) ) {
46
					$property = str_replace( '%%', $key, $output['property'] );
47
				}
48
				$this->styles[ $output['media_query'] ][ $output['element'] ][ $property ] = $output['prefix'] . $this->process_property_value( $property, $value[ $key ] ) . $output['suffix'];
49
			}
50
		} elseif ( isset( $output['choice'] ) ) {
51
			if ( false !== strpos( $output['property'], '%%' ) ) {
52
				$output['property'] = str_replace( '%%', $key, $output['property'] );
0 ignored issues
show
Bug introduced by
The variable $key seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
53
			}
54
			$this->styles[ $output['media_query'] ][ $output['element'] ][ $output['property'] ] = $output['prefix'] . $this->process_property_value( $output['property'], $value ) . $output['suffix'];
55
		}
56
	}
57
}
58