Completed
Pull Request — staging (#840)
by
unknown
17:30
created

FieldBuilder::get_field_classes()   F

Complexity

Conditions 12
Paths 774

Size

Total Lines 65

Duplication

Lines 35
Ratio 53.85 %

Importance

Changes 0
Metric Value
cc 12
nc 774
nop 1
dl 35
loc 65
rs 3.2547
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
        $field_classes[] = 'yikes-easy-mc-' . $field['type'];
18
        $label_classes[] = $field['merge'] . '-label';
19
20
        if( $field['additional-classes'] != '' ) {
21
22
            $field_classes = explode( ' ' , $field['additional-classes'] );
23
24 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...
25
                $$label_classes[] = 'field-left-half';
26
                $key = array_search( 'field-left-half' , $field_classes );
27
                unset( $field_classes[$key] );
28
            } // input half right
29 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...
30
                $$label_classes[] = 'field-right-half';
31
                $key = array_search( 'field-right-half' , $field_classes );
32
                unset( $field_classes[$key] );
33
            } // input thirds (1/3 width, floated left)
34 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...
35
                $$label_classes[] = 'field-third';
36
                $key = array_search( 'field-third' , $field_classes );
37
                unset( $field_classes[$key] );
38
            } // 2 column radio
39 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...
40
                $$label_classes[] = 'option-2-col';
41
                $key = array_search( 'option-2-col' , $field_classes );
42
                unset( $field_classes[$key] );
43
            } // 3 column radio
44 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...
45
                $$label_classes[] = 'option-3-col';
46
                $key = array_search( 'option-3-col' , $field_classes );
47
                unset( $field_classes[$key] );
48
            } // 4 column radio
49 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...
50
                $$label_classes[] = 'option-4-col';
51
                $key = array_search( 'option-4-col' , $field_classes );
52
                unset( $field_classes[$key] );
53
            } // inline radio & checkboxes etc
54 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...
55
                $$label_classes[] = 'option-inline';
56
                $key = array_search( 'option-inline' , $field_classes );
57
                unset( $field_classes[$key] );
58
            }
59
        }
60
61
        // if the form is set to inline, add the inline class to our labels
62
        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...
63
            $label_classes[] = 'label-inline';
64
        }
65
        
66
        if( isset( $field['hide-label'] ) ) {
67
            if( absint( $field['hide-label'] ) === 1 ) {
68
                $this->increase_hidden_label_count();
69
                $field_classes[] = 'field-no-label';
70
            }
71
        }
72
73
        return [
74
            'field_classes' => $field_classes,
75
            'label_classes' => $label_classes,
76
        ];
77
    }
78
79
    protected function increase_hidden_label_count() {
80
        $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...
81
    }
82
83
    protected function get_label( $field ) {
84
        $label = [];
85
        if( $field['type'] == 'email' ) {
86
            $label['props']['visible'] = '';
87
        } else {
88
            $label['props']['visible'] = isset( $field['hide'] ) ? 'style="display:none;"' : '';
89
        }
90
        if ( isset( $field['hide-label'] ) ) {
91
            $label['hide-label'] = true;
92
        }
93
        if ( isset( $field['label'] ) ) {
94
            $label['value'] = $field['label'];
95
        }
96
        return $label;
97
    }
98
99
    protected function get_value( $field ) {
100
        // pass our default value through our filter to parse dynamic data by tag (used solely for 'text' type)
101
        $default_value = ( isset( $field['default'] ) ? esc_attr( $field['default'] ) : '' );
102
        $default_value = apply_filters( 'yikes-mailchimp-process-default-tag', $default_value );
103
        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...
104
    }
105
106
    protected function get_placeholder( $field ) {
107
        return isset( $field['placeholder'] ) ? $field['placeholder'] : '';
108
    }
109
110
    protected function get_hidden( $field ) {
111
        $visible = false;
112
        // if both hide label and hide field are checked, we gotta hide the field!
113 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...
114
            if( isset( $field['hide-label' ] ) && $field['hide-label'] == 1 ) {
115
                $visible = true;
116
            }
117
        }
118
        return $visible;
119
    }
120
121
    protected function get_description( $field ) {
122
        $description       = '';
123
        $show_description  = isset( $field['description'] ) ? true : false;
124
        $description_above = isset( $field['description_above'] ) && $field['description_above'] === '1' ? true : false;
125
        if ( $show_description === true ) {
126
            $description = apply_filters( 'yikes-mailchimp-' . $field['merge'] . '-description', esc_attr( stripslashes( $field['description'] ) ), $this->form_id );
127
        }
128
        
129
        return [
130
            'show_description'  => $show_description,
131
            'description_above' => $description_above,
132
            'description'       => $description,
133
        ];
134
    }
135
}
136