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

BaseInput::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 9
dl 0
loc 11
rs 9.9
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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