Completed
Push — master ( 5ea630...6773cf )
by Maciej
17s queued 10s
created

FormComposite::display()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
namespace WFV;
3
defined( 'ABSPATH' ) || die();
4
5
use WFV\Artisan\FormArtisan;
6
use WFV\Contract\ValidateInterface;
7
use WFV\Factory\ValidatorFactory;
8
9
/**
10
 * Form Composition
11
 *
12
 * @since 0.10.0
13
 */
14
class FormComposite {
15
16
	/**
17
	 *
18
	 *
19
	 * @since 0.10.0
20
	 * @access private
21
	 * @var string
22
	 */
23
	protected $alias;
24
25
	/**
26
	 *
27
	 *
28
	 * @since 0.10.0
29
	 * @access protected
30
	 * @var array
31
	 */
32
	protected $collection;
33
34
	/**
35
	 *
36
	 *
37
	 * @since 0.11.0
38
	 * @access protected
39
	 * @var Validator
40
	 */
41
	protected $validator;
42
43
	/**
44
	 *
45
	 *
46
	 * @since 0.10.0
47
	 *
48
	 * @param ArtisanInterface $builder
49
	 * @param string $action
50
	 */
51
	public function __construct( FormArtisan $builder, $action ) {
52
		$this->alias = $action;
53
		$this->collection = $builder->collection;
54
		$this->validator = $builder->validator;
55
	}
56
57
	/**
58
	 * Convenience method to repopulate checkbox input
59
	 *
60
	 * @since 0.10.0
61
	 *
62
	 * @param string $field Field name.
63
	 * @param string $value Value to compare against.
64
	 * @return string|null
65
	 */
66
	public function checked_if( $field = null, $value = null ) {
67
		return $this->string_or_null( 'checked', $field, $value );
68
	}
69
70
	/**
71
	 * Echo the encoded value of given field from a callback
72
	 * Default callback is esc_html()
73
	 * Also returns the encoded string for assignment
74
	 *
75
	 * @since 0.10.1
76
	 *
77
	 * @param string (optional) $field
78
	 * @param callable (optional) $callback
79
	 * @return string
80
	 */
81
	public function display( $field = null, callable $callback = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $callback is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
		echo $input = $this->utilize('input')->escape( $field );
83
		return $input;
84
	}
85
86
	/**
87
	 * Use error collection
88
	 *
89
	 *
90
	 * @since 0.10.0
91
	 *
92
	 * @return WFV\Collection\ErrorCollection
93
	 */
94
	public function errors() {
95
		return $this->utilize('errors');
96
	}
97
98
	/**
99
	 * Use input collection
100
	 *
101
	 * @since 0.10.0
102
	 *
103
	 * @return WFV\Collection\InputCollection
104
	 */
105
	public function input() {
106
		return $this->utilize('input');
107
	}
108
109
	/**
110
	 * Check if the validation passed or failed
111
	 * Sets the error msgs if a fail
112
	 * Trigger pass or fail action
113
	 *
114
	 * @since 0.11.0
115
	 *
116
	 * @return bool
117
	 */
118
	public function is_valid() {
119
		$is_valid = $this->validator->is_valid();
120
		if( false === $is_valid ) {
121
			$this->utilize('errors')->set_errors( $this->validator->errors() );
122
		}
123
		$this->trigger_post_validate_action( $is_valid );
124
		return $is_valid;
125
	}
126
127
	/**
128
	 * Use rules collection
129
	 *
130
	 * @since 0.11.0
131
	 *
132
	 * @return WFV\Collection\RuleCollection
133
	 */
134
	public function rules() {
135
		return $this->utilize('rules');
136
	}
137
138
	/**
139
	 * Convenience method to repopulate select input
140
	 *
141
	 * @since 0.10.0
142
	 *
143
	 * @param string $field Field name.
144
	 * @param string $value Value to compare against.
145
	 * @return string|null
146
	 */
147
	public function selected_if( $field = null, $value = null ) {
148
		return $this->string_or_null( 'selected', $field, $value );
149
	}
150
151
	/**
152
	 * Convienience method to print the hidden fields
153
	 *  for token and action
154
	 *
155
	 * @since 0.10.0
156
	 *
157
	 */
158
	public function token_fields() {
159
		// TODO - Move markup into something - perhaps a renderable interface?
160
		$token_name = $this->alias . '_token';
161
		echo $nonce_field = wp_nonce_field( $this->alias, $token_name, false, false );
162
		echo $action_field = '<input type="hidden" name="action" value="'. $this->alias .'">';
163
	}
164
165
	/**
166
	 * Perform the validation cycle
167
	 *
168
	 * @since 0.11.0
169
	 *
170
	 * @param ValidatorFactory $factory
171
	 * @return self
172
	 */
173
	public function validate( ValidatorFactory $factory ) {
174
		$rule_collection = $this->utilize('rules');
175
		$rules = $rule_collection->get_array( true );
176
177
		foreach( $rules as $field => $ruleset ) {
178
			$input = $this->field_value( $field );
179
			$optional = $rule_collection->is_optional( $field );
180
181
			foreach( $ruleset as $index => $rule ) {
182
				$params = $rule_collection->get_params( $field, $index );
183
				$this->validator->validate( $factory->get( $rule ), $field, $input, $optional, $params );
0 ignored issues
show
Bug introduced by
It seems like $factory->get($rule) targeting WFV\Factory\ValidatorFactory::get() can also be of type boolean; however, WFV\Validator::validate() does only seem to accept object<WFV\Contract\ValidateInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $input defined by $this->field_value($field) on line 178 can also be of type null; however, WFV\Validator::validate() does only seem to accept string|array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
184
			}
185
		}
186
		return $this;
187
	}
188
189
	/**
190
	 * Returns the input value for a field
191
	 * When not present, returns null
192
	 *
193
	 * @since 0.11.0
194
	 * @access protected
195
	 *
196
	 * @param string $field
197
	 * @return string|array|null
198
	 */
199
	protected function field_value( $field ) {
200
		$input = $this->utilize('input');
201
		if( $input->has( $field ) ) {
202
			$input = $input->get_array();
203
			return $input[ $field ];
204
		}
205
		return null;
206
	}
207
208
	/**
209
	 *
210
	 *
211
	 * @since 0.10.0
212
	 * @access protected
213
	 *
214
	 * @param string $response
215
	 * @param string (optional) $field
216
	 * @param string (optional) $value
217
	 * @return string|null
218
	 */
219
	protected function string_or_null( $response, $field = null, $value = null ) {
220
		$input = $this->utilize('input');
221
		return ( $input->contains( $field, $value ) ) ? $response : null;
222
	}
223
224
	/**
225
	 * Trigger action hook for validation pass or fail
226
	 *
227
	 * @since 0.10.0
228
	 * @access protected
229
	 *
230
	 * @param bool $is_valid
231
	 */
232
	protected function trigger_post_validate_action( $is_valid = false ) {
233
		$action = ( true === $is_valid ) ? $this->alias : $this->alias .'_fail';
234
		do_action( $action, $this );
235
	}
236
237
	/**
238
	 * Use a component.
239
	 *
240
	 * @since 0.10.0
241
	 * @access protected
242
	 *
243
	 * @param string $component Key indentifier.
244
	 */
245
	protected function utilize( $component ) {
246
		return $this->collection[ $component ];
247
	}
248
}
249