Issues (1386)

model/base.php (266 issues)

1
<?php
0 ignored issues
show
Class file names should be based on the class name with "class-" prepended. Expected class-base.php, but found base.php.
Loading history...
This file is missing a doc comment.
Loading history...
2
namespace PodloveSubscribeButton\Model;
3
4
abstract class Base {
0 ignored issues
show
Coding Style Documentation introduced by
Missing class doc comment
Loading history...
5
	/**
6
	 * Property dictionary for all tables
7
	 */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @var tag in member variable comment
Loading history...
8
	private static $properties = array();
9
	
10
	private $is_new = true;
0 ignored issues
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
11
	
12
	/**
13
	 * Contains property values
14
	 */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @var tag in member variable comment
Loading history...
15
	private $data = array();
16
	
17
	public function __set( $name, $value ) {
0 ignored issues
show
Coding Style Documentation introduced by
Missing function doc comment
Loading history...
18
		if ( static::has_property( $name ) ) {
19
			$this->set_property( $name, $value );
20
		} else {
21
			$this->$name = $value;
22
		}
23
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
24
	
25
	private function set_property( $name, $value ) {
0 ignored issues
show
Coding Style Documentation introduced by
Missing function doc comment
Loading history...
26
		$this->data[ $name ] = $value;
27
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
28
	
29
	public function __get( $name ) {
0 ignored issues
show
Coding Style Documentation introduced by
Missing function doc comment
Loading history...
30
		if ( static::has_property( $name ) ) {
31
			return $this->get_property( $name );
32
		} elseif ( property_exists( $this, $name ) ) {
33
			return $this->$name;
34
		} else {
35
			return null;
36
		}
37
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
38
	
39
	private function get_property( $name ) {
0 ignored issues
show
Coding Style Documentation introduced by
Missing function doc comment
Loading history...
40
		if ( isset( $this->data[ $name ] ) ) {
41
			return $this->data[ $name ];
42
		} else {
43
			return null;
44
		}
45
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
46
47
	private static function unserialize_property($property) {
0 ignored issues
show
Expected 1 spaces between opening bracket and argument "$property"; 0 found
Loading history...
Coding Style Documentation introduced by
Missing function doc comment
Loading history...
No space after opening parenthesis is prohibited
Loading history...
No space before closing parenthesis is prohibited
Loading history...
48
		if ( ! isset($property) )
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
49
			return;
50
51
		if ( $unserialized_string = is_serialized($property) )
0 ignored issues
show
The assignment to $unserialized_string is dead and can be removed.
Loading history...
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
Variable assignment found within a condition. Did you mean to do a comparison?
Loading history...
Assignments must be the first block of code on a line
Loading history...
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
52
			return unserialize($property);
0 ignored issues
show
unserialize() found. Serialized data has known vulnerability problems with Object Injection. JSON is generally a better approach for serializing data. See https://www.owasp.org/index.php/PHP_Object_Injection
Loading history...
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
53
54
		return $property;
55
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
56
57
	/**
58
	 * Retrieves the database table name.
59
	 * 
60
	 * The name is derived from the namespace an class name. Additionally, it
61
	 * is prefixed with the global WordPress database table prefix.
62
	 * @todo cache
0 ignored issues
show
There must be exactly one blank line before the tags in a doc comment
Loading history...
63
	 * 
64
	 * @return string database table name
65
	 */
66
	public static function table_name() {
67
		global $wpdb;
68
		
69
		// prefix with $wpdb prefix
0 ignored issues
show
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
70
		return $wpdb->prefix . static::name();
71
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
72
	
73
	/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$args" missing
Loading history...
74
	 * Define a property with name and type.
75
	 * 
76
	 * Currently only supports basics.
77
	 * @todo enable additional options like NOT NULL, DEFAULT etc.
0 ignored issues
show
There must be exactly one blank line before the tags in a doc comment
Loading history...
78
	 * 
79
	 * @param string $name Name of the property / column
0 ignored issues
show
Coding Style Documentation introduced by
Parameter comment must end with a full stop
Loading history...
80
	 * @param string $type mySQL column type 
0 ignored issues
show
Coding Style Documentation introduced by
Parameter comment must end with a full stop
Loading history...
81
	 */
82
	public static function property( $name, $type, $args = array() ) {
83
		$class = get_called_class();
84
		
85
		if ( ! isset( static::$properties[ $class ] ) ) {
0 ignored issues
show
Since $properties is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $properties to at least protected.
Loading history...
86
			static::$properties[ $class ] = array();
87
		}
88
89
		// "id" columns and those ending on "_id" get an index by default
90
		$index = $name == 'id' || stripos( $name, '_id' );
0 ignored issues
show
Found: ==. Use strict comparisons (=== or !==).
Loading history...
Use Yoda Condition checks, you must.
Loading history...
91
		// but if the argument is set, it overrides the default
0 ignored issues
show
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
92
		if (isset($args['index'])) {
0 ignored issues
show
No space after opening parenthesis is prohibited
Loading history...
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
No space before closing parenthesis is prohibited
Loading history...
93
			$index = $args['index'];
94
		}
95
		
96
		static::$properties[ $class ][] = array(
97
			'name'  => $name,
0 ignored issues
show
Array double arrow not aligned correctly; expected 9 space(s) between "'name'" and double arrow, but found 2.
Loading history...
98
			'type'  => $type,
0 ignored issues
show
Array double arrow not aligned correctly; expected 9 space(s) between "'type'" and double arrow, but found 2.
Loading history...
99
			'index' => $index,
0 ignored issues
show
Array double arrow not aligned correctly; expected 8 space(s) between "'index'" and double arrow, but found 1.
Loading history...
100
			'index_length' => isset($args['index_length']) ? $args['index_length'] : null,
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
101
			'unique' => isset($args['unique']) ? $args['unique'] : null
0 ignored issues
show
Array double arrow not aligned correctly; expected 7 space(s) between "'unique'" and double arrow, but found 1.
Loading history...
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
Each array item in a multi-line array declaration must end in a comma
Loading history...
102
		);
103
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
104
	
105
	/**
106
	 * Return a list of property dictionaries.
107
	 * 
108
	 * @return array property list
109
	 */
110
	private static function properties() {
111
		$class = get_called_class();
112
		
113
		if ( ! isset( static::$properties[ $class ] ) ) {
0 ignored issues
show
Since $properties is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $properties to at least protected.
Loading history...
114
			static::$properties[ $class ] = array();
115
		}
116
		
117
		return static::$properties[ $class ];
118
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
119
	
120
	/**
121
	 * Does the given property exist?
122
	 * 
123
	 * @param string $name name of the property to test
0 ignored issues
show
Coding Style Documentation introduced by
Parameter comment must end with a full stop
Loading history...
124
	 * @return bool True if the property exists, else false.
125
	 */
126
	public static function has_property( $name ) {
127
		return in_array( $name, static::property_names() );
0 ignored issues
show
Not using strict comparison for in_array; supply true for third argument.
Loading history...
128
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
129
	
130
	/**
131
	 * Return a list of property names.
132
	 * 
133
	 * @return array property names
134
	 */
135
	public static function property_names() {
136
		return array_map( function ( $p ) { return $p['name']; } , static::properties() );
0 ignored issues
show
Opening brace must be the last content on the line
Loading history...
Closing brace of nested function must be on a new line
Loading history...
Space found before comma in function call
Loading history...
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
137
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
138
	
139
	/**
140
	 * Does the table have any entries?
141
	 * 
142
	 * @return bool True if there is at least one entry, else false.
143
	 */
144
	public static function has_entries() {
145
		return static::count() > 0;
146
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
147
	
148
	/**
149
	 * Return number of rows in the table.
150
	 * 
151
	 * @return int number of rows
152
	 */
153
	public static function count() {
154
		global $wpdb;
155
		
156
		$sql = 'SELECT COUNT(*) FROM ' . static::table_name();
157
		return (int) $wpdb->get_var( $sql );
0 ignored issues
show
Usage of a direct database call is discouraged.
Loading history...
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
Use placeholders and $wpdb->prepare(); found $sql
Loading history...
158
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
159
160
	public static function find_by_id( $id ) {
0 ignored issues
show
Coding Style Documentation introduced by
Missing function doc comment
Loading history...
161
		global $wpdb;
162
		
163
		$class = get_called_class();
164
		$model = new $class();
165
		$model->flag_as_not_new();
166
		
167
		$row = $wpdb->get_row( 'SELECT * FROM ' . static::table_name() . ' WHERE id = ' . (int) $id );
0 ignored issues
show
Usage of a direct database call is discouraged.
Loading history...
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
Use placeholders and $wpdb->prepare(); found static
Loading history...
Use placeholders and $wpdb->prepare(); found ::
Loading history...
Use placeholders and $wpdb->prepare(); found table_name
Loading history...
168
		
169
		if ( ! $row ) {
170
			return null;
171
		}
172
		
173
		foreach ( $row as $property => $value ) {
174
			$model->$property = static::unserialize_property($value);
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
175
		}
176
		
177
		return $model;
178
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
179
180
	public static function find_all_by_property( $property, $value ) {
0 ignored issues
show
Coding Style Documentation introduced by
Missing function doc comment
Loading history...
181
		global $wpdb;
182
		
183
		$class = get_called_class();
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
184
		$models = array();
185
		
186
		$rows = $wpdb->get_results(
0 ignored issues
show
Usage of a direct database call is discouraged.
Loading history...
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
187
			'SELECT * FROM ' . static::table_name() . ' WHERE ' . $property .  ' = \'' . $value . '\''
0 ignored issues
show
Use placeholders and $wpdb->prepare(); found static
Loading history...
Use placeholders and $wpdb->prepare(); found ::
Loading history...
Use placeholders and $wpdb->prepare(); found table_name
Loading history...
Use placeholders and $wpdb->prepare(); found $property
Loading history...
Concat operator must be surrounded by a single space
Loading history...
Use placeholders and $wpdb->prepare(); found $value
Loading history...
188
		);
189
		
190
		if ( ! $rows ) {
191
			return array();
192
		}
193
		
194
		foreach ( $rows as $row ) {
195
			$model = new $class();
196
			$model->flag_as_not_new();
197
			foreach ( $row as $property => $value ) {
0 ignored issues
show
$property is overwriting one of the parameters of this function.
Loading history...
$value is overwriting one of the parameters of this function.
Loading history...
198
				$model->$property = static::unserialize_property($value);
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
199
			}
200
			$models[] = $model;
201
		}
202
		
203
		return $models;
204
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
205
206
	public static function find_one_by_property( $property, $value ) {
0 ignored issues
show
Coding Style Documentation introduced by
Missing function doc comment
Loading history...
207
		global $wpdb;
208
		
209
		$class = get_called_class();
210
		$model = new $class();
211
		$model->flag_as_not_new();
212
		
213
		$row = $wpdb->get_row(
0 ignored issues
show
Usage of a direct database call is discouraged.
Loading history...
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
214
			'SELECT * FROM ' . static::table_name() . ' WHERE ' . $property .  ' = \'' . $value . '\' LIMIT 0,1'
0 ignored issues
show
Use placeholders and $wpdb->prepare(); found static
Loading history...
Use placeholders and $wpdb->prepare(); found ::
Loading history...
Use placeholders and $wpdb->prepare(); found table_name
Loading history...
Use placeholders and $wpdb->prepare(); found $property
Loading history...
Concat operator must be surrounded by a single space
Loading history...
Use placeholders and $wpdb->prepare(); found $value
Loading history...
215
		);
216
		
217
		if ( ! $row ) {
218
			return null;
219
		}
220
		
221
		foreach ( $row as $property => $value ) {
0 ignored issues
show
$property is overwriting one of the parameters of this function.
Loading history...
$value is overwriting one of the parameters of this function.
Loading history...
222
			$model->$property = static::unserialize_property($value);
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
223
		}
224
		
225
		return $model;
226
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
227
228
	public static function find_all_by_where( $where ) {
0 ignored issues
show
Coding Style Documentation introduced by
Missing function doc comment
Loading history...
229
		global $wpdb;
230
		
231
		$class = get_called_class();
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
232
		$models = array();
233
		
234
		$rows = $wpdb->get_results(
0 ignored issues
show
Usage of a direct database call is discouraged.
Loading history...
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
235
			'SELECT * FROM ' . static::table_name() . ' WHERE ' . $where
0 ignored issues
show
Use placeholders and $wpdb->prepare(); found static
Loading history...
Use placeholders and $wpdb->prepare(); found ::
Loading history...
Use placeholders and $wpdb->prepare(); found table_name
Loading history...
Use placeholders and $wpdb->prepare(); found $where
Loading history...
236
		);
237
		
238
		if ( ! $rows ) {
239
			return array();
240
		}
241
		
242
		foreach ( $rows as $row ) {
243
			$model = new $class();
244
			$model->flag_as_not_new();
245
			foreach ( $row as $property => $value ) {
246
				$model->$property = static::unserialize_property($value);
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
247
			}
248
			$models[] = $model;
249
		}
250
		
251
		return $models;
252
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
253
	
254
	public static function find_one_by_where( $where ) {
0 ignored issues
show
Coding Style Documentation introduced by
Missing function doc comment
Loading history...
255
		global $wpdb;
256
		
257
		$class = get_called_class();
258
		$model = new $class();
259
		$model->flag_as_not_new();
260
		
261
		$row = $wpdb->get_row(
0 ignored issues
show
Usage of a direct database call is discouraged.
Loading history...
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
262
			'SELECT * FROM ' . static::table_name() . ' WHERE ' . $where . ' LIMIT 0,1'
0 ignored issues
show
Use placeholders and $wpdb->prepare(); found static
Loading history...
Use placeholders and $wpdb->prepare(); found ::
Loading history...
Use placeholders and $wpdb->prepare(); found table_name
Loading history...
Use placeholders and $wpdb->prepare(); found $where
Loading history...
263
		);
264
		
265
		if ( ! $row ) {
266
			return null;
267
		}
268
		
269
		foreach ( $row as $property => $value ) {
270
			$model->$property = static::unserialize_property($value);
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
271
		}
272
		
273
		return $model;
274
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
275
	/**
276
	 * Retrieve all entries from the table.
277
	 *
278
	 * @param  string $sql_suffix optional SQL, appended after FROM clause
0 ignored issues
show
Coding Style Documentation introduced by
Parameter comment must end with a full stop
Loading history...
279
	 * @return array list of model objects
280
	 */
281
	public static function all( $sql_suffix = '' ) {
282
		global $wpdb;
283
		
284
		$class = get_called_class();
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
285
		$models = array();
286
		
287
		$rows = $wpdb->get_results( 'SELECT * FROM ' . static::table_name() . ' ' . $sql_suffix );
0 ignored issues
show
Usage of a direct database call is discouraged.
Loading history...
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
Use placeholders and $wpdb->prepare(); found static
Loading history...
Use placeholders and $wpdb->prepare(); found ::
Loading history...
Use placeholders and $wpdb->prepare(); found table_name
Loading history...
Use placeholders and $wpdb->prepare(); found $sql_suffix
Loading history...
288
289
		foreach ( $rows as $row ) {
290
			$model = new $class();
291
			$model->flag_as_not_new();
292
			foreach ( $row as $property => $value ) {
293
				$model->$property = static::unserialize_property($value);
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
294
			}
295
			$models[] = $model;
296
		}
297
		
298
		return $models;
299
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
300
	
301
	/**
302
	 * True if not yet saved to database. Else false.
303
	 */
304
	public function is_new() {
305
		return $this->is_new;
306
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
307
	
308
	public function flag_as_not_new() {
0 ignored issues
show
Coding Style Documentation introduced by
Missing function doc comment
Loading history...
309
		$this->is_new = false;
310
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
311
312
	/**
313
	 * Rails-ish update_attributes for easy form handling.
314
	 *
315
	 * Takes an array of form values and takes care of serializing it.
316
	 * 
317
	 * @param  array $attributes
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
318
	 * @return bool
319
	 */
320
	public function update_attributes( $attributes ) {
321
322
		if ( ! is_array( $attributes ) )
0 ignored issues
show
The condition is_array($attributes) is always true.
Loading history...
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
323
			return false;
324
325
		$request = filter_input_array(INPUT_POST); // Do this for security reasons
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
326
			
327
		foreach ( $attributes as $key => $value ) {
328
			if ( is_array($value) ) {
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
329
				$this->{$key} = serialize($value);
0 ignored issues
show
serialize() found. Serialized data has known vulnerability problems with Object Injection. JSON is generally a better approach for serializing data. See https://www.owasp.org/index.php/PHP_Object_Injection
Loading history...
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
330
			} else {
331
				$this->{$key} = esc_sql($value);
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
332
			}
333
		}
334
		
335
		if ( isset( $request['checkboxes'] ) && is_array( $request['checkboxes'] ) ) {
336
			foreach ( $request['checkboxes'] as $checkbox ) {
337
				if ( isset( $attributes[ $checkbox ] ) && $attributes[ $checkbox ] === 'on' ) {
0 ignored issues
show
Use Yoda Condition checks, you must.
Loading history...
338
					$this->$checkbox = 1;
339
				} else {
340
					$this->$checkbox = 0;
341
				}
342
			}
343
		}
344
345
		// @todo this is the wrong place to do this!
346
		// The feed password is the only "passphrase" which is saved. It is not encrypted!
347
		// However, we keep this function for later use
348
		if ( isset( $request['passwords'] ) && is_array( $request['passwords'] ) ) {
349
			foreach ( $request['passwords'] as $password ) {
350
				$this->$password = $attributes[ $password ];
351
			}
352
		}
353
		return $this->save();
354
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
355
356
	/**
357
	 * Update and save a single attribute.
358
	 * 	
0 ignored issues
show
Spaces must be used for mid-line alignment; tabs are not allowed
Loading history...
359
	 * @param  string $attribute attribute name
0 ignored issues
show
Coding Style Documentation introduced by
Parameter comment must end with a full stop
Loading history...
360
	 * @param  mixed  $value
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
361
	 * @return (bool) query success
362
	 */
363
	public function update_attribute($attribute, $value) {
0 ignored issues
show
Expected 1 spaces between opening bracket and argument "$attribute"; 0 found
Loading history...
No space after opening parenthesis is prohibited
Loading history...
No space before closing parenthesis is prohibited
Loading history...
364
		global $wpdb;
365
366
		$this->$attribute = $value;
367
368
		$sql = sprintf(
369
			"UPDATE %s SET %s = '%s' WHERE id = %s",
370
			static::table_name(),
371
			$attribute,
372
			mysqli_real_escape_string($value),
0 ignored issues
show
The call to mysqli_real_escape_string() has too few arguments starting with escapestr. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

372
			/** @scrutinizer ignore-call */ 
373
   mysqli_real_escape_string($value),

This check compares calls to functions or methods with their respective definitions. If the call has less 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. Please note the @ignore annotation hint above.

Loading history...
Accessing the database directly should be avoided. Please use the $wpdb object and associated functions instead. Found: mysqli_real_escape_string.
Loading history...
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
373
			$this->id
374
		);
375
376
		return $wpdb->query( $sql );
0 ignored issues
show
Usage of a direct database call is discouraged.
Loading history...
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
Use placeholders and $wpdb->prepare(); found $sql
Loading history...
377
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
378
	
379
	/**
380
	 * Saves changes to database.
381
	 * 
382
	 * @todo use wpdb::insert()
383
	 */
384
	public function save() {
385
		global $wpdb;
386
387
		if ( $this->is_new() ) {
388
389
			$this->set_defaults();
390
391
			$sql = 'INSERT INTO '
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
392
			     . static::table_name()
0 ignored issues
show
Found precision alignment of 1 spaces.
Loading history...
393
			     . ' ( '
0 ignored issues
show
Found precision alignment of 1 spaces.
Loading history...
394
			     . implode( ',', static::property_names() )
0 ignored issues
show
Found precision alignment of 1 spaces.
Loading history...
395
			     . ' ) '
0 ignored issues
show
Found precision alignment of 1 spaces.
Loading history...
396
			     . 'VALUES'
0 ignored issues
show
Found precision alignment of 1 spaces.
Loading history...
397
			     . ' ( '
0 ignored issues
show
Found precision alignment of 1 spaces.
Loading history...
398
			     . implode( ',', array_map( array( $this, 'property_name_to_sql_value' ), static::property_names() ) )
0 ignored issues
show
Found precision alignment of 1 spaces.
Loading history...
399
			     . ' );'
0 ignored issues
show
Found precision alignment of 1 spaces.
Loading history...
400
			;
0 ignored issues
show
Space found before semicolon; expected "' );';" but found "' );'
;"
Loading history...
401
			$success = $wpdb->query( $sql );
0 ignored issues
show
Usage of a direct database call is discouraged.
Loading history...
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
Use placeholders and $wpdb->prepare(); found $sql
Loading history...
402
			if ( $success ) {
403
				$this->id = $wpdb->insert_id;
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
404
			}
405
		} else {
406
			$sql = 'UPDATE ' . static::table_name()
407
			     . ' SET '
0 ignored issues
show
Found precision alignment of 1 spaces.
Loading history...
408
			     . implode( ',', array_map( array( $this, 'property_name_to_sql_update_statement' ), static::property_names() ) )
0 ignored issues
show
Found precision alignment of 1 spaces.
Loading history...
409
			     . ' WHERE id = ' . $this->id
0 ignored issues
show
Found precision alignment of 1 spaces.
Loading history...
410
			;
0 ignored issues
show
Space found before semicolon; expected "id;" but found "id
;"
Loading history...
411
412
			$success = $wpdb->query( $sql );
0 ignored issues
show
Usage of a direct database call is discouraged.
Loading history...
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
Use placeholders and $wpdb->prepare(); found $sql
Loading history...
413
		}
414
415
		$this->is_new = false;
416
417
		do_action('podlove_model_save', $this);
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
418
		do_action('podlove_model_change', $this);
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
419
420
		return $success;
421
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
422
423
	/**
424
	 * Sets default values.
425
	 * 
426
	 * @return array
427
	 */
428
	private function set_defaults() {
429
		
430
		$defaults = $this->default_values();
431
432
		if ( ! is_array( $defaults ) || empty( $defaults ) )
0 ignored issues
show
The condition is_array($defaults) is always true.
Loading history...
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
433
			return;
434
435
		foreach ( $defaults as $property => $value ) {
436
			if ( $this->$property === null )
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
Use Yoda Condition checks, you must.
Loading history...
437
				$this->$property = $value;
438
		}
439
440
	}
441
442
	/**
443
	 * Return default values for properties.
444
	 * 
445
	 * Can be overridden by inheriting model classes.
446
	 * 
447
	 * @return array
448
	 */
449
	public function default_values() {
450
		return array();
451
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
452
	
453
	public function delete() {
0 ignored issues
show
Coding Style Documentation introduced by
Missing function doc comment
Loading history...
454
		global $wpdb;
455
		
456
		$sql = 'DELETE FROM '
457
		     . static::table_name()
0 ignored issues
show
Found precision alignment of 1 spaces.
Loading history...
458
		     . ' WHERE id = ' . $this->id;
0 ignored issues
show
Found precision alignment of 1 spaces.
Loading history...
459
460
		$rows_affected = $wpdb->query( $sql );
0 ignored issues
show
Usage of a direct database call is discouraged.
Loading history...
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
Use placeholders and $wpdb->prepare(); found $sql
Loading history...
461
462
	    do_action('podlove_model_delete', $this);
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
463
	    do_action('podlove_model_change', $this);
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
464
465
		return $rows_affected !== false;
0 ignored issues
show
Use Yoda Condition checks, you must.
Loading history...
466
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
467
468
	private function property_name_to_sql_update_statement( $p ) {
0 ignored issues
show
Coding Style Documentation introduced by
Missing function doc comment
Loading history...
469
		global $wpdb;
470
471
		if ( $this->$p !== null && $this->$p !== '' ) {
0 ignored issues
show
Use Yoda Condition checks, you must.
Loading history...
472
			return sprintf( "%s = '%s'", $p, ( is_array($this->$p) ? serialize($this->$p) : $this->$p ) );
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
serialize() found. Serialized data has known vulnerability problems with Object Injection. JSON is generally a better approach for serializing data. See https://www.owasp.org/index.php/PHP_Object_Injection
Loading history...
473
		} else {
474
			return "$p = NULL";
475
		}
476
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
477
	
478
	private function property_name_to_sql_value( $p ) {
0 ignored issues
show
Coding Style Documentation introduced by
Missing function doc comment
Loading history...
479
		global $wpdb;
480
481
		if ( $this->$p !== null && $this->$p !== '' ) {
0 ignored issues
show
Use Yoda Condition checks, you must.
Loading history...
482
			return sprintf( "'%s'", $this->$p );
483
		} else {
484
			return 'NULL';
485
		}
486
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
487
	
488
	/**
489
	 * Create database table based on defined properties.
490
	 * 
491
	 * Automatically includes an id column as auto incrementing primary key.
492
	 * @todo allow model changes
0 ignored issues
show
There must be exactly one blank line before the tags in a doc comment
Loading history...
493
	 */
494
	public static function build() {
495
		global $wpdb;
496
		
497
		$property_sql = array();
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
498
		foreach ( static::properties() as $property )
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
499
			$property_sql[] = "`{$property['name']}` {$property['type']}";
500
		
501
		$sql = 'CREATE TABLE IF NOT EXISTS '
502
		     . static::table_name()
0 ignored issues
show
Found precision alignment of 1 spaces.
Loading history...
503
		     . ' ('
0 ignored issues
show
Found precision alignment of 1 spaces.
Loading history...
504
		     . implode( ',', $property_sql )
0 ignored issues
show
Found precision alignment of 1 spaces.
Loading history...
505
		     . ' ) CHARACTER SET utf8;'
0 ignored issues
show
Found precision alignment of 1 spaces.
Loading history...
506
		;
0 ignored issues
show
Space found before semicolon; expected "' ) CHARACTER SET utf8;';" but found "' ) CHARACTER SET utf8;'
;"
Loading history...
507
		
508
		$wpdb->query( $sql );
0 ignored issues
show
Usage of a direct database call is discouraged.
Loading history...
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
Use placeholders and $wpdb->prepare(); found $sql
Loading history...
509
510
		static::build_indices();
511
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
512
	
513
	/**
514
	 * Convention based index generation.
515
	 *
516
	 * Creates default indices for all columns matching both:
517
	 * - equals "id" or contains "_id"
518
	 * - doesn't have an index yet
519
	 */
520
	public static function build_indices() {
521
		global $wpdb;
522
523
		$indices_sql = 'SHOW INDEX FROM `' . static::table_name() . '`';
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
524
		$indices = $wpdb->get_results( $indices_sql );
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
Usage of a direct database call is discouraged.
Loading history...
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
Use placeholders and $wpdb->prepare(); found $indices_sql
Loading history...
525
		$index_columns = array_map( function($index){ return $index->Column_name; }, $indices );
0 ignored issues
show
Expected 1 spaces between opening bracket and argument "$index"; 0 found
Loading history...
No space after opening parenthesis is prohibited
Loading history...
No space before closing parenthesis is prohibited
Loading history...
Expected 1 space before opening brace; found 0
Loading history...
Space between opening control structure and closing parenthesis is required
Loading history...
Opening brace must be the last content on the line
Loading history...
Object property "Column_name" is not in valid snake_case format
Loading history...
Closing brace of nested function must be on a new line
Loading history...
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
526
527
		foreach ( static::properties() as $property ) {
528
529
			if ( $property['index'] && ! in_array( $property['name'], $index_columns ) ) {
0 ignored issues
show
Not using strict comparison for in_array; supply true for third argument.
Loading history...
530
				$length = isset($property['index_length']) ? '(' . (int) $property['index_length'] . ')' : '';
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
531
				$unique = isset($property['unique']) && $property['unique'] ? 'UNIQUE' : '';
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
532
				$sql = 'ALTER TABLE `' . static::table_name() . '` ADD ' . $unique . ' INDEX `' . $property['name'] . '` (' . $property['name'] . $length . ')';
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
533
				$wpdb->query( $sql );
0 ignored issues
show
Usage of a direct database call is discouraged.
Loading history...
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
Use placeholders and $wpdb->prepare(); found $sql
Loading history...
534
			}
535
		}
536
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
537
538
	/**
539
	 * Model identifier.
540
	 */
541
	public static function name() {
542
		// get name of implementing class
0 ignored issues
show
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
543
		$table_name = get_called_class();
544
		// replace backslashes from namespace by underscores
0 ignored issues
show
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
545
		$table_name = str_replace( '\\', '_', $table_name );
546
		// remove Models subnamespace from name
0 ignored issues
show
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
547
		$table_name = str_replace( 'Model_', '', $table_name );
548
		// all lowercase
0 ignored issues
show
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
549
		$table_name = strtolower( $table_name );
550
551
		return $table_name;
552
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
553
}