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

FormArtisan::resolve_strategies()   C

Complexity

Conditions 7
Paths 11

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 11
nop 0
dl 0
loc 31
rs 6.7272
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 19 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\Artisan;
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\Contract\ArtisanInterface;
6
use WFV\Collection\ErrorCollection;
7
use WFV\Collection\InputCollection;
8
use WFV\Collection\MessageCollection;
9
use WFV\Collection\RuleCollection;
10
11
use WFV\FormComposite;
12
use WFV\Validator;
13
14
/**
15
 *
16
 *
17
 * @since 0.10.0
18
 */
19
class FormArtisan implements ArtisanInterface {
20
21
	/**
22
	 *
23
	 *
24
	 * @since 0.10.0
25
	 * @var array
26
	 */
27
	public $collection = array();
28
29
	/**
30
	 *
31
	 *
32
	 * @since 0.11.0
33
	 * @var WFV\Validator
34
	 */
35
	public $validator;
36
37
	/**
38
	 *
39
	 *
40
	 * @since 0.10.0
41
	 * @access protected
42
	 * @var array
43
	 */
44
	protected $config = array();
45
46
	/**
47
	 *
48
	 *
49
	 * @since 0.10.0
50
	 * @access protected
51
	 * @var WFV\FormComposite
52
	 */
53
	protected $form;
54
55
	/**
56
	 *
57
	 *
58
	 * @since 0.11.0
59
	 *
60
	 * @param array $config
61
	 */
62
	function __construct( array $config ) {
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...
63
		$this->config = $config;
64
	}
65
66
	/**
67
	 * Return the final Form instance
68
	 *
69
	 * @since 0.10.0
70
	 *
71
	 * @return WFV\FormComposite
72
	 */
73
	public function actualize() {
74
		return $this->form;
75
	}
76
77
	/**
78
	 * Creates the instance of FormComposite
79
	 *
80
	 * @since 0.10.0
81
	 *
82
	 * @param string $action
83
	 * @return WFV\Artisan\FormArtisan
84
	 */
85
	public function create( $action ) {
86
		$this->form = new FormComposite( $this, $action );
0 ignored issues
show
Documentation Bug introduced by
It seems like new \WFV\FormComposite($this, $action) of type object<WFV\FormComposite> is incompatible with the declared type object<WFV\Artisan\WFV\FormComposite> of property $form.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
87
		return $this;
88
	}
89
90
	/**
91
	 * Create instance of ErrorCollection
92
	 * Save it in $collection array property
93
	 *
94
	 * @since 0.10.0
95
	 *
96
	 * @return WFV\Artisan\FormArtisan
97
	 */
98
	public function errors() {
99
		$this->collection['errors'] = new ErrorCollection( $this->labels() );
100
		return $this;
101
	}
102
103
	/**
104
	 * Create instance of InputCollection
105
	 * Save it in $collection array property
106
	 *
107
	 * @since 0.10.0
108
	 *
109
	 * @param array $data
110
	 * @return WFV\Artisan\FormArtisan
111
	 */
112
	public function input( array $data = [] ) {
113
		$input = $data[0];
114
		$trim = $data[1];
115
		$this->collection['input'] = new InputCollection( $input, $trim );
116
		return $this;
117
	}
118
119
	/**
120
	 *
121
	 *
122
	 * @since 0.10.0
123
	 *
124
	 * @return WFV\Artisan\FormArtisan
125
	 */
126
	public function messages() {
127
		$this->collection['messages'] = new MessageCollection( $this->config );
128
		return $this;
129
	}
130
131
	/**
132
	 * Create instance of RuleCollection
133
	 * Save it in $collection array property
134
	 *
135
	 * @since 0.10.0
136
	 *
137
	 * @return WFV\Artisan\FormArtisan
138
	 */
139
	public function rules() {
140
		foreach( $this->config as $field => $options ) {
141
			$rules[ $field ] = $options['rules'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$rules was never initialized. Although not strictly required by PHP, it is generally a good practice to add $rules = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
142
		}
143
		$this->collection['rules'] = new RuleCollection( $rules );
0 ignored issues
show
Bug introduced by
The variable $rules does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
144
		return $this;
145
	}
146
147
	/**
148
	 * Create instance of WFV\Validator
149
	 * Save it in $validator property
150
	 *
151
	 * @since 0.11.0
152
	 *
153
	 * @return WFV\Artisan\FormArtisan
154
	 */
155
	public function validator() {
156
		$this->validator = new Validator();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \WFV\Validator() of type object<WFV\Validator> is incompatible with the declared type object<WFV\Artisan\WFV\Validator> of property $validator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
157
		return $this;
158
	}
159
160
	/**
161
	 * Returns an array of human friendly field labels
162
	 *  as defined in the config array
163
	 *
164
	 * @since 0.11.0
165
	 * @access protected
166
	 *
167
	 * @return array
168
	 */
169
	protected function labels() {
170
		return array_map( function( $item ) {
171
			return $item['label'];
172
		}, $this->config);
173
	}
174
}
175