Passed
Push — master ( 2d27c8...269005 )
by Richard
01:38
created
maphper/datasource/DatabaseSelect.php 3 patches
Indentation   +22 added lines, -22 removed lines patch added patch discarded remove patch
@@ -2,24 +2,24 @@  discard block
 block discarded – undo
2 2
 namespace Maphper\DataSource;
3 3
 
4 4
 class DatabaseSelect {
5
-    private $resultCache = [];
6
-    private $selectBuilder;
7
-    private $whereBuilder;
8
-    private $adapter;
9
-    private $databaseModify;
10
-    private $defaultSort;
11
-    private $table;
5
+	private $resultCache = [];
6
+	private $selectBuilder;
7
+	private $whereBuilder;
8
+	private $adapter;
9
+	private $databaseModify;
10
+	private $defaultSort;
11
+	private $table;
12 12
 
13
-    public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify,  $defaultSort, $table) {
14
-        $this->adapter = $adapter;
15
-        $this->databaseModify = $databaseModify;
16
-        $this->selectBuilder = new \Maphper\Lib\SelectBuilder();
17
-        $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder();
18
-        $this->defaultSort = $defaultSort;
19
-        $this->table = $table;
20
-    }
13
+	public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify,  $defaultSort, $table) {
14
+		$this->adapter = $adapter;
15
+		$this->databaseModify = $databaseModify;
16
+		$this->selectBuilder = new \Maphper\Lib\SelectBuilder();
17
+		$this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder();
18
+		$this->defaultSort = $defaultSort;
19
+		$this->table = $table;
20
+	}
21 21
 
22
-    public function findByField(array $fields, $options = []) {
22
+	public function findByField(array $fields, $options = []) {
23 23
 		$cacheId = md5(serialize(func_get_args()));
24 24
 		if (!isset($this->resultCache[$cacheId])) {
25 25
 			$query = $this->whereBuilder->createSql($fields);
@@ -39,11 +39,11 @@  discard block
 block discarded – undo
39 39
 		return $this->resultCache[$cacheId];
40 40
 	}
41 41
 
42
-    private function selectQuery(\Maphper\Lib\Query $query) {
43
-        return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ);
44
-    }
42
+	private function selectQuery(\Maphper\Lib\Query $query) {
43
+		return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ);
44
+	}
45 45
 
46
-    public function clearResultCache() {
47
-        $this->resultCache = [];
48
-    }
46
+	public function clearResultCache() {
47
+		$this->resultCache = [];
48
+	}
49 49
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -10,7 +10,7 @@
 block discarded – undo
10 10
     private $defaultSort;
11 11
     private $table;
12 12
 
13
-    public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify,  $defaultSort, $table) {
13
+    public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify, $defaultSort, $table) {
14 14
         $this->adapter = $adapter;
15 15
         $this->databaseModify = $databaseModify;
16 16
         $this->selectBuilder = new \Maphper\Lib\SelectBuilder();
Please login to merge, or discard this patch.
Braces   +4 added lines, -3 removed lines patch added patch discarded remove patch
@@ -24,14 +24,15 @@
 block discarded – undo
24 24
 		if (!isset($this->resultCache[$cacheId])) {
25 25
 			$query = $this->whereBuilder->createSql($fields);
26 26
 
27
-			if (!isset($options['order'])) $options['order'] = $this->defaultSort;
27
+			if (!isset($options['order'])) {
28
+				$options['order'] = $this->defaultSort;
29
+			}
28 30
 
29 31
 			try {
30 32
 				$this->resultCache[$cacheId] = $this->selectQuery($this->selectBuilder->select($this->table, $query['sql'], $query['args'], $options));
31 33
 				$this->databaseModify->addIndex(array_keys($query['args']));
32 34
 				$this->databaseModify->addIndex(explode(',', $options['order']));
33
-			}
34
-			catch (\Exception $e) {
35
+			} catch (\Exception $e) {
35 36
 				$this->errors[] = $e;
36 37
 				$this->resultCache[$cacheId] = [];
37 38
 			}
Please login to merge, or discard this patch.
maphper/datasource/database.php 3 patches
Indentation   +54 added lines, -54 removed lines patch added patch discarded remove patch
@@ -6,17 +6,17 @@  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 = '*';
13 13
 	private $defaultSort;
14 14
 	private $adapter;
15 15
 	private $crudBuilder;
16
-    private $selectBuilder;
17
-    private $whereBuilder;
18
-    private $databaseModify;
19
-    private $databaseSelect;
16
+	private $selectBuilder;
17
+	private $whereBuilder;
18
+	private $databaseModify;
19
+	private $databaseSelect;
20 20
 
21 21
 	public function __construct($db, $table, $primaryKey = 'id', array $options = []) {
22 22
 		$this->options = new DatabaseOptions($db, $options);
@@ -27,13 +27,13 @@  discard block
 block discarded – undo
27 27
 
28 28
 		$this->crudBuilder = new \Maphper\Lib\CrudBuilder();
29 29
 		$this->selectBuilder = new \Maphper\Lib\SelectBuilder();
30
-        $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder();
30
+		$this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder();
31 31
 
32 32
 		$this->fields = implode(',', array_map([$this->adapter, 'quote'], (array) $this->options->read('fields')));
33 33
 
34 34
 		$defaultSort = $this->options->read('defaultSort') !== false ? $this->options->read('defaultSort')  : implode(', ', $this->primaryKey);
35
-        $this->databaseModify = new DatabaseModify($this->adapter, $this->options->getEditMode(), $this->table);
36
-        $this->databaseSelect = new DatabaseSelect($this->adapter, $this->databaseModify, $defaultSort, $this->table);
35
+		$this->databaseModify = new DatabaseModify($this->adapter, $this->options->getEditMode(), $this->table);
36
+		$this->databaseSelect = new DatabaseSelect($this->adapter, $this->databaseModify, $defaultSort, $this->table);
37 37
 
38 38
 		$this->databaseModify->optimizeColumns();
39 39
 	}
@@ -78,18 +78,18 @@  discard block
 block discarded – undo
78 78
 		}
79 79
 	}
80 80
 
81
-    private function determineAggregateResult($result, $group, $field) {
82
-        if ($group != null) {
83
-            $ret = [];
84
-            foreach ($result as $res) $ret[$res->$field] = $res->val;
85
-            return $ret;
86
-        }
87
-        else if (isset($result[0])) return $result[0]->val;
88
-        else return 0;
89
-    }
81
+	private function determineAggregateResult($result, $group, $field) {
82
+		if ($group != null) {
83
+			$ret = [];
84
+			foreach ($result as $res) $ret[$res->$field] = $res->val;
85
+			return $ret;
86
+		}
87
+		else if (isset($result[0])) return $result[0]->val;
88
+		else return 0;
89
+	}
90 90
 
91 91
 	public function findByField(array $fields, $options = []) {
92
-        return $this->databaseSelect->findByField($fields, $options);
92
+		return $this->databaseSelect->findByField($fields, $options);
93 93
 	}
94 94
 
95 95
 	public function deleteByField(array $fields, array $options = []) {
@@ -102,22 +102,22 @@  discard block
 block discarded – undo
102 102
 		$this->databaseSelect->clearResultCache();
103 103
 	}
104 104
 
105
-    private function getIfNew($data) {
106
-        $new = false;
107
-        foreach ($this->primaryKey as $k) {
108
-            if (empty($data->$k)) {
109
-                $data->$k = null;
110
-                $new = true;
111
-            }
112
-        }
113
-        return $new;
114
-    }
105
+	private function getIfNew($data) {
106
+		$new = false;
107
+		foreach ($this->primaryKey as $k) {
108
+			if (empty($data->$k)) {
109
+				$data->$k = null;
110
+				$new = true;
111
+			}
112
+		}
113
+		return $new;
114
+	}
115 115
 
116 116
 	public function save($data, $tryagain = true) {
117
-        $new = $this->getIfNew($data);
117
+		$new = $this->getIfNew($data);
118 118
 
119 119
 		try {
120
-            $result = $this->insert($this->table, $this->primaryKey, $data);
120
+			$result = $this->insert($this->table, $this->primaryKey, $data);
121 121
 
122 122
 			//If there was an error but PDO is silent, trigger the catch block anyway
123 123
 			if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table);
@@ -135,25 +135,25 @@  discard block
 block discarded – undo
135 135
 		$this->updateCache($data);
136 136
 	}
137 137
 
138
-    private function getTryAgain($tryagain) {
139
-        return $tryagain && self::EDIT_STRUCTURE & $this->alterDb;
140
-    }
138
+	private function getTryAgain($tryagain) {
139
+		return $tryagain && self::EDIT_STRUCTURE & $this->alterDb;
140
+	}
141 141
 
142
-    private function updatePK($data, $new) {
143
-        if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId();
144
-    }
142
+	private function updatePK($data, $new) {
143
+		if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId();
144
+	}
145 145
 
146
-    private function checkIfUpdateWorked($data) {
147
-        $updateWhere = $this->whereBuilder->createSql($data);
148
-        $matched = $this->findByField($updateWhere['args']);
149
-        if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints');
150
-    }
146
+	private function checkIfUpdateWorked($data) {
147
+		$updateWhere = $this->whereBuilder->createSql($data);
148
+		$matched = $this->findByField($updateWhere['args']);
149
+		if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints');
150
+	}
151 151
 
152
-    private function updateCache($data) {
153
-        $pkValue = $data->{$this->primaryKey[0]};
152
+	private function updateCache($data) {
153
+		$pkValue = $data->{$this->primaryKey[0]};
154 154
 		if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data);
155 155
 		else $this->cache[$pkValue] = $data;
156
-    }
156
+	}
157 157
 
158 158
 	private function insert($table, array $primaryKey, $data) {
159 159
 		$error = 0;
@@ -165,20 +165,20 @@  discard block
 block discarded – undo
165 165
 		}
166 166
 
167 167
  		if ($error || $result->errorCode() !== '00000') {
168
-            $result = $this->tryUpdate($table, $primaryKey, $data);
169
-        }
168
+			$result = $this->tryUpdate($table, $primaryKey, $data);
169
+		}
170 170
 
171 171
 		return $result;
172 172
 	}
173 173
 
174
-    private function tryUpdate($table, array $primaryKey, $data) {
175
-        $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data));
176
-        if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data);
174
+	private function tryUpdate($table, array $primaryKey, $data) {
175
+		$result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data));
176
+		if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data);
177 177
 
178
-        return $result;
179
-    }
178
+		return $result;
179
+	}
180 180
 
181
-    private function selectQuery(\Maphper\Lib\Query $query) {
182
-        return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ);
183
-    }
181
+	private function selectQuery(\Maphper\Lib\Query $query) {
182
+		return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ);
183
+	}
184 184
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -29,9 +29,9 @@  discard block
 block discarded – undo
29 29
 		$this->selectBuilder = new \Maphper\Lib\SelectBuilder();
30 30
         $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder();
31 31
 
32
-		$this->fields = implode(',', array_map([$this->adapter, 'quote'], (array) $this->options->read('fields')));
32
+		$this->fields = implode(',', array_map([$this->adapter, 'quote'], (array)$this->options->read('fields')));
33 33
 
34
-		$defaultSort = $this->options->read('defaultSort') !== false ? $this->options->read('defaultSort')  : implode(', ', $this->primaryKey);
34
+		$defaultSort = $this->options->read('defaultSort') !== false ? $this->options->read('defaultSort') : implode(', ', $this->primaryKey);
35 35
         $this->databaseModify = new DatabaseModify($this->adapter, $this->options->getEditMode(), $this->table);
36 36
         $this->databaseSelect = new DatabaseSelect($this->adapter, $this->databaseModify, $defaultSort, $this->table);
37 37
 
@@ -151,7 +151,7 @@  discard block
 block discarded – undo
151 151
 
152 152
     private function updateCache($data) {
153 153
         $pkValue = $data->{$this->primaryKey[0]};
154
-		if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data);
154
+		if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object)array_merge((array)$this->cache[$pkValue], (array)$data);
155 155
 		else $this->cache[$pkValue] = $data;
156 156
     }
157 157
 
Please login to merge, or discard this patch.
Braces   +39 added lines, -21 removed lines patch added patch discarded remove patch
@@ -51,19 +51,23 @@  discard block
 block discarded – undo
51 51
 		if (!isset($this->cache[$id])) {
52 52
 			try {
53 53
 				$result = $this->selectQuery($this->selectBuilder->select($this->table, $this->getPrimaryKey()[0] . ' = :id', [':id' => $id], ['limit' => 1]));
54
-			}
55
-			catch (\Exception $e) {
54
+			} catch (\Exception $e) {
56 55
 			}
57 56
 
58
-			if (isset($result[0])) 	$this->cache[$id] = $result[0];
59
-			else return null;
57
+			if (isset($result[0])) {
58
+				$this->cache[$id] = $result[0];
59
+			} else {
60
+				return null;
61
+			}
60 62
 		}
61 63
 		return $this->cache[$id];
62 64
 	}
63 65
 
64 66
 	public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) {
65 67
 		//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
66
-		if (is_array($field)) $field = $field[0];
68
+		if (is_array($field)) {
69
+			$field = $field[0];
70
+		}
67 71
 		$query = $this->whereBuilder->createSql($criteria);
68 72
 
69 73
 		try {
@@ -72,8 +76,7 @@  discard block
 block discarded – undo
72 76
 			$result = $this->selectQuery($this->selectBuilder->aggregate($this->table, $function, $field, $query['sql'], $query['args'], $group));
73 77
 
74 78
 			return $this->determineAggregateResult($result, $group, $field);
75
-		}
76
-		catch (\Exception $e) {
79
+		} catch (\Exception $e) {
77 80
 			return $group ? [] : 0;
78 81
 		}
79 82
 	}
@@ -81,11 +84,15 @@  discard block
 block discarded – undo
81 84
     private function determineAggregateResult($result, $group, $field) {
82 85
         if ($group != null) {
83 86
             $ret = [];
84
-            foreach ($result as $res) $ret[$res->$field] = $res->val;
87
+            foreach ($result as $res) {
88
+            	$ret[$res->$field] = $res->val;
89
+            }
85 90
             return $ret;
91
+        } else if (isset($result[0])) {
92
+        	return $result[0]->val;
93
+        } else {
94
+        	return 0;
86 95
         }
87
-        else if (isset($result[0])) return $result[0]->val;
88
-        else return 0;
89 96
     }
90 97
 
91 98
 	public function findByField(array $fields, $options = []) {
@@ -120,10 +127,13 @@  discard block
 block discarded – undo
120 127
             $result = $this->insert($this->table, $this->primaryKey, $data);
121 128
 
122 129
 			//If there was an error but PDO is silent, trigger the catch block anyway
123
-			if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table);
124
-		}
125
-		catch (\Exception $e) {
126
-			if (!$this->getTryAgain($tryagain)) throw $e;
130
+			if ($result->errorCode() !== '00000') {
131
+				throw new \Exception('Could not insert into ' . $this->table);
132
+			}
133
+		} catch (\Exception $e) {
134
+			if (!$this->getTryAgain($tryagain)) {
135
+				throw $e;
136
+			}
127 137
 
128 138
 			$this->adapter->alterDatabase($this->table, $this->primaryKey, $data);
129 139
 			$this->save($data, false);
@@ -140,27 +150,33 @@  discard block
 block discarded – undo
140 150
     }
141 151
 
142 152
     private function updatePK($data, $new) {
143
-        if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId();
153
+        if ($new && count($this->primaryKey) == 1) {
154
+        	$data->{$this->primaryKey[0]} = $this->adapter->lastInsertId();
155
+        }
144 156
     }
145 157
 
146 158
     private function checkIfUpdateWorked($data) {
147 159
         $updateWhere = $this->whereBuilder->createSql($data);
148 160
         $matched = $this->findByField($updateWhere['args']);
149
-        if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints');
161
+        if (count($matched) == 0) {
162
+        	throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints');
163
+        }
150 164
     }
151 165
 
152 166
     private function updateCache($data) {
153 167
         $pkValue = $data->{$this->primaryKey[0]};
154
-		if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data);
155
-		else $this->cache[$pkValue] = $data;
168
+		if (isset($this->cache[$pkValue])) {
169
+			$this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data);
170
+		} else {
171
+			$this->cache[$pkValue] = $data;
172
+		}
156 173
     }
157 174
 
158 175
 	private function insert($table, array $primaryKey, $data) {
159 176
 		$error = 0;
160 177
 		try {
161 178
 			$result = $this->adapter->query($this->crudBuilder->insert($table, $data));
162
-		}
163
-		catch (\Exception $e) {
179
+		} catch (\Exception $e) {
164 180
 			$error = 1;
165 181
 		}
166 182
 
@@ -173,7 +189,9 @@  discard block
 block discarded – undo
173 189
 
174 190
     private function tryUpdate($table, array $primaryKey, $data) {
175 191
         $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data));
176
-        if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data);
192
+        if ($result->rowCount() === 0) {
193
+        	$this->checkIfUpdateWorked($data);
194
+        }
177 195
 
178 196
         return $result;
179 197
     }
Please login to merge, or discard this patch.
maphper/datasource/DatabaseModify.php 3 patches
Indentation   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -2,21 +2,21 @@
 block discarded – undo
2 2
 namespace Maphper\DataSource;
3 3
 
4 4
 class DatabaseModify {
5
-    private $adapter;
6
-    private $alterDb;
7
-    private $table;
5
+	private $adapter;
6
+	private $alterDb;
7
+	private $table;
8 8
 
9
-    public function __construct(DatabaseAdapter $adapter, $alterDb, $table) {
10
-        $this->adapter = $adapter;
11
-        $this->alterDb = $alterDb;
12
-        $this->table = $table;
13
-    }
9
+	public function __construct(DatabaseAdapter $adapter, $alterDb, $table) {
10
+		$this->adapter = $adapter;
11
+		$this->alterDb = $alterDb;
12
+		$this->table = $table;
13
+	}
14 14
 
15
-    public function addIndex($args) {
15
+	public function addIndex($args) {
16 16
 		if (Database::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args);
17 17
 	}
18 18
 
19
-    public function optimizeColumns() {
20
-        if (Database::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table);
21
-    }
19
+	public function optimizeColumns() {
20
+		if (Database::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table);
21
+	}
22 22
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -17,6 +17,6 @@
 block discarded – undo
17 17
 	}
18 18
 
19 19
     public function optimizeColumns() {
20
-        if (Database::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table);
20
+        if (Database::EDIT_OPTIMISE & $this->alterDb && rand(0, 500) == 1) $this->adapter->optimiseColumns($this->table);
21 21
     }
22 22
 }
Please login to merge, or discard this patch.
Braces   +6 added lines, -2 removed lines patch added patch discarded remove patch
@@ -13,10 +13,14 @@
 block discarded – undo
13 13
     }
14 14
 
15 15
     public function addIndex($args) {
16
-		if (Database::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args);
16
+		if (Database::EDIT_INDEX & $this->alterDb) {
17
+			$this->adapter->addIndex($this->table, $args);
18
+		}
17 19
 	}
18 20
 
19 21
     public function optimizeColumns() {
20
-        if (Database::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table);
22
+        if (Database::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) {
23
+        	$this->adapter->optimiseColumns($this->table);
24
+        }
21 25
     }
22 26
 }
Please login to merge, or discard this patch.