Completed
Pull Request — staging (#840)
by
unknown
18:46
created

FieldBuilder::get_field_classes()   F

Complexity

Conditions 12
Paths 774

Size

Total Lines 64

Duplication

Lines 35
Ratio 54.69 %

Importance

Changes 0
Metric Value
cc 12
nc 774
nop 1
dl 35
loc 64
rs 3.2765
c 0
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
 * YIKES Inc. Easy Forms.
4
 *
5
 * @package   YIKES\EasyForms
6
 * @author    Freddie Mixell
7
 * @license   GPL2
8
 */
9
10
namespace YIKES\EasyForms\Form;
11
12
trait FieldBuilder {
13
    protected function get_field_classes( $field ) {
14
        $field_classes = [];
15
        $label_classes = [];
16
17
        $label_classes[] = $field['merge'] . '-label';
18
19
        if( $field['additional-classes'] != '' ) {
20
21
            $field_classes = explode( ' ' , $field['additional-classes'] );
22
23 View Code Duplication
            if( in_array( 'field-left-half' , $field_classes ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
                $$label_classes[] = 'field-left-half';
25
                $key = array_search( 'field-left-half' , $field_classes );
26
                unset( $field_classes[$key] );
27
            } // input half right
28 View Code Duplication
            if( in_array( 'field-right-half' , $field_classes ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
                $$label_classes[] = 'field-right-half';
30
                $key = array_search( 'field-right-half' , $field_classes );
31
                unset( $field_classes[$key] );
32
            } // input thirds (1/3 width, floated left)
33 View Code Duplication
            if( in_array( 'field-third' , $field_classes ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
                $$label_classes[] = 'field-third';
35
                $key = array_search( 'field-third' , $field_classes );
36
                unset( $field_classes[$key] );
37
            } // 2 column radio
38 View Code Duplication
            if( in_array( 'option-2-col' , $field_classes ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
                $$label_classes[] = 'option-2-col';
40
                $key = array_search( 'option-2-col' , $field_classes );
41
                unset( $field_classes[$key] );
42
            } // 3 column radio
43 View Code Duplication
            if( in_array( 'option-3-col' , $field_classes ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
                $$label_classes[] = 'option-3-col';
45
                $key = array_search( 'option-3-col' , $field_classes );
46
                unset( $field_classes[$key] );
47
            } // 4 column radio
48 View Code Duplication
            if( in_array( 'option-4-col' , $field_classes ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
                $$label_classes[] = 'option-4-col';
50
                $key = array_search( 'option-4-col' , $field_classes );
51
                unset( $field_classes[$key] );
52
            } // inline radio & checkboxes etc
53 View Code Duplication
            if( in_array( 'option-inline' , $field_classes ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
                $$label_classes[] = 'option-inline';
55
                $key = array_search( 'option-inline' , $field_classes );
56
                unset( $field_classes[$key] );
57
            }
58
        }
59
60
        // if the form is set to inline, add the inline class to our labels
61
        if( $this->form_inline ) {
0 ignored issues
show
Bug introduced by
The property form_inline does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
62
            $label_classes[] = 'label-inline';
63
        }
64
        
65
        if( isset( $field['hide-label'] ) ) {
66
            if( absint( $field['hide-label'] ) === 1 ) {
67
                $this->increase_hidden_label_count();
68
                $field_classes[] = 'field-no-label';
69
            }
70
        }
71
72
        return [
73
            'field_classes' => $field_classes,
74
            'label_classes' => $label_classes,
75
        ];
76
    }
77
78
    protected function increase_hidden_label_count() {
79
        $this->hidden_label_count = $this->hidden_label_count++;
0 ignored issues
show
Bug introduced by
The property hidden_label_count does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
80
    }
81
82
    protected function get_label( $field ) {
83
        $label = [];
84
        if( $field['type'] == 'email' ) {
85
            $label['props']['visible'] = '';
86
        } else {
87
            $label['props']['visible'] = isset( $field['hide'] ) ? 'style="display:none;"' : '';
88
        }
89
        if ( isset( $field['hide-label'] ) ) {
90
            $label['hide-label'] = true;
91
        }
92
        if ( isset( $field['label'] ) ) {
93
            $label['value'] = $field['label'];
94
        }
95
        return $label;
96
    }
97
98
    protected function get_value( $field ) {
99
        // pass our default value through our filter to parse dynamic data by tag (used solely for 'text' type)
100
        $default_value = ( isset( $field['default'] ) ? esc_attr( $field['default'] ) : '' );
101
        $default_value = apply_filters( 'yikes-mailchimp-process-default-tag', $default_value );
102
        return apply_filters( 'yikes-mailchimp-' . $field['merge'] . '-default-value', $default_value, $field, $this->form_id );
0 ignored issues
show
Bug introduced by
The property form_id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
103
    }
104
105
    protected function get_placeholder( $field ) {
106
        return isset( $field['placeholder'] ) ? $field['placeholder'] : '';
107
    }
108
109
    protected function get_hidden( $field ) {
110
        $visible = true;
111
        // if both hide label and hide field are checked, we gotta hide the field!
112 View Code Duplication
        if( isset( $field['hide' ] ) && $field['hide'] == 1 ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
            if( isset( $field['hide-label' ] ) && $field['hide-label'] == 1 ) {
114
                $visible = false;
115
            }
116
        }
117
        return $visible;
118
    }
119
120
    protected function get_description( $field ) {
121
        $show_description  = false;
122
        $description_above = false;
123
        $description       = '';
124
125
        if ( isset( $field['description'] ) && trim( $field['description'] ) !== '' ) {
126
            $show_description = true;
127
            $description = $field['description'];
128
        }
129
130
        if ( isset( $field['description_above'] ) && $field['description_above'] === '1' ) {
131
            $description_above = true;
132
        }
133
        
134
        return [
135
            'show_description'  => $show_description,
136
            'description_above' => $description_above,
137
            'description'       => $description,
138
        ];
139
    }
140
}
141