Passed
Push — master ( 7f0dd2...2d27c8 )
by Richard
01:42
created
maphper/lib/selectbuilder.php 1 patch
Braces   +10 added lines, -4 removed lines patch added patch discarded remove patch
@@ -7,17 +7,23 @@
 block discarded – undo
7 7
 
8 8
 		if (isset($options['offset'])) {
9 9
 			$offset = ' OFFSET ' . $options['offset'];
10
-			if (!$limit) $limit = ' LIMIT  1000';
10
+			if (!$limit) {
11
+				$limit = ' LIMIT  1000';
12
+			}
13
+		} else {
14
+			$offset = '';
11 15
 		}
12
-		else $offset = '';
13 16
 
14 17
 		$order = isset($options['order']) ? ' ORDER BY ' . $options['order'] : '';
15 18
 		return new Query('SELECT * FROM ' . $table . ' ' . $where . $order . $limit . $offset, $args);
16 19
 	}
17 20
 
18 21
 	public function aggregate($table, $function, $field, $where, $args, $group) {
19
-		if ($group == true) $groupBy = ' GROUP BY ' . $field;
20
-		else $groupBy = '';
22
+		if ($group == true) {
23
+			$groupBy = ' GROUP BY ' . $field;
24
+		} else {
25
+			$groupBy = '';
26
+		}
21 27
 		return new Query('SELECT ' . $function . '(' . $field . ') as val, ' . $field . '   FROM ' . $table . ($where != null ? ' WHERE ' . $where : '') . ' ' . $groupBy, $args);
22 28
 	}
23 29
 }
Please login to merge, or discard this patch.
maphper/lib/Sql/WhereBuilder.php 1 patch
Braces   +51 added lines, -28 removed lines patch added patch discarded remove patch
@@ -9,71 +9,94 @@
 block discarded – undo
9 9
 
10 10
 		foreach ($fields as $key => $value) {
11 11
             if ($value instanceof \DateTime) {
12
-    			if ($value->format('H:i:s')  == '00:00:00') $value = $value->format('Y-m-d');
13
-    			else $value = $value->format('Y-m-d H:i:s');
12
+    			if ($value->format('H:i:s')  == '00:00:00') {
13
+    				$value = $value->format('Y-m-d');
14
+    			} else {
15
+    				$value = $value->format('Y-m-d H:i:s');
16
+    			}
14 17
     		}
15 18
 
16
-            if (is_object($value)) continue;
19
+            if (is_object($value)) {
20
+            	continue;
21
+            }
17 22
 			if (is_numeric($key) && is_array($value)) {
18 23
 				$result = $this->createSql($value, $key);
19
-				foreach ($result['args'] as $arg_key => $arg) $args[$arg_key] = $arg;
20
-				foreach ($result['sql'] as $arg) $sql[] = $arg;
21
-			}
22
-			else if (\Maphper\Maphper::FIND_BETWEEN & $mode) {
24
+				foreach ($result['args'] as $arg_key => $arg) {
25
+					$args[$arg_key] = $arg;
26
+				}
27
+				foreach ($result['sql'] as $arg) {
28
+					$sql[] = $arg;
29
+				}
30
+			} else if (\Maphper\Maphper::FIND_BETWEEN & $mode) {
23 31
 				$sql[] = $key . '>= :' . $key . 'from';
24 32
 				$sql[] = $key . ' <= :' . $key . 'to';
25 33
 
26 34
 				$args[$key . 'from'] = $value[0];
27 35
 				$args[$key . 'to'] = $value[1];
28
-			}
29
-			else if (!is_numeric($key) && is_array($value)) {
36
+			} else if (!is_numeric($key) && is_array($value)) {
30 37
 				$inSql = [];
31 38
 				$count = count($value);
32 39
 				for ($i = 0; $i < $count; $i++) {
33 40
 					$args[$key . $i] = $value[$i];
34 41
 					$inSql[] = ':' . $key . $i;
35 42
 				}
36
-				if (count($inSql) == 0) return [];
37
-				else $sql[] = $key . ' IN ( ' .  implode(', ', $inSql) . ')';
38
-			}
39
-			else if ($value === NULL) {
43
+				if (count($inSql) == 0) {
44
+					return [];
45
+				} else {
46
+					$sql[] = $key . ' IN ( ' .  implode(', ', $inSql) . ')';
47
+				}
48
+			} else if ($value === NULL) {
40 49
 				$nullSql = $key . ' IS ';
41
-				if (\Maphper\Maphper::FIND_NOT & $mode) $nullSql .= 'NOT ';
50
+				if (\Maphper\Maphper::FIND_NOT & $mode) {
51
+					$nullSql .= 'NOT ';
52
+				}
42 53
 				$sql[] = $nullSql . 'NULL';
43
-			}
44
-			else {
54
+			} else {
45 55
 
46 56
 				if (\Maphper\Maphper::FIND_LIKE & $mode) {
47 57
 					$operator = 'LIKE';
48 58
 					$value = '%' . $value . '%';
49
-				}
50
-				else if (\Maphper\Maphper::FIND_STARTS & $mode) {
59
+				} else if (\Maphper\Maphper::FIND_STARTS & $mode) {
51 60
 					$operator = 'LIKE';
52 61
 					$value = $value . '%';
62
+				} else {
63
+					$operator = $this->getOperator($mode);
53 64
 				}
54
-				else $operator = $this->getOperator($mode);
55 65
 
56 66
 				$args[$key] = $value;
57 67
 				$sql[] = $key . ' ' . $operator . ' :' . $key;
58 68
 			}
59 69
 		}
60 70
 
61
-		if (\Maphper\Maphper::FIND_OR & $mode) $query = implode(' OR  ', $sql);
62
-		else $query = implode(' AND ', $sql);
63
-		if (!empty($query)) $query = '(' . $query . ')';
71
+		if (\Maphper\Maphper::FIND_OR & $mode) {
72
+			$query = implode(' OR  ', $sql);
73
+		} else {
74
+			$query = implode(' AND ', $sql);
75
+		}
76
+		if (!empty($query)) {
77
+			$query = '(' . $query . ')';
78
+		}
64 79
 		return ['args' => $args, 'sql' => $query];
65 80
 	}
66 81
 
67 82
     private function getOperator($mode) {
68 83
         $operator = "";
69 84
 
70
-        if (\Maphper\Maphper::FIND_NOCASE & $mode) $operator = 'LIKE';
71
-        else if (\Maphper\Maphper::FIND_BIT & $mode) $operator = '&';
72
-        else if (\Maphper\Maphper::FIND_GREATER & $mode) $operator = '>';
73
-        else if (\Maphper\Maphper::FIND_LESS & $mode) $operator = '<';
74
-        else if (\Maphper\Maphper::FIND_NOT & $mode) $operator = '!=';
85
+        if (\Maphper\Maphper::FIND_NOCASE & $mode) {
86
+        	$operator = 'LIKE';
87
+        } else if (\Maphper\Maphper::FIND_BIT & $mode) {
88
+        	$operator = '&';
89
+        } else if (\Maphper\Maphper::FIND_GREATER & $mode) {
90
+        	$operator = '>';
91
+        } else if (\Maphper\Maphper::FIND_LESS & $mode) {
92
+        	$operator = '<';
93
+        } else if (\Maphper\Maphper::FIND_NOT & $mode) {
94
+        	$operator = '!=';
95
+        }
75 96
 
76
-        if (\Maphper\Maphper::FIND_EXACT & $mode) $operator .= '=';
97
+        if (\Maphper\Maphper::FIND_EXACT & $mode) {
98
+        	$operator .= '=';
99
+        }
77 100
 
78 101
         return $operator;
79 102
     }
Please login to merge, or discard this patch.
maphper/datasource/database.php 1 patch
Braces   +49 added lines, -26 removed lines patch added patch discarded remove patch
@@ -39,7 +39,9 @@  discard block
 block discarded – undo
39 39
 	}
40 40
 
41 41
     private function optimizeColumns() {
42
-        if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table);
42
+        if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) {
43
+        	$this->adapter->optimiseColumns($this->table);
44
+        }
43 45
     }
44 46
 
45 47
 	public function getPrimaryKey() {
@@ -55,19 +57,23 @@  discard block
 block discarded – undo
55 57
 		if (!isset($this->cache[$id])) {
56 58
 			try {
57 59
 				$result = $this->selectQuery($this->selectBuilder->select($this->table, $this->getPrimaryKey()[0] . ' = :id', [':id' => $id], ['limit' => 1]));
58
-			}
59
-			catch (\Exception $e) {
60
+			} catch (\Exception $e) {
60 61
 			}
61 62
 
62
-			if (isset($result[0])) 	$this->cache[$id] = $result[0];
63
-			else return null;
63
+			if (isset($result[0])) {
64
+				$this->cache[$id] = $result[0];
65
+			} else {
66
+				return null;
67
+			}
64 68
 		}
65 69
 		return $this->cache[$id];
66 70
 	}
67 71
 
68 72
 	public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) {
69 73
 		//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
70
-		if (is_array($field)) $field = $field[0];
74
+		if (is_array($field)) {
75
+			$field = $field[0];
76
+		}
71 77
 		$query = $this->whereBuilder->createSql($criteria);
72 78
 
73 79
 		try {
@@ -76,8 +82,7 @@  discard block
 block discarded – undo
76 82
 			$result = $this->selectQuery($this->selectBuilder->aggregate($this->table, $function, $field, $query['sql'], $query['args'], $group));
77 83
 
78 84
 			return $this->determineAggregateResult($result, $group, $field);
79
-		}
80
-		catch (\Exception $e) {
85
+		} catch (\Exception $e) {
81 86
 			return $group ? [] : 0;
82 87
 		}
83 88
 	}
@@ -85,15 +90,21 @@  discard block
 block discarded – undo
85 90
     private function determineAggregateResult($result, $group, $field) {
86 91
         if ($group != null) {
87 92
             $ret = [];
88
-            foreach ($result as $res) $ret[$res->$field] = $res->val;
93
+            foreach ($result as $res) {
94
+            	$ret[$res->$field] = $res->val;
95
+            }
89 96
             return $ret;
97
+        } else if (isset($result[0])) {
98
+        	return $result[0]->val;
99
+        } else {
100
+        	return 0;
90 101
         }
91
-        else if (isset($result[0])) return $result[0]->val;
92
-        else return 0;
93 102
     }
94 103
 
95 104
 	private function addIndex($args) {
96
-		if (self::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args);
105
+		if (self::EDIT_INDEX & $this->alterDb) {
106
+			$this->adapter->addIndex($this->table, $args);
107
+		}
97 108
 	}
98 109
 
99 110
 	public function findByField(array $fields, $options = []) {
@@ -101,14 +112,15 @@  discard block
 block discarded – undo
101 112
 		if (!isset($this->resultCache[$cacheId])) {
102 113
 			$query = $this->whereBuilder->createSql($fields);
103 114
 
104
-			if (!isset($options['order'])) $options['order'] = $this->defaultSort;
115
+			if (!isset($options['order'])) {
116
+				$options['order'] = $this->defaultSort;
117
+			}
105 118
 
106 119
 			try {
107 120
 				$this->resultCache[$cacheId] = $this->selectQuery($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
 			}
@@ -144,10 +156,13 @@  discard block
 block discarded – undo
144 156
             $result = $this->insert($this->table, $this->primaryKey, $data);
145 157
 
146 158
 			//If there was an error but PDO is silent, trigger the catch block anyway
147
-			if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table);
148
-		}
149
-		catch (\Exception $e) {
150
-			if (!$this->getTryAgain($tryagain)) throw $e;
159
+			if ($result->errorCode() !== '00000') {
160
+				throw new \Exception('Could not insert into ' . $this->table);
161
+			}
162
+		} catch (\Exception $e) {
163
+			if (!$this->getTryAgain($tryagain)) {
164
+				throw $e;
165
+			}
151 166
 
152 167
 			$this->adapter->alterDatabase($this->table, $this->primaryKey, $data);
153 168
 			$this->save($data, false);
@@ -164,28 +179,34 @@  discard block
 block discarded – undo
164 179
     }
165 180
 
166 181
     private function updatePK($data, $new) {
167
-        if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId();
182
+        if ($new && count($this->primaryKey) == 1) {
183
+        	$data->{$this->primaryKey[0]} = $this->adapter->lastInsertId();
184
+        }
168 185
     }
169 186
 
170 187
     private function checkIfUpdateWorked($data) {
171 188
         $updateWhere = $this->whereBuilder->createSql($data);
172 189
         $matched = $this->findByField($updateWhere['args']);
173 190
 
174
-        if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints');
191
+        if (count($matched) == 0) {
192
+        	throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints');
193
+        }
175 194
     }
176 195
 
177 196
     private function updateCache($data) {
178 197
         $pkValue = $data->{$this->primaryKey[0]};
179
-		if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data);
180
-		else $this->cache[$pkValue] = $data;
198
+		if (isset($this->cache[$pkValue])) {
199
+			$this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data);
200
+		} else {
201
+			$this->cache[$pkValue] = $data;
202
+		}
181 203
     }
182 204
 
183 205
 	private function insert($table, array $primaryKey, $data) {
184 206
 		$error = 0;
185 207
 		try {
186 208
 			$result = $this->adapter->query($this->crudBuilder->insert($table, $data));
187
-		}
188
-		catch (\Exception $e) {
209
+		} catch (\Exception $e) {
189 210
 			$error = 1;
190 211
 		}
191 212
 
@@ -198,7 +219,9 @@  discard block
 block discarded – undo
198 219
 
199 220
     private function tryUpdate($table, array $primaryKey, $data) {
200 221
         $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data));
201
-        if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data);
222
+        if ($result->rowCount() === 0) {
223
+        	$this->checkIfUpdateWorked($data);
224
+        }
202 225
 
203 226
         return $result;
204 227
     }
Please login to merge, or discard this patch.