Test Failed
Pull Request — master (#119)
by Maciej
02:39
created

FormComposite::errors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
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' ) or die();
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
4
5
use WFV\Abstraction\Composable;
6
use WFV\Contract\ArtisanInterface;
7
use WFV\Contract\ValidateInterface;
8
9
/**
10
 * Form Composition
11
 *
12
 * @since 0.10.0
13
 */
14
class FormComposite extends Composable {
15
16
	/**
17
	 *
18
	 *
19
	 * @since 0.11.0
20
	 * @access protected
21
	 * @var array
22
	 */
23
	protected $validator;
24
25
	/**
26
	 *
27
	 *
28
	 * @since 0.10.0
29
	 *
30
	 * @param ArtisanInterface $builder
31
	 * @param string $action
32
	 */
33
	function __construct( ArtisanInterface $builder, $action ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
34
		$this->alias = $action;
35
		$this->install( $builder->collection );
0 ignored issues
show
Bug introduced by
Accessing collection on the interface WFV\Contract\ArtisanInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
36
		$this->validator = $builder->validator;
0 ignored issues
show
Bug introduced by
Accessing validator on the interface WFV\Contract\ArtisanInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
37
	}
38
39
	/**
40
	 * Convenience method to repopulate checkbox input
41
	 *
42
	 * @since 0.10.0
43
	 *
44
	 * @param string $field Field name.
45
	 * @param string $value Value to compare against.
46
	 * @return string|null
47
	 */
48
	public function checked_if( $field = null, $value = null ) {
49
		return $this->string_or_null( 'checked', $field, $value );
50
	}
51
52
	/**
53
	 * Echo the encoded value of given field from a callback
54
	 * Default callback is esc_html()
55
	 * Also returns the encoded string for assignment
56
	 *
57
	 * @since 0.10.1
58
	 *
59
	 * @param string (optional) $field
60
	 * @param callable (optional) $callback
61
	 * @return string
62
	 */
63
	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...
64
		echo $input = $this->utilize('input')->escape( $field );
65
		return $input;
66
	}
67
68
	/**
69
	 * Use error collection
70
	 *
71
	 *
72
	 * @since 0.10.0
73
	 *
74
	 * @return WFV\Collection\ErrorCollection
75
	 */
76
	public function errors() {
77
		return $this->utilize('errors');
78
	}
79
80
	/**
81
	 * Use input collection
82
	 *
83
	 * @since 0.10.0
84
	 *
85
	 * @return WFV\Collection\InputCollection
86
	 */
87
	public function input() {
88
		return $this->utilize('input');
89
	}
90
91
	/**
92
	 * Use message collection
93
	 *
94
	 * @since 0.11.0
95
	 *
96
	 * @return WFV\Collection\MessageCollection
97
	 */
98
	public function messages() {
99
		return $this->utilize('messages');
100
	}
101
102
	/**
103
	 * Use rules collection
104
	 *
105
	 * @since 0.11.0
106
	 *
107
	 * @return WFV\Collection\RuleCollection
108
	 */
109
	public function rules() {
110
		return $this->utilize('rules');
111
	}
112
113
	/**
114
	 * Convenience method to repopulate select input
115
	 *
116
	 * @since 0.10.0
117
	 *
118
	 * @param string $field Field name.
119
	 * @param string $value Value to compare against.
120
	 * @return string|null
121
	 */
122
	public function selected_if( $field = null, $value = null ) {
123
		return $this->string_or_null( 'selected', $field, $value );
124
	}
125
126
	/**
127
	 * Convienience method to print the hidden fields
128
	 *  for token and action
129
	 *
130
	 * @since 0.10.0
131
	 *
132
	 */
133
	public function token_fields() {
134
		// TODO - Move markup into something - perhaps a renderable interface?
135
		$token_name = $this->alias . '_token';
136
		echo $nonce_field = wp_nonce_field( $this->alias, $token_name, false, false );
137
		echo $action_field = '<input type="hidden" name="action" value="'. $this->alias .'">';
138
	}
139
140
	/**
141
	 * Validate a field's input/rule pair
142
	 *
143
	 * @since 0.11.0
144
	 *
145
	 */
146
	public function validate( ValidateInterface $rule, $field ) {
147
		$input = $this->field_value( $field );
148
		$this->validator->validate( $rule, $field, $input );
0 ignored issues
show
Bug introduced by
The method validate cannot be called on $this->validator (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
149
	}
150
151
	/**
152
	 *
153
	 *
154
	 * @since 0.11.0
155
	 * @access protected
156
	 *
157
	 * @param string $field
158
	 */
159
	protected function field_value( $field ) {
160
		$input = $this->utilize('input');
161
		if( $input->has( $field ) ) {
162
			$input = $input->get_array( false );
163
			return $input[ $field ];
164
		}
165
		return null;
166
	}
167
168
	/**
169
	 * Check if the validation passed or failed
170
	 * Sets the error msgs if a fail
171
	 * Trigger pass or fail action
172
	 *
173
	 * @since 0.11.0
174
	 *
175
	 * @return bool
176
	 */
177
	public function is_valid() {
178
		$is_valid = $this->validator->is_valid();
0 ignored issues
show
Bug introduced by
The method is_valid cannot be called on $this->validator (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
179
		if( false === $is_valid ) {
180
			$this->utilize('errors')->set_errors( $this->validator->errors() );
0 ignored issues
show
Bug introduced by
The method errors cannot be called on $this->validator (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
181
		}
182
		$this->trigger_post_validate_action( $is_valid );
183
		return $is_valid;
184
	}
185
186
	/**
187
	 *
188
	 *
189
	 * @since 0.10.0
190
	 * @access protected
191
	 *
192
	 * @param string $response
193
	 * @param string (optional) $field
194
	 * @param string (optional) $value
195
	 * @return string|null
196
	 */
197
	protected function string_or_null( $response, $field = null, $value = null ) {
198
		return ( $this->input( $field )->contains( $field, $value ) ) ? $response : null;
0 ignored issues
show
Unused Code introduced by
The call to FormComposite::input() has too many arguments starting with $field.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
199
	}
200
201
	/**
202
	 * Trigger action hook for validation pass or fail
203
	 *
204
	 * @since 0.10.0
205
	 * @access protected
206
	 *
207
	 * @param bool $is_valid
208
	 */
209
	protected function trigger_post_validate_action( $is_valid = false ) {
210
		$action = ( true === $is_valid ) ? $this->alias : $this->alias .'_fail';
211
		do_action( $action, $this );
212
	}
213
}
214