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.

Base::table_name()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
0 ignored issues
show
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...
Coding Style introduced by
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 doc comment for class Base
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 doc comment for function __set()
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
Coding Style introduced by
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 doc comment for function set_property()
Loading history...
Coding Style introduced by
Method name "Base::set_property" is not in camel caps format
Loading history...
26
		$this->data[ $name ] = $value;
27
	}
0 ignored issues
show
Coding Style introduced by
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 doc comment for function __get()
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
Coding Style introduced by
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 doc comment for function get_property()
Loading history...
Coding Style introduced by
Method name "Base::get_property" is not in camel caps format
Loading history...
40
		if ( isset( $this->data[ $name ] ) ) {
41
			return $this->data[ $name ];
42
		} else {
43
			return null;
44
		}
45
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
46
47
	private static function unserialize_property($property) {
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function unserialize_property()
Loading history...
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
Coding Style introduced by
Method name "Base::unserialize_property" is not in camel caps format
Loading history...
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
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...
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing parenthesis; 0 found
Loading history...
49
			return;
50
51
		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...
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing parenthesis; 0 found
Loading history...
52
			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...
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing parenthesis; 0 found
Loading history...
53
54
		return $property;
55
	}
0 ignored issues
show
Coding Style introduced by
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
Coding Style introduced by
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() {
0 ignored issues
show
Coding Style introduced by
Method name "Base::table_name" is not in camel caps format
Loading history...
67
		global $wpdb;
68
		
69
		// 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...
70
		return $wpdb->prefix . static::name();
71
	}
0 ignored issues
show
Coding Style introduced by
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
Coding Style introduced by
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
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...
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
introduced by
Found: ==. Use strict comparisons (=== or !==).
Loading history...
introduced by
Use Yoda Condition checks, you must.
Loading history...
91
		// 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...
92
		if (isset($args['index'])) {
0 ignored issues
show
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing parenthesis; 0 found
Loading history...
93
			$index = $args['index'];
94
		}
95
		
96
		static::$properties[ $class ][] = array(
97
			'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...
98
			'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...
99
			'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...
100
			'index_length' => isset($args['index_length']) ? $args['index_length'] : null,
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing parenthesis; 0 found
Loading history...
101
			'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
Each array item in a multi-line array declaration must end in a comma
Loading history...
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing parenthesis; 0 found
Loading history...
102
		);
103
	}
0 ignored issues
show
Coding Style introduced by
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
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...
114
			static::$properties[ $class ] = array();
115
		}
116
		
117
		return static::$properties[ $class ];
118
	}
0 ignored issues
show
Coding Style introduced by
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 ) {
0 ignored issues
show
Coding Style introduced by
Method name "Base::has_property" is not in camel caps format
Loading history...
127
		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...
128
	}
0 ignored issues
show
Coding Style introduced by
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() {
0 ignored issues
show
Coding Style introduced by
Method name "Base::property_names" is not in camel caps format
Loading history...
136
		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...
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...
Coding Style introduced by
Space found before comma in argument list
Loading history...
137
	}
0 ignored issues
show
Coding Style introduced by
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() {
0 ignored issues
show
Coding Style introduced by
Method name "Base::has_entries" is not in camel caps format
Loading history...
145
		return static::count() > 0;
146
	}
0 ignored issues
show
Coding Style introduced by
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
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...
158
	}
0 ignored issues
show
Coding Style introduced by
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 doc comment for function find_by_id()
Loading history...
Coding Style introduced by
Method name "Base::find_by_id" is not in camel caps format
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
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...
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
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing parenthesis; 0 found
Loading history...
175
		}
176
		
177
		return $model;
178
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
179
180
	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...
181
		global $wpdb;
182
		
183
		$class = get_called_class();
184
		$model = new $class();
185
		$model->flag_as_not_new();
186
		
187
		$query = $wpdb->prepare('SELECT * FROM ' . static::table_name() . ' WHERE ' . $property .  ' = \'%s\' LIMIT 0,1', $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...
Coding Style introduced by
Concat operator must be surrounded by a single space
Loading history...
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing parenthesis; 0 found
Loading history...
introduced by
Simple placeholders should not be quoted in the query string in $wpdb->prepare(). Found: \'%s\'.
Loading history...
188
		$row = $wpdb->get_row($query);
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...
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...
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing parenthesis; 0 found
Loading history...
introduced by
Use placeholders and $wpdb->prepare(); found $query
Loading history...
189
		
190
		if ( ! $row ) {
191
			return null;
192
		}
193
		
194
		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...
195
			$model->$property = static::unserialize_property($value);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing parenthesis; 0 found
Loading history...
196
		}
197
		
198
		return $model;
199
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
200
	
201
	/**
202
	 * Retrieve all entries from the table.
203
	 *
204
	 * @return array list of model objects
205
	 */
206
	public static function all() {
207
		global $wpdb;
208
		
209
		$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...
210
		$models = array();
211
		
212
		$rows = $wpdb->get_results( 'SELECT * FROM ' . 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
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...
213
214
		foreach ( $rows as $row ) {
215
			$model = new $class();
216
			$model->flag_as_not_new();
217
			foreach ( $row as $property => $value ) {
218
				$model->$property = static::unserialize_property($value);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing parenthesis; 0 found
Loading history...
219
			}
220
			$models[] = $model;
221
		}
222
		
223
		return $models;
224
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
225
	
226
	/**
227
	 * True if not yet saved to database. Else false.
228
	 */
229
	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...
230
		return $this->is_new;
231
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
232
	
233
	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...
234
		$this->is_new = false;
235
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
236
237
	/**
238
	 * Rails-ish update_attributes for easy form handling.
239
	 *
240
	 * Takes an array of form values and takes care of serializing it.
241
	 * 
242
	 * @param  array $attributes
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
243
	 * @return bool
244
	 */
245
	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...
246
247
		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...
248
			return false;
249
250
		$request = filter_input_array(INPUT_POST); // Do this for security reasons
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing parenthesis; 0 found
Loading history...
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
251
			
252
		foreach ( $attributes as $key => $value ) {
253
			if ( is_array($value) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing parenthesis; 0 found
Loading history...
254
				$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...
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing parenthesis; 0 found
Loading history...
255
			} else {
256
				$this->{$key} = esc_sql($value);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing parenthesis; 0 found
Loading history...
257
			}
258
		}
259
		
260
		if ( isset( $request['checkboxes'] ) && is_array( $request['checkboxes'] ) ) {
261
			foreach ( $request['checkboxes'] as $checkbox ) {
262
				if ( isset( $attributes[ $checkbox ] ) && $attributes[ $checkbox ] === 'on' ) {
0 ignored issues
show
introduced by
Use Yoda Condition checks, you must.
Loading history...
263
					$this->$checkbox = 1;
264
				} else {
265
					$this->$checkbox = 0;
266
				}
267
			}
268
		}
269
270
		// @todo this is the wrong place to do this!
271
		// The feed password is the only "passphrase" which is saved. It is not encrypted!
272
		// However, we keep this function for later use
273
		if ( isset( $request['passwords'] ) && is_array( $request['passwords'] ) ) {
274
			foreach ( $request['passwords'] as $password ) {
275
				$this->$password = $attributes[ $password ];
276
			}
277
		}
278
		return $this->save();
279
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
280
281
	/**
282
	 * Update and save a single attribute.
283
	 * 	
0 ignored issues
show
introduced by
Spaces must be used for mid-line alignment; tabs are not allowed
Loading history...
284
	 * @param  string $attribute attribute name
0 ignored issues
show
Coding Style Documentation introduced by
Parameter comment must end with a full stop
Loading history...
285
	 * @param  mixed  $value
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
286
	 * @return (bool) query success
287
	 */
288
	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...
introduced by
No space after opening parenthesis is prohibited
Loading history...
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
289
		global $wpdb;
290
291
		$this->$attribute = $value;
292
293
		$sql = sprintf(
294
			"UPDATE %s SET %s = '%s' WHERE id = %s",
295
			static::table_name(),
296
			$attribute,
297
			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 string. ( Ignorable by Annotation )

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

297
			/** @scrutinizer ignore-call */ 
298
   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...
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing parenthesis; 0 found
Loading history...
298
			$this->id
299
		);
300
301
		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...
302
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
303
	
304
	/**
305
	 * Saves changes to database.
306
	 * 
307
	 * @todo use wpdb::insert()
308
	 */
309
	public function save() {
310
		global $wpdb;
311
312
		if ( $this->is_new() ) {
313
314
			$this->set_defaults();
315
316
			$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...
317
			     . static::table_name()
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
318
			     . ' ( '
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
319
			     . implode( ',', static::property_names() )
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
320
			     . ' ) '
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
321
			     . 'VALUES'
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
322
			     . ' ( '
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
323
			     . 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...
324
			     . ' );'
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
325
			;
0 ignored issues
show
introduced by
Space found before semicolon; expected "' );';" but found "' );'\n ;"
Loading history...
326
			$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...
327
			if ( $success ) {
328
				$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...
329
			}
330
		} else {
331
			$sql = 'UPDATE ' . static::table_name()
332
			     . ' SET '
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
333
			     . 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...
334
			     . ' WHERE id = ' . $this->id
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
335
			;
0 ignored issues
show
introduced by
Space found before semicolon; expected "id;" but found "id\n ;"
Loading history...
336
337
			$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...
338
		}
339
340
		$this->is_new = false;
341
342
		do_action('podlove_model_save', $this);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing parenthesis; 0 found
Loading history...
343
		do_action('podlove_model_change', $this);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing parenthesis; 0 found
Loading history...
344
345
		return $success;
346
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
347
348
	/**
349
	 * Sets default values.
350
	 * 
351
	 * @return array
352
	 */
353
	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...
354
		
355
		$defaults = $this->default_values();
356
357
		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...
358
			return;
359
360
		foreach ( $defaults as $property => $value ) {
361
			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...
362
				$this->$property = $value;
363
		}
364
365
	}
366
367
	/**
368
	 * Return default values for properties.
369
	 * 
370
	 * Can be overridden by inheriting model classes.
371
	 * 
372
	 * @return array
373
	 */
374
	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...
375
		return array();
376
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
377
	
378
	public function delete() {
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function delete()
Loading history...
379
		global $wpdb;
380
		
381
		$sql = 'DELETE FROM '
382
		     . static::table_name()
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
383
		     . ' WHERE id = ' . $this->id;
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
384
385
		$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...
386
387
	    do_action('podlove_model_delete', $this);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing parenthesis; 0 found
Loading history...
388
	    do_action('podlove_model_change', $this);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing parenthesis; 0 found
Loading history...
389
390
		return $rows_affected !== false;
0 ignored issues
show
introduced by
Use Yoda Condition checks, you must.
Loading history...
391
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
392
393
	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...
394
		global $wpdb;
395
396
		if ( $this->$p !== null && $this->$p !== '' ) {
0 ignored issues
show
introduced by
Use Yoda Condition checks, you must.
Loading history...
397
			return sprintf( "%s = '%s'", $p, ( is_array($this->$p) ? serialize($this->$p) : $this->$p ) );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing parenthesis; 0 found
Loading history...
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...
398
		} else {
399
			return "$p = NULL";
400
		}
401
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
402
	
403
	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...
404
		global $wpdb;
405
406
		if ( $this->$p !== null && $this->$p !== '' ) {
0 ignored issues
show
introduced by
Use Yoda Condition checks, you must.
Loading history...
407
			return sprintf( "'%s'", $this->$p );
408
		} else {
409
			return 'NULL';
410
		}
411
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
412
	
413
	/**
414
	 * Create database table based on defined properties.
415
	 * 
416
	 * Automatically includes an id column as auto incrementing primary key.
417
	 * @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...
418
	 */
419
	public static function build() {
420
		global $wpdb;
421
		
422
		$property_sql = array();
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...
423
		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...
424
			$property_sql[] = "`{$property['name']}` {$property['type']}";
425
		
426
		$sql = 'CREATE TABLE IF NOT EXISTS '
427
		     . static::table_name()
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
428
		     . ' ('
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
429
		     . implode( ',', $property_sql )
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
430
		     . ' ) CHARACTER SET utf8;'
0 ignored issues
show
introduced by
Found precision alignment of 1 spaces.
Loading history...
431
		;
0 ignored issues
show
introduced by
Space found before semicolon; expected "' ) CHARACTER SET utf8;';" but found "' ) CHARACTER SET utf8;'\n ;"
Loading history...
432
		
433
		$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...
434
435
		static::build_indices();
436
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
437
	
438
	/**
439
	 * Convention based index generation.
440
	 *
441
	 * Creates default indices for all columns matching both:
442
	 * - equals "id" or contains "_id"
443
	 * - doesn't have an index yet
444
	 */
445
	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...
446
		global $wpdb;
447
448
		$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...
449
		$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...
450
		$index_columns = array_map( function($index){ return $index->Column_name; }, $indices );
0 ignored issues
show
introduced by
No space after opening parenthesis is prohibited
Loading history...
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
introduced by
Space between opening control structure and closing parenthesis is required
Loading history...
Coding Style introduced by
Opening brace must be the last content on the line
Loading history...
Coding Style introduced by
Expected 1 space before opening brace; found 0
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...
451
452
		foreach ( static::properties() as $property ) {
453
454
			if ( $property['index'] && ! in_array( $property['name'], $index_columns ) ) {
0 ignored issues
show
introduced by
Not using strict comparison for in_array; supply true for third argument.
Loading history...
455
				$length = isset($property['index_length']) ? '(' . (int) $property['index_length'] . ')' : '';
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing parenthesis; 0 found
Loading history...
456
				$unique = isset($property['unique']) && $property['unique'] ? 'UNIQUE' : '';
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening parenthesis; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing parenthesis; 0 found
Loading history...
457
				$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...
458
				$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...
459
			}
460
		}
461
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
462
463
	/**
464
	 * Model identifier.
465
	 */
466
	public static function name() {
467
		// 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...
468
		$table_name = get_called_class();
469
		// 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...
470
		$table_name = str_replace( '\\', '_', $table_name );
471
		// 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...
472
		$table_name = str_replace( 'Model_', '', $table_name );
473
		// all lowercase
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
474
		$table_name = strtolower( $table_name );
475
476
		return $table_name;
477
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
478
}