Issues (73)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Collection/RuleCollection.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 12 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\Collection;
3
defined( 'ABSPATH' ) || die();
4
5
use WFV\Abstraction\Collectable;
6
7
/**
8
 *
9
 *
10
 * @since 0.10.0
11
 */
12
class RuleCollection extends Collectable {
13
14
	/**
15
	 *
16
	 *
17
	 * @since 0.10.0
18
	 *
19
	 * @param array $form
20
	 */
21
	public function __construct( array $form ) {
22
		$this->data = $this->parse_rules( $form );
23
	}
24
25
26
	/**
27
	 * Get rules array
28
	 * When $flat is true, returns array without params
29
	 *
30
	 * @since 0.10.0
31
	 *
32
	 * @param bool (optional) $flat
33
	 * @return array
34
	 */
35
	public function get_array( $flat = false ) {
36
		return ( $flat ) ? $this->remove_params() : $this->data;
37
	}
38
39
	/**
40
	 * Return a rule's parameters or false if none
41
	 *
42
	 * @since 0.11.0
43
	 *
44
	 * @param string $field
45
	 * @param int $index
46
	 * @return array|bool
47
	 */
48
	public function get_params( $field, $index ) {
49
		return ( $this->has_params( $field, $index ) )
50
			? $this->data[ $field ][ $index ]['params']
51
			: false;
52
	}
53
54
	/**
55
	 * Returns true if field is optional
56
	 *
57
	 * @since 0.11.0
58
	 *
59
	 * @param string $field
60
	 * @return bool
61
	 */
62
	public function is_optional( $field ) {
63
		if( $this->has( $field ) ){
64
			return in_array('optional', $this->data[ $field ] );
65
		}
66
		return false;
67
	}
68
69
	/**
70
	 * Get array of unique rule types
71
	 *
72
	 * @since 0.11.0
73
	 *
74
	 * @return array
75
	 */
76
	public function unique() {
77
		$flat = $this->flatten( $this->remove_params() );
78
		return array_values( array_unique( $flat ) );
79
	}
80
81
	/**
82
	 * Extract rule name from a rule string
83
	 *
84
	 * @since 0.11.0
85
	 * @access protected
86
	 *
87
	 * @param string $rule
88
	 * @return string
89
	 */
90
	protected function extract_name( $rule ) {
91
		return strstr( $rule, ':', true );
92
	}
93
94
	/**
95
	 * Extract rule parameters from a rule string
96
	 *
97
	 * @since 0.11.0
98
	 * @access protected
99
	 *
100
	 * @param string $rule
101
	 * @return string
102
	 */
103
	protected function extract_params( $rule ) {
104
		return ltrim( strstr($rule, ':'), ':');
105
	}
106
107
	/**
108
	 * Extract rules from form config array
109
	 *
110
	 * @since 0.11.3
111
	 * @access protected
112
	 *
113
	 * @param array $form
114
	 * @return array
115
	 */
116
	protected function extract_rules( array $form ) {
117
		foreach( $form as $field => $options ) {
118
			$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...
119
		}
120
		return $rules;
0 ignored issues
show
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...
121
	}
122
123
	/**
124
	 * Returns a flat index array of rules
125
	 *
126
	 * @since 0.11.0
127
	 * @access protected
128
	 *
129
	 * @param array $array
130
	 * @return array
131
	 */
132
	protected function flatten( array $array ) {
133
		$flat = array();
134
		foreach( $array as $rule ) {
135
			if( is_array( $rule ) ){
136
				$flat = array_merge( $flat, $this->flatten( $rule ) );
137
			} else {
138
				$flat[] = $rule;
139
			}
140
		}
141
		return $flat;
142
	}
143
144
	/**
145
	 * Returns true when a rule has parameters
146
	 *
147
	 * @since 0.11.0
148
	 * @access protected
149
	 *
150
	 * @param string $field
151
	 * @param int $index
152
	 * @return bool
153
	 */
154
	protected function has_params( $field, $index ) {
155
		return is_array( $this->data[ $field ][ $index ] );
156
	}
157
158
	/**
159
	 * Split each string ruleset from config array
160
	 *  into a machine friendly multi-dimensional array
161
	 *
162
	 * @since 0.11.0
163
	 * @access protected
164
	 *
165
	 * @param array $form
166
	 * @return array
167
	 */
168
	protected function parse_rules( array $form ) {
169
		// WIP - works, but confusing - simplify or breakdown into small methods
170
		$rules = $this->extract_rules( $form );
171
172
		$parsed = array();
173
		$this->split_rules( $rules );
174
		foreach( $rules as $field => $ruleset ) {
175
			$parsed[ $field ] = array_map( function( $rule ) {
176
				if ( $this->string_has_params( $rule ) ) {
177
					return array(
178
						'rule' => $this->extract_name( $rule ),
179
						'params' => explode( ',', $this->extract_params( $rule ) )
180
					);
181
				}
182
				return $rule;
183
			}, $ruleset );
184
		}
185
		return $parsed;
186
	}
187
188
	/**
189
	 * Flatens rules with parameters in the collection
190
	 *  and returns the new array.
191
	 *
192
	 * @since 0.11.0
193
	 * @access protected
194
	 *
195
	 * @return array
196
	 */
197
	protected function remove_params() {
198
		return array_map( function( $item ) {
199
			foreach( $item as $index => $rule ) {
200
				if( $rule !== 'optional' ) {
201
					$rules[ $index ] = ( is_string( $rule ) ) ? $rule : $rule['rule'];
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...
202
				}
203
			}
204
			return $rules;
0 ignored issues
show
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...
205
		}, $this->data );
206
	}
207
208
	/**
209
	 * Converts string ruleset to index array
210
	 *
211
	 * @since 0.11.0
212
	 * @access protected
213
	 *
214
	 * @param array $rules
215
	 */
216
	protected function split_rules( array &$rules ) {
217
		// perhaps the $rules array structure should be validated here?...
218
		$rules = array_map( function( $item ) {
219
			return explode( '|', $item );
220
		}, $rules );
221
	}
222
223
	/**
224
	 * Checks if a rule string has parameters
225
	 *
226
	 * @since 0.11.0
227
	 * @access protected
228
	 *
229
	 * @param string $rule
230
	 * @return bool
231
	 */
232
	protected function string_has_params( $rule ) {
233
		return strpos( $rule, ':' );
234
	}
235
}
236