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

BaseInput::render()   B

Complexity

Conditions 8
Paths 32

Size

Total Lines 45

Duplication

Lines 15
Ratio 33.33 %

Importance

Changes 0
Metric Value
cc 8
nc 32
nop 0
dl 15
loc 45
rs 7.9555
c 0
b 0
f 0
1
<?php
2
/**
3
 * YIKES Inc. Easy Mailchimp Forms Plugin.
4
 *
5
 * @package YIKES\EasyForms
6
 * @author  Freddie Mixell
7
 * @license GPL2
8
 */
9
10
namespace YIKES\EasyForms\Field;
11
12
use YIKES\EasyForms\Exception\MustExtend;
13
14
/**
15
 * Class BaseInput
16
 *
17
 * @since   %VERSION%
18
 * @package YIKES\EasyForms
19
 */
20
class BaseInput extends BaseField {
21
22
	/**
23
	 * Field ID.
24
	 *
25
	 * @var string
26
	 */
27
	private $field_id;
28
29
	/**
30
	 * Field's classes
31
	 *
32
	 * @var array
33
	 */
34
	private $classes = [];
35
36
	/**
37
	 * Field placeholder.
38
	 *
39
	 * @var string
40
	 */
41
	private $placeholder;
42
43
	/**
44
	 * Field name.
45
	 *
46
	 * @var string
47
	 */
48
	private $name;
49
50
	/**
51
	 * Field value.
52
	 *
53
	 * @var string
54
	 */
55
	private $value;
56
57
	/**
58
	 * Construct Field
59
	 *
60
	 * @param string $id          Fields ID.
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
61
	 * @param array  $classes     Field and label classes.
62
	 * @param string $placeholder Fields placeholder.
63
	 * @param string $name        Field name.
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
64
	 */
65
	public function __construct( $classes, $placeholder, $label, $value, $description, $merge, $form_id, $hidden ) {
66
		$this->classes     = $classes;
67
		$this->placeholder = $placeholder;
68
		$this->label       = $label;
0 ignored issues
show
Bug introduced by
The property label 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...
69
		$this->value       = $value;
70
		$this->description = $this->set_description( $description );
0 ignored issues
show
Bug introduced by
The property description 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...
Bug introduced by
Are you sure the assignment to $this->description is correct as $this->set_description($description) (which targets YIKES\EasyForms\Field\BaseInput::set_description()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
71
		$this->merge       = $merge;
0 ignored issues
show
Bug introduced by
The property merge 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...
72
		$this->form_id     = $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...
73
		$this->hidden      = $hidden;
0 ignored issues
show
Bug introduced by
The property hidden 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...
74
	}
75
76
	const TYPE     = 'text';
77
	const REQUIRED = false;
78
79
	/**
80
	 * Get Field Type
81
	 *
82
	 * @return string $field['type']
83
	 */
84
	public function get_type() {
85
		return static::TYPE;
86
	}
87
88
	public function get_placeholder() {
89
		return $this->placeholder;
90
	}
91
92
	public function field_classes() {
93
		return $this->classes['field_classes'];
94
	}
95
96
	public function label_classes() {
97
		if ( true === static::REQUIRED ) {
98
			$this->classes['label_classes'][] = 'yikes-mailchimp-field-required';
99
		}
100
		return $this->classes['label_classes'];
101
	}
102
103
	public function get_name() {
104
		return $this->merge;
105
	}
106
107
	public function get_id() {
108
        return 'yikes-easy-mc-form-' . $this->form_id . '-' . $this->merge;
109
    }
110
111
	public function get_value() {
112
		return $this->value;
113
	}
114
115
	public function set_description( $description ) {
116
		$this->show_desc   = $description['show_description'];
0 ignored issues
show
Bug introduced by
The property show_desc 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...
117
		$this->desc_above  = $description['description_above'];
0 ignored issues
show
Bug introduced by
The property desc_above 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...
118
		$this->description = $description['description'];
119
	}
120
121
	/**
122
	 * Render the field.
123
	 *
124
	 * @since %VERSION%
125
	 */
126
	public function render() {
127
		?>
128
		<label for="<?= esc_attr( $this->get_id() ); ?>" class="<?= esc_html( implode( ' ' , $this->label_classes() ) ); ?>" <?= esc_html( implode( ' ' , $this->label['props'] ) ); ?> >
129
130
		<!-- dictate label visibility -->
131
		<?php if ( ! isset( $this->label['hide-label'] ) ) { ?>
132
			<span class="<?= esc_attr( $this->merge ) . '-label'; ?>">
133
				<?= esc_html( apply_filters( 'yikes-mailchimp-'. $this->merge .'-label' , esc_attr( $this->label['value'] ), $this->form_id ) ); ?>
134
			</span>
135
		<?php }
136
137 View Code Duplication
		if ( $this->show_desc === true && $this->desc_above === true ) :
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...
138
		?>
139
140
        <p class="form-field-description" id="form-field-description-<?= esc_attr( $this->merge ); ?>">
141
			<?= esc_html( apply_filters( 'yikes-mailchimp-' . $this->merge . '-description', $this->description, $this->form_id ) ); ?>
142
		</p>
143
144
        <?php
145
        endif;
146
		?>
147
		<input type="<?= esc_attr( $this->get_type() ); ?>"
148
			class="<?= esc_attr( implode( ' ' , $this->field_classes() ) ); ?>"
149
			name="<?= esc_attr( $this->get_name() ); ?>"
150
			placeholder="<?= esc_attr( $this->get_placeholder() ); ?>"
151
			id="<?= esc_attr( $this->get_id() ); ?>"
152
			value="<?= esc_attr( $this->get_value() ); ?>"
153
			<?php if ( true === static::REQUIRED ) : ?>
154
			required="required"
155
			<?php endif; ?>
156
			<?php if ( true === $this->hidden ) : ?>
157
			style="display:none;"
158
			<?php endif; ?>
159
		/>
160
		<?php
161 View Code Duplication
		if ( $this->show_desc === true && $this->desc_above === false ) {
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...
162
			$desc_value = apply_filters( 'yikes-mailchimp-' . $this->merge . '-description', $this->description, $this->form_id );
163
		?>
164
            <p class="form-field-description" id="form-field-description-<?= esc_attr( $this->merge ); ?>"><?=  esc_html( $desc_value ); ?></p>
165
        <?php
166
		}
167
		?>
168
		</label>
169
		<?php
170
	}
171
}
172