Passed
Push — master ( 36e64a...0adb6c )
by Richard
01:36
created
maphper/datasource/database.php 3 patches
Indentation   +48 added lines, -48 removed lines patch added patch discarded remove patch
@@ -6,7 +6,7 @@  discard block
 block discarded – undo
6 6
 	const EDIT_OPTIMISE = 4;
7 7
 
8 8
 	private $table;
9
-    private $options;
9
+	private $options;
10 10
 	private $cache = [];
11 11
 	private $primaryKey;
12 12
 	private $fields = '*';
@@ -15,7 +15,7 @@  discard block
 block discarded – undo
15 15
 	private $alterDb = false;
16 16
 	private $adapter;
17 17
 	private $crudBuilder;
18
-    private $selectBuilder;
18
+	private $selectBuilder;
19 19
 
20 20
 	public function __construct($db, $table, $primaryKey = 'id', array $options = []) {
21 21
 		$this->options = new DatabaseOptions($db, $options);
@@ -36,9 +36,9 @@  discard block
 block discarded – undo
36 36
 		$this->optimizeColumns();
37 37
 	}
38 38
 
39
-    private function optimizeColumns() {
40
-        if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table);
41
-    }
39
+	private function optimizeColumns() {
40
+		if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table);
41
+	}
42 42
 
43 43
 	public function getPrimaryKey() {
44 44
 		return $this->primaryKey;
@@ -80,15 +80,15 @@  discard block
 block discarded – undo
80 80
 		}
81 81
 	}
82 82
 
83
-    private function determineAggregateResult($result, $group) {
84
-        if (isset($result[0]) && $group == null) return $result[0]->val;
85
-        else if ($group != null) {
86
-            $ret = [];
87
-            foreach ($result as $res) $ret[$res->$field] = $res->val;
88
-            return $ret;
89
-        }
90
-        else return 0;
91
-    }
83
+	private function determineAggregateResult($result, $group) {
84
+		if (isset($result[0]) && $group == null) return $result[0]->val;
85
+		else if ($group != null) {
86
+			$ret = [];
87
+			foreach ($result as $res) $ret[$res->$field] = $res->val;
88
+			return $ret;
89
+		}
90
+		else return 0;
91
+	}
92 92
 
93 93
 	private function addIndex($args) {
94 94
 		if (self::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args);
@@ -122,7 +122,7 @@  discard block
 block discarded – undo
122 122
 		else $limit = '';
123 123
 
124 124
 		$query = $this->selectBuilder->createSql($fields, $mode);
125
-        $query['sql'] = array_filter($query['sql']);
125
+		$query['sql'] = array_filter($query['sql']);
126 126
 		$this->adapter->query($this->crudBuilder->delete($this->table, $query['sql'], $query['args'], $limit));
127 127
 		$this->addIndex(array_keys($query['args']));
128 128
 
@@ -131,22 +131,22 @@  discard block
 block discarded – undo
131 131
 		$this->resultCache = [];
132 132
 	}
133 133
 
134
-    private function getIfNew($data) {
135
-        $new = false;
136
-        foreach ($this->primaryKey as $k) {
137
-            if (empty($data->$k)) {
138
-                $data->$k = null;
139
-                $new = true;
140
-            }
141
-        }
142
-        return $new;
143
-    }
134
+	private function getIfNew($data) {
135
+		$new = false;
136
+		foreach ($this->primaryKey as $k) {
137
+			if (empty($data->$k)) {
138
+				$data->$k = null;
139
+				$new = true;
140
+			}
141
+		}
142
+		return $new;
143
+	}
144 144
 
145 145
 	public function save($data, $tryagain = true) {
146
-        $new = $this->getIfNew($data);
146
+		$new = $this->getIfNew($data);
147 147
 
148 148
 		try {
149
-            $result = $this->insert($this->table, $this->primaryKey, $data);
149
+			$result = $this->insert($this->table, $this->primaryKey, $data);
150 150
 
151 151
 			//If there was an error but PDO is silent, trigger the catch block anyway
152 152
 			if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table);
@@ -164,26 +164,26 @@  discard block
 block discarded – undo
164 164
 		$this->updateCache($data);
165 165
 	}
166 166
 
167
-    private function getTryAgain($tryagain) {
168
-        return $tryagain && self::EDIT_STRUCTURE & $this->alterDb;
169
-    }
167
+	private function getTryAgain($tryagain) {
168
+		return $tryagain && self::EDIT_STRUCTURE & $this->alterDb;
169
+	}
170 170
 
171
-    private function updatePK($data, $new) {
172
-        if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId();
173
-    }
171
+	private function updatePK($data, $new) {
172
+		if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId();
173
+	}
174 174
 
175
-    private function checkIfUpdateWorked($data) {
176
-        $updateWhere = $this->crudBuilder->update($this->table, $this->primaryKey, $data);
177
-        $matched = $this->findByField($updateWhere->getArgs());
175
+	private function checkIfUpdateWorked($data) {
176
+		$updateWhere = $this->crudBuilder->update($this->table, $this->primaryKey, $data);
177
+		$matched = $this->findByField($updateWhere->getArgs());
178 178
 
179
-        if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints');
180
-    }
179
+		if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints');
180
+	}
181 181
 
182
-    private function updateCache($data) {
183
-        $pkValue = $data->{$this->primaryKey[0]};
182
+	private function updateCache($data) {
183
+		$pkValue = $data->{$this->primaryKey[0]};
184 184
 		if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data);
185 185
 		else $this->cache[$pkValue] = $data;
186
-    }
186
+	}
187 187
 
188 188
 	private function insert($table, array $primaryKey, $data) {
189 189
 		$error = 0;
@@ -195,16 +195,16 @@  discard block
 block discarded – undo
195 195
 		}
196 196
 
197 197
  		if ($error || $result->errorCode() !== '00000') {
198
-            $result = $this->tryUpdate($table, $primaryKey, $data);
199
-        }
198
+			$result = $this->tryUpdate($table, $primaryKey, $data);
199
+		}
200 200
 
201 201
 		return $result;
202 202
 	}
203 203
 
204
-    private function tryUpdate($table, array $primaryKey, $data) {
205
-        $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data));
206
-        if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data);
204
+	private function tryUpdate($table, array $primaryKey, $data) {
205
+		$result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data));
206
+		if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data);
207 207
 
208
-        return $result;
209
-    }
208
+		return $result;
209
+	}
210 210
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -27,9 +27,9 @@  discard block
 block discarded – undo
27 27
 		$this->crudBuilder = new \Maphper\Lib\CrudBuilder();
28 28
 		$this->selectBuilder = new \Maphper\Lib\SelectBuilder();
29 29
 
30
-		$this->fields = implode(',', array_map([$this->adapter, 'quote'], (array) $this->options->read('fields')));
30
+		$this->fields = implode(',', array_map([$this->adapter, 'quote'], (array)$this->options->read('fields')));
31 31
 
32
-		$this->defaultSort = $this->options->read('defaultSort') !== false ? $this->options->read('defaultSort')  : implode(', ', $this->primaryKey);
32
+		$this->defaultSort = $this->options->read('defaultSort') !== false ? $this->options->read('defaultSort') : implode(', ', $this->primaryKey);
33 33
 
34 34
 		$this->alterDb = $this->options->getEditMode();
35 35
 
@@ -37,7 +37,7 @@  discard block
 block discarded – undo
37 37
 	}
38 38
 
39 39
     private function optimizeColumns() {
40
-        if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table);
40
+        if (self::EDIT_OPTIMISE & $this->alterDb && rand(0, 500) == 1) $this->adapter->optimiseColumns($this->table);
41 41
     }
42 42
 
43 43
 	public function getPrimaryKey() {
@@ -181,7 +181,7 @@  discard block
 block discarded – undo
181 181
 
182 182
     private function updateCache($data) {
183 183
         $pkValue = $data->{$this->primaryKey[0]};
184
-		if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data);
184
+		if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object)array_merge((array)$this->cache[$pkValue], (array)$data);
185 185
 		else $this->cache[$pkValue] = $data;
186 186
     }
187 187
 
Please login to merge, or discard this patch.
Braces   +58 added lines, -30 removed lines patch added patch discarded remove patch
@@ -37,7 +37,9 @@  discard block
 block discarded – undo
37 37
 	}
38 38
 
39 39
     private function optimizeColumns() {
40
-        if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table);
40
+        if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) {
41
+        	$this->adapter->optimiseColumns($this->table);
42
+        }
41 43
     }
42 44
 
43 45
 	public function getPrimaryKey() {
@@ -53,19 +55,23 @@  discard block
 block discarded – undo
53 55
 		if (!isset($this->cache[$id])) {
54 56
 			try {
55 57
 				$result = $this->adapter->query($this->selectBuilder->select($this->table, [$this->getPrimaryKey()[0] . ' = :id'], [':id' => $id], ['limit' => 1]));
56
-			}
57
-			catch (\Exception $e) {
58
+			} catch (\Exception $e) {
58 59
 			}
59 60
 
60
-			if (isset($result[0])) 	$this->cache[$id] = $result[0];
61
-			else return null;
61
+			if (isset($result[0])) {
62
+				$this->cache[$id] = $result[0];
63
+			} else {
64
+				return null;
65
+			}
62 66
 		}
63 67
 		return $this->cache[$id];
64 68
 	}
65 69
 
66 70
 	public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) {
67 71
 		//Cannot count/sum/max multiple fields, pick the first one. This should only come into play when trying to count() a mapper with multiple primary keys
68
-		if (is_array($field)) $field = $field[0];
72
+		if (is_array($field)) {
73
+			$field = $field[0];
74
+		}
69 75
 		$query = $this->selectBuilder->createSql($criteria, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND);
70 76
 
71 77
 		try {
@@ -74,24 +80,29 @@  discard block
 block discarded – undo
74 80
 			$result = $this->adapter->query($this->selectBuilder->aggregate($this->table, $function, $field, $query['sql'], $query['args'], $group));
75 81
 
76 82
 			return $this->determineAggregateResult($result, $group);
77
-		}
78
-		catch (\Exception $e) {
83
+		} catch (\Exception $e) {
79 84
 			return $group ? [] : 0;
80 85
 		}
81 86
 	}
82 87
 
83 88
     private function determineAggregateResult($result, $group) {
84
-        if (isset($result[0]) && $group == null) return $result[0]->val;
85
-        else if ($group != null) {
89
+        if (isset($result[0]) && $group == null) {
90
+        	return $result[0]->val;
91
+        } else if ($group != null) {
86 92
             $ret = [];
87
-            foreach ($result as $res) $ret[$res->$field] = $res->val;
93
+            foreach ($result as $res) {
94
+            	$ret[$res->$field] = $res->val;
95
+            }
88 96
             return $ret;
97
+        } else {
98
+        	return 0;
89 99
         }
90
-        else return 0;
91 100
     }
92 101
 
93 102
 	private function addIndex($args) {
94
-		if (self::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args);
103
+		if (self::EDIT_INDEX & $this->alterDb) {
104
+			$this->adapter->addIndex($this->table, $args);
105
+		}
95 106
 	}
96 107
 
97 108
 	public function findByField(array $fields, $options = []) {
@@ -99,7 +110,9 @@  discard block
 block discarded – undo
99 110
 		if (!isset($this->resultCache[$cacheId])) {
100 111
 			$query = $this->selectBuilder->createSql($fields, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND);
101 112
 
102
-			if (!isset($options['order'])) $options['order'] = $this->defaultSort;
113
+			if (!isset($options['order'])) {
114
+				$options['order'] = $this->defaultSort;
115
+			}
103 116
 
104 117
 			$query['sql'] = array_filter($query['sql']);
105 118
 
@@ -107,8 +120,7 @@  discard block
 block discarded – undo
107 120
 				$this->resultCache[$cacheId] = $this->adapter->query($this->selectBuilder->select($this->table, $query['sql'], $query['args'], $options));
108 121
 				$this->addIndex(array_keys($query['args']));
109 122
 				$this->addIndex(explode(',', $options['order']));
110
-			}
111
-			catch (\Exception $e) {
123
+			} catch (\Exception $e) {
112 124
 				$this->errors[] = $e;
113 125
 				$this->resultCache[$cacheId] = [];
114 126
 			}
@@ -117,9 +129,14 @@  discard block
 block discarded – undo
117 129
 	}
118 130
 
119 131
 	public function deleteByField(array $fields, array $options = [], $mode = null) {
120
-		if ($mode == null) $mode = \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND;
121
-		if (isset($options['limit']) != null) $limit = ' LIMIT ' . $options['limit'];
122
-		else $limit = '';
132
+		if ($mode == null) {
133
+			$mode = \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND;
134
+		}
135
+		if (isset($options['limit']) != null) {
136
+			$limit = ' LIMIT ' . $options['limit'];
137
+		} else {
138
+			$limit = '';
139
+		}
123 140
 
124 141
 		$query = $this->selectBuilder->createSql($fields, $mode);
125 142
         $query['sql'] = array_filter($query['sql']);
@@ -149,10 +166,13 @@  discard block
 block discarded – undo
149 166
             $result = $this->insert($this->table, $this->primaryKey, $data);
150 167
 
151 168
 			//If there was an error but PDO is silent, trigger the catch block anyway
152
-			if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table);
153
-		}
154
-		catch (\Exception $e) {
155
-			if (!$this->getTryAgain($tryagain)) throw $e;
169
+			if ($result->errorCode() !== '00000') {
170
+				throw new \Exception('Could not insert into ' . $this->table);
171
+			}
172
+		} catch (\Exception $e) {
173
+			if (!$this->getTryAgain($tryagain)) {
174
+				throw $e;
175
+			}
156 176
 
157 177
 			$this->adapter->alterDatabase($this->table, $this->primaryKey, $data);
158 178
 			$this->save($data, false);
@@ -169,28 +189,34 @@  discard block
 block discarded – undo
169 189
     }
170 190
 
171 191
     private function updatePK($data, $new) {
172
-        if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId();
192
+        if ($new && count($this->primaryKey) == 1) {
193
+        	$data->{$this->primaryKey[0]} = $this->adapter->lastInsertId();
194
+        }
173 195
     }
174 196
 
175 197
     private function checkIfUpdateWorked($data) {
176 198
         $updateWhere = $this->crudBuilder->update($this->table, $this->primaryKey, $data);
177 199
         $matched = $this->findByField($updateWhere->getArgs());
178 200
 
179
-        if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints');
201
+        if (count($matched) == 0) {
202
+        	throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints');
203
+        }
180 204
     }
181 205
 
182 206
     private function updateCache($data) {
183 207
         $pkValue = $data->{$this->primaryKey[0]};
184
-		if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data);
185
-		else $this->cache[$pkValue] = $data;
208
+		if (isset($this->cache[$pkValue])) {
209
+			$this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data);
210
+		} else {
211
+			$this->cache[$pkValue] = $data;
212
+		}
186 213
     }
187 214
 
188 215
 	private function insert($table, array $primaryKey, $data) {
189 216
 		$error = 0;
190 217
 		try {
191 218
 			$result = $this->adapter->query($this->crudBuilder->insert($table, $data));
192
-		}
193
-		catch (\Exception $e) {
219
+		} catch (\Exception $e) {
194 220
 			$error = 1;
195 221
 		}
196 222
 
@@ -203,7 +229,9 @@  discard block
 block discarded – undo
203 229
 
204 230
     private function tryUpdate($table, array $primaryKey, $data) {
205 231
         $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data));
206
-        if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data);
232
+        if ($result->rowCount() === 0) {
233
+        	$this->checkIfUpdateWorked($data);
234
+        }
207 235
 
208 236
         return $result;
209 237
     }
Please login to merge, or discard this patch.