GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 70bbd7...41de00 )
by Christian
02:53
created

Base::destroy()   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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
0 ignored issues
show
introduced by
Filenames should be all lowercase with hyphens as word separators. Expected base.php, but found Base.php.
Loading history...
introduced by
Class file names should be based on the class name with "class-" prepended. Expected class-base.php, but found Base.php.
Loading history...
2
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
3
 * @author    Podlove <[email protected]>
4
 * @copyright Copyright (c) 2014-2018, Podlove
5
 * @license   https://github.com/podlove/podlove-subscribe-button-wp-plugin/blob/master/LICENSE MIT
6
 * @package   Podlove\PodloveSubscribeButton
7
 */
8
9
namespace PodloveSubscribeButton\Model;
10
11
abstract class Base {
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for class Base
Loading history...
12
	/**
13
	 * Property dictionary for all tables
14
	 */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @var tag in member variable comment
Loading history...
15
	private static $properties = array();
16
17
	private $is_new = true;
0 ignored issues
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
18
19
	/**
20
	 * Contains property values
21
	 */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @var tag in member variable comment
Loading history...
22
	private $data = array();
23
24
	public function __set( $name, $value ) {
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function __set()
Loading history...
25
		if ( static::has_property( $name ) ) {
26
			$this->set_property( $name, $value );
27
		} else {
28
			$this->$name = $value;
29
		}
30
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
31
32
	private function set_property( $name, $value ) {
0 ignored issues
show
Coding Style introduced by
Method name "Base::set_property" is not in camel caps format
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function set_property()
Loading history...
33
		$this->data[ $name ] = $value;
34
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
35
36
	public function __get( $name ) {
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function __get()
Loading history...
37
		if ( static::has_property( $name ) ) {
38
			return $this->get_property( $name );
39
		} elseif ( property_exists( $this, $name ) ) {
40
			return $this->$name;
41
		} else {
42
			return null;
43
		}
44
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
45
46
	private function get_property( $name ) {
0 ignored issues
show
Coding Style introduced by
Method name "Base::get_property" is not in camel caps format
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function get_property()
Loading history...
47
		if ( isset( $this->data[ $name ] ) ) {
48
			return $this->data[ $name ];
49
		} else {
50
			return null;
51
		}
52
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
53
54
	private static function unserialize_property( $property ) {
0 ignored issues
show
Coding Style introduced by
Method name "Base::unserialize_property" is not in camel caps format
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function unserialize_property()
Loading history...
55
		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...
56
			return;
57
58
		if ( $unserialized_string = is_serialized( $property ) )
0 ignored issues
show
Unused Code introduced by
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...
introduced by
Variable assignment found within a condition. Did you mean to do a comparison?
Loading history...
Coding Style introduced by
Assignments must be the first block of code on a line
Loading history...
59
			return unserialize( $property );
0 ignored issues
show
introduced by
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...
60
61
		return $property;
62
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
63
64
	/**
65
	 * Retrieves the database table name.
66
	 *
67
	 * The name is derived from the namespace an class name. Additionally, it
68
	 * is prefixed with the global WordPress database table prefix.
69
	 * @todo cache
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
70
	 *
71
	 * @return string database table name
72
	 */
73
	public static function table_name() {
0 ignored issues
show
Coding Style introduced by
Method name "Base::table_name" is not in camel caps format
Loading history...
74
		global $wpdb;
75
76
		// prefix with $wpdb prefix
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
77
		return $wpdb->prefix . static::name();
78
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
79
80
	/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$args" missing
Loading history...
81
	 * Define a property with name and type.
82
	 *
83
	 * Currently only supports basics.
84
	 * @todo enable additional options like NOT NULL, DEFAULT etc.
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
85
	 *
86
	 * @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...
87
	 * @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...
88
	 */
89
	public static function property( $name, $type, $args = array() ) {
90
		$class = get_called_class();
91
92
		if ( ! isset( static::$properties[ $class ] ) ) {
0 ignored issues
show
Bug introduced by
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...
93
			static::$properties[ $class ] = array();
94
		}
95
96
		// "id" columns and those ending on "_id" get an index by default
97
		$index = $name == 'id' || stripos( $name, '_id' );
0 ignored issues
show
introduced by
Found: ==. Use strict comparisons (=== or !==).
Loading history...
introduced by
Use Yoda Condition checks, you must.
Loading history...
98
		// but if the argument is set, it overrides the default
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
99
		if ( isset( $args[ 'index' ] ) ) {
0 ignored issues
show
introduced by
Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
100
			$index = $args[ 'index' ];
0 ignored issues
show
introduced by
Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
101
		}
102
103
		static::$properties[ $class ][ ] = array(
0 ignored issues
show
introduced by
Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
104
			'name'  => $name,
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 9 space(s) between "'name'" and double arrow, but found 2.
Loading history...
105
			'type'  => $type,
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 9 space(s) between "'type'" and double arrow, but found 2.
Loading history...
106
			'index' => $index,
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 8 space(s) between "'index'" and double arrow, but found 1.
Loading history...
107
			'index_length' => isset( $args[ 'index_length' ] ) ? $args[ 'index_length' ] : null,
0 ignored issues
show
introduced by
Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
108
			'unique' => isset( $args[ 'unique' ] ) ? $args[ 'unique' ] : null
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 7 space(s) between "'unique'" and double arrow, but found 1.
Loading history...
introduced by
Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
introduced by
Each array item in a multi-line array declaration must end in a comma
Loading history...
109
		);
110
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
111
112
	/**
113
	 * Return a list of property dictionaries.
114
	 *
115
	 * @return array property list
116
	 */
117
	private static function properties() {
118
		$class = get_called_class();
119
120
		if ( ! isset( static::$properties[ $class ] ) ) {
0 ignored issues
show
Bug introduced by
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...
121
			static::$properties[ $class ] = array();
122
		}
123
124
		return static::$properties[ $class ];
125
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
126
127
	/**
128
	 * Does the given property exist?
129
	 *
130
	 * @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...
131
	 * @return bool True if the property exists, else false.
132
	 */
133
	public static function has_property( $name ) {
0 ignored issues
show
Coding Style introduced by
Method name "Base::has_property" is not in camel caps format
Loading history...
134
		return in_array( $name, static::property_names() );
0 ignored issues
show
introduced by
Not using strict comparison for in_array; supply true for third argument.
Loading history...
135
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
136
137
	/**
138
	 * Return a list of property names.
139
	 *
140
	 * @return array property names
141
	 */
142
	public static function property_names() {
0 ignored issues
show
Coding Style introduced by
Method name "Base::property_names" is not in camel caps format
Loading history...
143
		return array_map( function( $p ) { return $p[ 'name' ]; } , static::properties() );
0 ignored issues
show
Coding Style introduced by
Opening brace must be the last content on the line
Loading history...
introduced by
Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
Coding Style introduced by
Closing brace of nested function must be on a new line
Loading history...
Coding Style introduced by
Space found before comma in function call
Loading history...
Coding Style introduced by
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...
144
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
145
146
	/**
147
	 * Does the table have any entries?
148
	 *
149
	 * @return bool True if there is at least one entry, else false.
150
	 */
151
	public static function has_entries() {
0 ignored issues
show
Coding Style introduced by
Method name "Base::has_entries" is not in camel caps format
Loading history...
152
		return static::count() > 0;
153
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
154
155
	/**
156
	 * Return number of rows in the table.
157
	 *
158
	 * @return int number of rows
159
	 */
160
	public static function count() {
161
		global $wpdb;
162
163
		$sql = 'SELECT COUNT(*) FROM ' . static::table_name();
164
		return (int) $wpdb->get_var( $sql );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found $sql
Loading history...
165
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
166
167
	public static function find_by_id( $id ) {
0 ignored issues
show
Coding Style introduced by
Method name "Base::find_by_id" is not in camel caps format
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function find_by_id()
Loading history...
168
		global $wpdb;
169
170
		$class = get_called_class();
171
		$model = new $class();
172
		$model->flag_as_not_new();
173
174
		$row = $wpdb->get_row( 'SELECT * FROM ' . static::table_name() . ' WHERE id = ' . (int) $id );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found static
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found ::
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found table_name
Loading history...
175
176
		if ( ! $row ) {
177
			return null;
178
		}
179
180
		foreach ( $row as $property => $value ) {
181
			$model->$property = static::unserialize_property( $value );
182
		}
183
184
		return $model;
185
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
186
187
	public static function find_all_by_property( $property, $value ) {
0 ignored issues
show
Coding Style introduced by
Method name "Base::find_all_by_property" is not in camel caps format
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function find_all_by_property()
Loading history...
188
		global $wpdb;
189
190
		$class = get_called_class();
0 ignored issues
show
Coding Style introduced by
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...
191
		$models = array();
192
193
		$rows = $wpdb->get_results(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
194
			'SELECT * FROM ' . static::table_name() . ' WHERE ' . $property . ' = \'' . $value . '\''
0 ignored issues
show
introduced by
Use placeholders and $wpdb->prepare(); found static
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found ::
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found table_name
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found $property
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found $value
Loading history...
195
		);
196
197
		if ( ! $rows ) {
198
			return array();
199
		}
200
201
		foreach ( $rows as $row ) {
202
			$model = new $class();
203
			$model->flag_as_not_new();
204
			foreach ( $row as $property => $value ) {
0 ignored issues
show
introduced by
$value is overwriting one of the parameters of this function.
Loading history...
introduced by
$property is overwriting one of the parameters of this function.
Loading history...
205
				$model->$property = static::unserialize_property( $value );
206
			}
207
			$models[ ] = $model;
0 ignored issues
show
introduced by
Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
208
		}
209
210
		return $models;
211
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
212
213
	public static function find_one_by_property( $property, $value ) {
0 ignored issues
show
Coding Style introduced by
Method name "Base::find_one_by_property" is not in camel caps format
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function find_one_by_property()
Loading history...
214
		global $wpdb;
215
216
		$class = get_called_class();
217
		$model = new $class();
218
		$model->flag_as_not_new();
219
220
		$row = $wpdb->get_row(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
221
			'SELECT * FROM ' . static::table_name() . ' WHERE ' . $property . ' = \'' . $value . '\' LIMIT 0,1'
0 ignored issues
show
introduced by
Use placeholders and $wpdb->prepare(); found static
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found ::
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found table_name
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found $property
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found $value
Loading history...
222
		);
223
224
		if ( ! $row ) {
225
			return null;
226
		}
227
228
		foreach ( $row as $property => $value ) {
0 ignored issues
show
introduced by
$property is overwriting one of the parameters of this function.
Loading history...
introduced by
$value is overwriting one of the parameters of this function.
Loading history...
229
			$model->$property = static::unserialize_property( $value );
230
		}
231
232
		return $model;
233
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
234
235
	public static function find_all_by_where( $where ) {
0 ignored issues
show
Coding Style introduced by
Method name "Base::find_all_by_where" is not in camel caps format
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function find_all_by_where()
Loading history...
236
		global $wpdb;
237
238
		$class = get_called_class();
0 ignored issues
show
Coding Style introduced by
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...
239
		$models = array();
240
241
		$rows = $wpdb->get_results(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
242
			'SELECT * FROM ' . static::table_name() . ' WHERE ' . $where
0 ignored issues
show
introduced by
Use placeholders and $wpdb->prepare(); found static
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found ::
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found table_name
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found $where
Loading history...
243
		);
244
245
		if ( ! $rows ) {
246
			return array();
247
		}
248
249
		foreach ( $rows as $row ) {
250
			$model = new $class();
251
			$model->flag_as_not_new();
252
			foreach ( $row as $property => $value ) {
253
				$model->$property = static::unserialize_property( $value );
254
			}
255
			$models[ ] = $model;
0 ignored issues
show
introduced by
Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
256
		}
257
258
		return $models;
259
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
260
261
	public static function find_one_by_where( $where ) {
0 ignored issues
show
Coding Style introduced by
Method name "Base::find_one_by_where" is not in camel caps format
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function find_one_by_where()
Loading history...
262
		global $wpdb;
263
264
		$class = get_called_class();
265
		$model = new $class();
266
		$model->flag_as_not_new();
267
268
		$row = $wpdb->get_row(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
269
			'SELECT * FROM ' . static::table_name() . ' WHERE ' . $where . ' LIMIT 0,1'
0 ignored issues
show
introduced by
Use placeholders and $wpdb->prepare(); found static
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found ::
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found table_name
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found $where
Loading history...
270
		);
271
272
		if ( ! $row ) {
273
			return null;
274
		}
275
276
		foreach ( $row as $property => $value ) {
277
			$model->$property = static::unserialize_property( $value );
278
		}
279
280
		return $model;
281
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
282
	/**
283
	 * Retrieve all entries from the table.
284
	 *
285
	 * @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...
286
	 * @return array list of Model objects
287
	 */
288
	public static function all( $sql_suffix = '' ) {
289
		global $wpdb;
290
291
		$class = get_called_class();
0 ignored issues
show
Coding Style introduced by
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...
292
		$models = array();
293
294
		$rows = $wpdb->get_results( 'SELECT * FROM ' . static::table_name() . ' ' . $sql_suffix );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found static
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found ::
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found table_name
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found $sql_suffix
Loading history...
295
296
		foreach ( $rows as $row ) {
297
			$model = new $class();
298
			$model->flag_as_not_new();
299
			foreach ( $row as $property => $value ) {
300
				$model->$property = static::unserialize_property( $value );
301
			}
302
			$models[ ] = $model;
0 ignored issues
show
introduced by
Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
303
		}
304
305
		return $models;
306
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
307
308
	/**
309
	 * True if not yet saved to database. Else false.
310
	 */
311
	public function is_new() {
0 ignored issues
show
Coding Style introduced by
Method name "Base::is_new" is not in camel caps format
Loading history...
312
		return $this->is_new;
313
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
314
315
	public function flag_as_not_new() {
0 ignored issues
show
Coding Style introduced by
Method name "Base::flag_as_not_new" is not in camel caps format
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function flag_as_not_new()
Loading history...
316
		$this->is_new = false;
317
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
318
319
	/**
320
	 * Rails-ish update_attributes for easy form handling.
321
	 *
322
	 * Takes an array of form values and takes care of serializing it.
323
	 *
324
	 * @param  array $attributes
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
325
	 * @return bool
326
	 */
327
	public function update_attributes( $attributes ) {
0 ignored issues
show
Coding Style introduced by
Method name "Base::update_attributes" is not in camel caps format
Loading history...
328
329
		if ( ! is_array( $attributes ) )
0 ignored issues
show
introduced by
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...
330
			return false;
331
332
		$request = filter_input_array( INPUT_POST ); // Do this for security reasons
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
333
334
		foreach ( $attributes as $key => $value ) {
335
			if ( is_array( $value ) ) {
336
				$this->{$key} = serialize( $value );
0 ignored issues
show
introduced by
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...
337
			} else {
338
				$this->{$key} = esc_sql( $value );
339
			}
340
		}
341
342
		if ( isset( $request[ 'checkboxes' ] ) && is_array( $request[ 'checkboxes' ] ) ) {
0 ignored issues
show
introduced by
Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
343
			foreach ( $request[ 'checkboxes' ] as $checkbox ) {
0 ignored issues
show
introduced by
Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
344
				if ( isset( $attributes[ $checkbox ] ) && $attributes[ $checkbox ] === 'on' ) {
0 ignored issues
show
introduced by
Use Yoda Condition checks, you must.
Loading history...
345
					$this->$checkbox = 1;
346
				} else {
347
					$this->$checkbox = 0;
348
				}
349
			}
350
		}
351
352
		// @todo this is the wrong place to do this!
353
		// The feed password is the only "passphrase" which is saved. It is not encrypted!
354
		// However, we keep this function for later use
355
		if ( isset( $request[ 'passwords' ] ) && is_array( $request[ 'passwords' ] ) ) {
0 ignored issues
show
introduced by
Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
356
			foreach ( $request[ 'passwords' ] as $password ) {
0 ignored issues
show
introduced by
Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
357
				$this->$password = $attributes[ $password ];
358
			}
359
		}
360
		return $this->save();
361
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
362
363
	/**
364
	 * Update and save a single attribute.
365
	 *
366
	 * @param  string $attribute attribute name
0 ignored issues
show
Coding Style Documentation introduced by
Parameter comment must end with a full stop
Loading history...
367
	 * @param  mixed  $value
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
368
	 * @return (bool) query success
369
	 */
370
	public function update_attribute( $attribute, $value ) {
0 ignored issues
show
Coding Style introduced by
Method name "Base::update_attribute" is not in camel caps format
Loading history...
371
		global $wpdb;
372
373
		$this->$attribute = $value;
374
375
		$sql = sprintf(
376
			"UPDATE %s SET %s = '%s' WHERE id = %s",
377
			static::table_name(),
378
			$attribute,
379
			mysqli_real_escape_string( $value ),
0 ignored issues
show
Bug introduced by
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

379
			/** @scrutinizer ignore-call */ 
380
   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...
introduced by
Accessing the database directly should be avoided. Please use the $wpdb object and associated functions instead. Found: mysqli_real_escape_string.
Loading history...
380
			$this->id
381
		);
382
383
		return $wpdb->query( $sql );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found $sql
Loading history...
384
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
385
386
	/**
387
	 * Saves changes to database.
388
	 *
389
	 * @todo use wpdb::insert()
390
	 */
391
	public function save() {
392
		global $wpdb;
393
394
		if ( $this->is_new() ) {
395
396
			$this->set_defaults();
397
398
			$sql = 'INSERT INTO '
0 ignored issues
show
Coding Style introduced by
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...
399
			     . static::table_name()
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
400
			     . ' ( '
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
401
			     . implode( ',', static::property_names() )
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
402
			     . ' ) '
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
403
			     . 'VALUES'
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
404
			     . ' ( '
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
405
			     . implode( ',', array_map( array( $this, 'property_name_to_sql_value' ), static::property_names() ) )
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
406
			     . ' );'
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
407
			;
0 ignored issues
show
introduced by
Space found before semicolon; expected "' );';" but found "' );'
;"
Loading history...
408
			$success = $wpdb->query( $sql );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found $sql
Loading history...
409
			if ( $success ) {
410
				$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...
411
			}
412
		} else {
413
			$sql = 'UPDATE ' . static::table_name()
414
			     . ' SET '
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
415
			     . implode( ',', array_map( array( $this, 'property_name_to_sql_update_statement' ), static::property_names() ) )
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
416
			     . ' WHERE id = ' . $this->id
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
417
			;
0 ignored issues
show
introduced by
Space found before semicolon; expected "id;" but found "id
;"
Loading history...
418
419
			$success = $wpdb->query( $sql );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found $sql
Loading history...
420
		}
421
422
		$this->is_new = false;
423
424
		do_action( 'podlove_model_save', $this );
425
		do_action( 'podlove_model_change', $this );
426
427
		return $success;
428
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
429
430
	/**
431
	 * Sets default values.
432
	 *
433
	 * @return array
434
	 */
435
	private function set_defaults() {
0 ignored issues
show
Coding Style introduced by
Method name "Base::set_defaults" is not in camel caps format
Loading history...
436
437
		$defaults = $this->default_values();
438
439
		if ( ! is_array( $defaults ) || empty( $defaults ) )
0 ignored issues
show
introduced by
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...
440
			return;
441
442
		foreach ( $defaults as $property => $value ) {
443
			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...
introduced by
Use Yoda Condition checks, you must.
Loading history...
444
				$this->$property = $value;
445
		}
446
447
	}
448
449
	/**
450
	 * Return default values for properties.
451
	 *
452
	 * Can be overridden by inheriting Model classes.
453
	 *
454
	 * @return array
455
	 */
456
	public function default_values() {
0 ignored issues
show
Coding Style introduced by
Method name "Base::default_values" is not in camel caps format
Loading history...
457
		return array();
458
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
459
460
	public function delete() {
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function delete()
Loading history...
461
		global $wpdb;
462
463
		$sql = 'DELETE FROM '
464
		     . static::table_name()
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
465
		     . ' WHERE id = ' . $this->id;
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
466
467
		$rows_affected = $wpdb->query( $sql );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found $sql
Loading history...
468
469
	    do_action( 'podlove_model_delete', $this );
470
	    do_action( 'podlove_model_change', $this );
471
472
		return $rows_affected !== false;
0 ignored issues
show
introduced by
Use Yoda Condition checks, you must.
Loading history...
473
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
474
475
	private function property_name_to_sql_update_statement( $p ) {
0 ignored issues
show
Coding Style introduced by
Method name "Base::property_name_to_sql_update_statement" is not in camel caps format
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function property_name_to_sql_update_statement()
Loading history...
476
		global $wpdb;
477
478
		if ( $this->$p !== null && $this->$p !== '' ) {
0 ignored issues
show
introduced by
Use Yoda Condition checks, you must.
Loading history...
479
			return sprintf( "%s = '%s'", $p, ( is_array( $this->$p ) ? serialize( $this->$p ) : $this->$p ) );
0 ignored issues
show
introduced by
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...
480
		} else {
481
			return "$p = NULL";
482
		}
483
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
484
485
	private function property_name_to_sql_value( $p ) {
0 ignored issues
show
Coding Style introduced by
Method name "Base::property_name_to_sql_value" is not in camel caps format
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function property_name_to_sql_value()
Loading history...
486
		global $wpdb;
487
488
		if ( $this->$p !== null && $this->$p !== '' ) {
0 ignored issues
show
introduced by
Use Yoda Condition checks, you must.
Loading history...
489
			return sprintf( "'%s'", $this->$p );
490
		} else {
491
			return 'NULL';
492
		}
493
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
494
495
	/**
496
	 * Create database table based on defined properties.
497
	 *
498
	 * Automatically includes an id column as auto incrementing primary key.
499
	 * @todo allow Model changes
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
500
	 */
501
	public static function build() {
502
		global $wpdb;
503
504
		$property_sql = array();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 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...
505
		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...
506
			$property_sql[ ] = "`{$property[ 'name' ]}` {$property[ 'type' ]}";
0 ignored issues
show
introduced by
Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
507
508
		$sql = 'CREATE TABLE IF NOT EXISTS '
509
		     . static::table_name()
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
510
		     . ' ('
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
511
		     . implode( ',', $property_sql )
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
512
		     . ' ) CHARACTER SET utf8;'
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
513
		;
0 ignored issues
show
introduced by
Space found before semicolon; expected "' ) CHARACTER SET utf8;';" but found "' ) CHARACTER SET utf8;'
;"
Loading history...
514
515
		$wpdb->query( $sql );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found $sql
Loading history...
516
517
		static::build_indices();
518
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
519
520
	/**
521
	 * Convention based index generation.
522
	 *
523
	 * Creates default indices for all columns matching both:
524
	 * - equals "id" or contains "_id"
525
	 * - doesn't have an index yet
526
	 */
527
	public static function build_indices() {
0 ignored issues
show
Coding Style introduced by
Method name "Base::build_indices" is not in camel caps format
Loading history...
528
		global $wpdb;
529
530
		$indices_sql = 'SHOW INDEX FROM `' . static::table_name() . '`';
0 ignored issues
show
Coding Style introduced by
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...
531
		$indices = $wpdb->get_results( $indices_sql );
0 ignored issues
show
Coding Style introduced by
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...
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found $indices_sql
Loading history...
532
		$index_columns = array_map( function( $index ) { return $index->Column_name; }, $indices );
0 ignored issues
show
Coding Style introduced by
Opening brace must be the last content on the line
Loading history...
introduced by
Object property "Column_name" is not in valid snake_case format
Loading history...
Coding Style introduced by
Closing brace of nested function must be on a new line
Loading history...
Coding Style introduced by
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...
533
534
		foreach ( static::properties() as $property ) {
535
536
			if ( $property[ 'index' ] && ! in_array( $property[ 'name' ], $index_columns ) ) {
0 ignored issues
show
introduced by
Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
introduced by
Not using strict comparison for in_array; supply true for third argument.
Loading history...
537
				$length = isset( $property[ 'index_length' ] ) ? '(' . (int) $property[ 'index_length' ] . ')' : '';
0 ignored issues
show
introduced by
Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
538
				$unique = isset( $property[ 'unique' ] ) && $property[ 'unique' ] ? 'UNIQUE' : '';
0 ignored issues
show
introduced by
Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
539
				$sql = 'ALTER TABLE `' . static::table_name() . '` ADD ' . $unique . ' INDEX `' . $property[ 'name' ] . '` (' . $property[ 'name' ] . $length . ')';
0 ignored issues
show
Coding Style introduced by
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...
introduced by
Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
540
				$wpdb->query( $sql );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found $sql
Loading history...
541
			}
542
		}
543
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
544
545
	/**
546
	 * Model identifier.
547
	 */
548
	public static function name() {
549
		// get name of implementing class
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
550
		$table_name = get_called_class();
551
		// replace backslashes from namespace by underscores
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
552
		$table_name = str_replace( '\\', '_', $table_name );
553
		// remove Models subnamespace from name
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
554
		$table_name = str_replace( 'Model_', '', $table_name );
555
		// all lowercase
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
556
		$table_name = strtolower( $table_name );
557
558
		return $table_name;
559
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
560
561
	public static function destroy() {
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function destroy()
Loading history...
562
		global $wpdb;
563
		$wpdb->query( 'DROP TABLE ' . static::table_name() );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
Loading history...
introduced by
Attempting a database schema change is discouraged.
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found static
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found ::
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found table_name
Loading history...
564
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
565
}