Completed
Pull Request — staging (#840)
by
unknown
16:43
created

FieldBuilder::get_description()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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