Passed
Push — master ( e288dc...71d24f )
by Richard
01:40
created
maphper/lib/visibilityoverride.php 2 patches
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -7,20 +7,20 @@  discard block
 block discarded – undo
7 7
 
8 8
 	public function __construct($object) {
9 9
 		if ($object instanceof \stdclass) {
10
-			$this->readClosure = function() use ($object) { return $object;	};
11
-			$this->writeClosure = function ($field, $value) use ($object) { $object->$field = $value; };
10
+			$this->readClosure = function() use ($object) { return $object; };
11
+			$this->writeClosure = function($field, $value) use ($object) { $object->$field = $value; };
12 12
 		}
13 13
 		else {
14 14
 			$this->readClosure = function() {
15 15
 				$data = new \stdClass;
16
-				foreach ($this as $k => $v)	{
16
+				foreach ($this as $k => $v) {
17 17
 					if (is_scalar($v) || is_null($v) || (is_object($v) && $v instanceof \DateTime))	$data->$k = $v;
18 18
 				}
19 19
 				return $data;
20 20
 			};
21 21
 			$this->readClosure = $this->readClosure->bindTo($object, $object);
22 22
 
23
-			$this->writeClosure = function ($field, $value) { $this->$field = $value; };
23
+			$this->writeClosure = function($field, $value) { $this->$field = $value; };
24 24
 			$this->writeClosure = $this->writeClosure->bindTo($object, $object);
25 25
 		}
26 26
 			
@@ -33,7 +33,7 @@  discard block
 block discarded – undo
33 33
 	public function write($data) {
34 34
 		if ($data != null) {
35 35
 			foreach ($data as $key => $value) {
36
-				($this->writeClosure)($key,  $this->processDates($value));
36
+				($this->writeClosure)($key, $this->processDates($value));
37 37
 			}
38 38
 		}
39 39
 	}
Please login to merge, or discard this patch.
Braces   +4 added lines, -3 removed lines patch added patch discarded remove patch
@@ -9,12 +9,13 @@
 block discarded – undo
9 9
 		if ($object instanceof \stdclass) {
10 10
 			$this->readClosure = function() use ($object) { return $object;	};
11 11
 			$this->writeClosure = function ($field, $value) use ($object) { $object->$field = $value; };
12
-		}
13
-		else {
12
+		} else {
14 13
 			$this->readClosure = function() {
15 14
 				$data = new \stdClass;
16 15
 				foreach ($this as $k => $v)	{
17
-					if (is_scalar($v) || is_null($v) || (is_object($v) && $v instanceof \DateTime))	$data->$k = $v;
16
+					if (is_scalar($v) || is_null($v) || (is_object($v) && $v instanceof \DateTime)) {
17
+						$data->$k = $v;
18
+					}
18 19
 				}
19 20
 				return $data;
20 21
 			};
Please login to merge, or discard this patch.
maphper/Optimiser/MySql.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -96,7 +96,7 @@  discard block
 block discarded – undo
96 96
 		return;
97 97
 
98 98
 		$runAgain = false;
99
-		$columns = $this->pdo->query('SELECT * FROM '. $this->quote($table) . ' PROCEDURE ANALYSE(1,1)')->fetchAll(\PDO::FETCH_OBJ);
99
+		$columns = $this->pdo->query('SELECT * FROM ' . $this->quote($table) . ' PROCEDURE ANALYSE(1,1)')->fetchAll(\PDO::FETCH_OBJ);
100 100
 		foreach ($columns as $column) {
101 101
 			$parts = explode('.', $column->Field_name);
102 102
 			$name = $this->quote(end($parts));
@@ -130,7 +130,7 @@  discard block
 block discarded – undo
130 130
 					}
131 131
 				}
132 132
 
133
-				$this->pdo->query('ALTER TABLE ' . $this->quote($table) . ' MODIFY '. $name . ' ' . $type);
133
+				$this->pdo->query('ALTER TABLE ' . $this->quote($table) . ' MODIFY ' . $name . ' ' . $type);
134 134
 			}
135 135
 		}
136 136
 		//Sometimes a second pass is needed, if a column has gone from varchar -> int(11) a better int type may be needed
Please login to merge, or discard this patch.
Braces   +13 added lines, -7 removed lines patch added patch discarded remove patch
@@ -11,16 +11,21 @@  discard block
 block discarded – undo
11 11
 		foreach ($columns as $column) {
12 12
 			$parts = explode('.', $column->Field_name);
13 13
 			$name = $this->quote(end($parts));
14
-			if ($column->Min_value === null && $column->Max_value === null) $this->pdo->query('ALTER TABLE ' . $this->quote($table) . ' DROP COLUMN ' . $name);
15
-			else {
14
+			if ($column->Min_value === null && $column->Max_value === null) {
15
+				$this->pdo->query('ALTER TABLE ' . $this->quote($table) . ' DROP COLUMN ' . $name);
16
+			} else {
16 17
 				$type = $column->Optimal_fieldtype;
17 18
 				if ($column->Max_length < 11) {
18 19
 					//Check for dates
19 20
 					$count = $this->pdo->query('SELECT count(*) as `count` FROM ' . $this->quote($table) . ' WHERE STR_TO_DATE(' . $name . ',\'%Y-%m-%d %H:%i:s\') IS NULL OR STR_TO_DATE(' . $name . ',\'%Y-%m-%d %H:%i:s\') != ' . $name . ' LIMIT 1')->fetch(\PDO::FETCH_OBJ)->count;
20
-					if ($count == 0) $type = 'DATETIME';
21
+					if ($count == 0) {
22
+						$type = 'DATETIME';
23
+					}
21 24
 
22 25
 					$count = $this->pdo->query('SELECT count(*) as `count` FROM ' . $this->quote($table) . ' WHERE STR_TO_DATE(' . $name . ',\'%Y-%m-%d\') IS NULL OR STR_TO_DATE(' . $name . ',\'%Y-%m-%d\') != ' . $name . ' LIMIT 1')->fetch(\PDO::FETCH_OBJ)->count;
23
-					if ($count == 0) $type = 'DATE';
26
+					if ($count == 0) {
27
+						$type = 'DATE';
28
+					}
24 29
 				}
25 30
 
26 31
 				//If it's text, work out if it would be better to be something else
@@ -30,8 +35,7 @@  discard block
 block discarded – undo
30 35
 					if ($count == 0) {
31 36
 						$type = 'INT(11)';
32 37
 						$runAgain = true;
33
-					}
34
-					else {
38
+					} else {
35 39
 						//See if it's decimal
36 40
 						$count = $this->pdo->query('SELECT count(*) FROM ' . $table . ' WHERE concat(\'\', ' . $name . ' * 1) != ' . $name . ')')->fetch(\PDO::FETCH_OBJ)->count;
37 41
 						if ($count == 0) {
@@ -45,6 +49,8 @@  discard block
 block discarded – undo
45 49
 			}
46 50
 		}
47 51
 		//Sometimes a second pass is needed, if a column has gone from varchar -> int(11) a better int type may be needed
48
-		if ($runAgain) $this->optimiseColumns($table);
52
+		if ($runAgain) {
53
+			$this->optimiseColumns($table);
54
+		}
49 55
 	}
50 56
 }
51 57
\ No newline at end of file
Please login to merge, or discard this patch.
maphper/datasource/databaseoptions.php 1 patch
Braces   +6 added lines, -2 removed lines patch added patch discarded remove patch
@@ -10,7 +10,9 @@  discard block
 block discarded – undo
10 10
 	}
11 11
 
12 12
 	public function getAdapter() {
13
-		if (!($this->db instanceof \PDO)) return $this->db;
13
+		if (!($this->db instanceof \PDO)) {
14
+			return $this->db;
15
+		}
14 16
 
15 17
 		$adapter = '\\Maphper\\DataSource\\' . ucfirst($this->db->getAttribute(\PDO::ATTR_DRIVER_NAME)) . 'Adapter';
16 18
 
@@ -18,7 +20,9 @@  discard block
 block discarded – undo
18 20
 	}
19 21
 
20 22
 	public function getEditMode() {
21
-		if (!isset($this->options['editmode'])) return false;
23
+		if (!isset($this->options['editmode'])) {
24
+			return false;
25
+		}
22 26
 
23 27
 		return $this->options['editmode'] === true ? Database::EDIT_STRUCTURE | Database::EDIT_INDEX | Database::EDIT_OPTIMISE : $this->options['editmode'];
24 28
 	}
Please login to merge, or discard this patch.
maphper/datasource/StmtCache.php 2 patches
Braces   +6 added lines, -3 removed lines patch added patch discarded remove patch
@@ -10,10 +10,13 @@
 block discarded – undo
10 10
 
11 11
     public function getCachedStmt($sql) {
12 12
 		$queryId = $this->getQueryId($sql);
13
-		if (isset($this->queryCache[$queryId])) $stmt = $this->queryCache[$queryId];
14
-		else {
13
+		if (isset($this->queryCache[$queryId])) {
14
+			$stmt = $this->queryCache[$queryId];
15
+		} else {
15 16
 			$stmt = $this->pdo->prepare($sql, [\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY]);
16
-			if ($stmt) $this->queryCache[$queryId] = $stmt;
17
+			if ($stmt) {
18
+				$this->queryCache[$queryId] = $stmt;
19
+			}
17 20
 		}
18 21
 		return $stmt;
19 22
 	}
Please login to merge, or discard this patch.
Indentation   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -1,14 +1,14 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 namespace Maphper\DataSource;
3 3
 class StmtCache {
4
-    private $pdo;
5
-    private $queryCache = [];
4
+	private $pdo;
5
+	private $queryCache = [];
6 6
 
7
-    public function __construct(\PDO $pdo) {
8
-        $this->pdo = $pdo;
9
-    }
7
+	public function __construct(\PDO $pdo) {
8
+		$this->pdo = $pdo;
9
+	}
10 10
 
11
-    public function getCachedStmt($sql) {
11
+	public function getCachedStmt($sql) {
12 12
 		$queryId = $this->getQueryId($sql);
13 13
 		if (isset($this->queryCache[$queryId])) $stmt = $this->queryCache[$queryId];
14 14
 		else {
@@ -18,15 +18,15 @@  discard block
 block discarded – undo
18 18
 		return $stmt;
19 19
 	}
20 20
 
21
-    private function getQueryId($sql) {
22
-        return md5($sql);
23
-    }
21
+	private function getQueryId($sql) {
22
+		return md5($sql);
23
+	}
24 24
 
25
-    public function deleteQueryFromCache($sql) {
26
-        unset($this->queryCache[$this->getQueryId($sql)]);
27
-    }
25
+	public function deleteQueryFromCache($sql) {
26
+		unset($this->queryCache[$this->getQueryId($sql)]);
27
+	}
28 28
 
29
-    public function clearCache() {
30
-        $this->queryCache = [];
31
-    }
29
+	public function clearCache() {
30
+		$this->queryCache = [];
31
+	}
32 32
 }
Please login to merge, or discard this patch.
maphper/datasource/mysqladapter.php 2 patches
Indentation   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -8,7 +8,7 @@  discard block
 block discarded – undo
8 8
 		$this->pdo = $pdo;
9 9
 		//Set to strict mode to detect 'out of range' errors, action at a distance but it needs to be set for all INSERT queries
10 10
 		$this->pdo->query('SET sql_mode = STRICT_ALL_TABLES');
11
-        $this->stmtCache = new StmtCache($pdo);
11
+		$this->stmtCache = new StmtCache($pdo);
12 12
 	}
13 13
 
14 14
 	public function quote($str) {
@@ -18,7 +18,7 @@  discard block
 block discarded – undo
18 18
 	public function query(\Maphper\Lib\Query $query) {
19 19
 		$stmt = $this->stmtCache->getCachedStmt($query->getSql());
20 20
 		$args = $query->getArgs();
21
-        $stmt->execute($args);
21
+		$stmt->execute($args);
22 22
 
23 23
 		return $stmt;
24 24
 	}
Please login to merge, or discard this patch.
Braces   +27 added lines, -12 removed lines patch added patch discarded remove patch
@@ -24,10 +24,15 @@  discard block
 block discarded – undo
24 24
 	}
25 25
 
26 26
 	private function getType($val) {
27
-		if ($val instanceof \DateTime) return 'DATETIME';
28
-		else if (is_int($val)) return  'INT(11)';
29
-		else if (is_double($val)) return 'DECIMAL(9,' . strlen($val) - strrpos($val, '.') - 1 . ')';
30
-		else if (is_string($val)) return strlen($val) < 192 ? 'VARCHAR(191)' : 'LONGBLOB';
27
+		if ($val instanceof \DateTime) {
28
+			return 'DATETIME';
29
+		} else if (is_int($val)) {
30
+			return  'INT(11)';
31
+		} else if (is_double($val)) {
32
+			return 'DECIMAL(9,' . strlen($val) - strrpos($val, '.') - 1 . ')';
33
+		} else if (is_string($val)) {
34
+			return strlen($val) < 192 ? 'VARCHAR(191)' : 'LONGBLOB';
35
+		}
31 36
 		return 'VARCHAR(191)';
32 37
 	}
33 38
 
@@ -36,8 +41,11 @@  discard block
 block discarded – undo
36 41
 		$parts = [];
37 42
 		foreach ($primaryKey as $key) {
38 43
 			$pk = $data->$key;
39
-			if ($pk == null) $parts[] = $key . ' INT(11) NOT NULL AUTO_INCREMENT';
40
-			else $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL';
44
+			if ($pk == null) {
45
+				$parts[] = $key . ' INT(11) NOT NULL AUTO_INCREMENT';
46
+			} else {
47
+				$parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL';
48
+			}
41 49
 		}
42 50
 
43 51
 		$pkField = implode(', ', $parts) . ', PRIMARY KEY(' . implode(', ', $primaryKey) . ')';
@@ -48,15 +56,20 @@  discard block
 block discarded – undo
48 56
 		$this->createTable($table, $primaryKey, $data);
49 57
 
50 58
 		foreach ($data as $key => $value) {
51
-			if (is_array($value) || (is_object($value) && !($value instanceof \DateTime))) continue;
52
-			if (in_array($key, $primaryKey)) continue;
59
+			if (is_array($value) || (is_object($value) && !($value instanceof \DateTime))) {
60
+				continue;
61
+			}
62
+			if (in_array($key, $primaryKey)) {
63
+				continue;
64
+			}
53 65
 
54 66
 			$type = $this->getType($value);
55 67
 
56 68
 			try {
57
-				if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) throw new \Exception('Could not alter table');
58
-			}
59
-			catch (\Exception $e) {
69
+				if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) {
70
+					throw new \Exception('Could not alter table');
71
+				}
72
+			} catch (\Exception $e) {
60 73
 				$this->pdo->query('ALTER TABLE ' . $table . ' MODIFY ' . $this->quote($key) . ' ' . $type);
61 74
 			}
62 75
 		}
@@ -74,7 +87,9 @@  discard block
 block discarded – undo
74 87
 		$keyName = $this->quote(implode('_', $fields));
75 88
 
76 89
 		$results = $this->pdo->query('SHOW INDEX FROM ' . $this->quote($table) . ' WHERE Key_Name = "' . $keyName . '"');
77
-		if ($results && count($results->fetchAll()) == 0)  $this->pdo->query('CREATE INDEX ' . $keyName . ' ON ' . $this->quote($table) . ' (' . implode(', ', $fields) . ')');
90
+		if ($results && count($results->fetchAll()) == 0) {
91
+			$this->pdo->query('CREATE INDEX ' . $keyName . ' ON ' . $this->quote($table) . ' (' . implode(', ', $fields) . ')');
92
+		}
78 93
 	}
79 94
 
80 95
 	public function optimiseColumns($table) {
Please login to merge, or discard this patch.
maphper/datasource/sqliteadapter.php 3 patches
Indentation   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -7,7 +7,7 @@  discard block
 block discarded – undo
7 7
 
8 8
 	public function __construct(\PDO $pdo) {
9 9
 		$this->pdo = $pdo;
10
-        $this->stmtCache = new StmtCache($pdo);
10
+		$this->stmtCache = new StmtCache($pdo);
11 11
 	}
12 12
 
13 13
 	public function quote($str) {
@@ -15,19 +15,19 @@  discard block
 block discarded – undo
15 15
 	}
16 16
 
17 17
 	public function query(\Maphper\Lib\Query $query) {
18
-        $stmt = $this->stmtCache->getCachedStmt($query->getSql());
18
+		$stmt = $this->stmtCache->getCachedStmt($query->getSql());
19 19
 		$args = $query->getArgs();
20 20
 
21
-        //Handle SQLite when PDO_ERRMODE is set to SILENT
22
-        if ($stmt === false) throw new \Exception('Invalid query');
21
+		//Handle SQLite when PDO_ERRMODE is set to SILENT
22
+		if ($stmt === false) throw new \Exception('Invalid query');
23 23
 
24
-        $stmt->execute($args);
25
-        if ($stmt->errorCode() !== '00000' && $stmt->errorInfo()[2] == 'database schema has changed') {
24
+		$stmt->execute($args);
25
+		if ($stmt->errorCode() !== '00000' && $stmt->errorInfo()[2] == 'database schema has changed') {
26 26
 			$this->stmtCache->deleteQueryFromCache($query->getSql());
27 27
 			return $this->query($query);
28
-        }
28
+		}
29 29
 
30
-        return $stmt;
30
+		return $stmt;
31 31
 	}
32 32
 	
33 33
 	public function lastInsertId() {
Please login to merge, or discard this patch.
Braces   +35 added lines, -17 removed lines patch added patch discarded remove patch
@@ -19,7 +19,9 @@  discard block
 block discarded – undo
19 19
 		$args = $query->getArgs();
20 20
 
21 21
         //Handle SQLite when PDO_ERRMODE is set to SILENT
22
-        if ($stmt === false) throw new \Exception('Invalid query');
22
+        if ($stmt === false) {
23
+        	throw new \Exception('Invalid query');
24
+        }
23 25
 
24 26
         $stmt->execute($args);
25 27
         if ($stmt->errorCode() !== '00000' && $stmt->errorInfo()[2] == 'database schema has changed') {
@@ -35,12 +37,19 @@  discard block
 block discarded – undo
35 37
 	}
36 38
 	
37 39
 	private function getType($val) {
38
-		if ($val instanceof \DateTime) return 'DATETIME';
39
-		else if (is_int($val)) return  'INTEGER';
40
-		else if (is_double($val)) return 'DECIMAL(9,' . strlen($val) - strrpos($val, '.') - 1 . ')';
41
-		else if (is_string($val) && strlen($val) < 256) return 'VARCHAR(255)';
42
-		else if (is_string($val) && strlen($val) > 256) return 'LONGBLOG';
43
-		else return 'VARCHAR(255)';		
40
+		if ($val instanceof \DateTime) {
41
+			return 'DATETIME';
42
+		} else if (is_int($val)) {
43
+			return  'INTEGER';
44
+		} else if (is_double($val)) {
45
+			return 'DECIMAL(9,' . strlen($val) - strrpos($val, '.') - 1 . ')';
46
+		} else if (is_string($val) && strlen($val) < 256) {
47
+			return 'VARCHAR(255)';
48
+		} else if (is_string($val) && strlen($val) > 256) {
49
+			return 'LONGBLOG';
50
+		} else {
51
+			return 'VARCHAR(255)';
52
+		}
44 53
 	}
45 54
 
46 55
 	private function tableExists($name) {
@@ -74,8 +83,7 @@  discard block
 block discarded – undo
74 83
 				$this->pdo->query('INSERT INTO ' . $this->quote($table . $affix) . '(' . $columns . ') SELECT ' . $columns . ' FROM ' . $this->quote($table));
75 84
 				$this->pdo->query('DROP TABLE IF EXISTS ' . $table );
76 85
 			}
77
-		}
78
-		catch (\PDOException $e) {
86
+		} catch (\PDOException $e) {
79 87
 			// No data to copy
80 88
 			echo $e->getMessage();
81 89
 		}
@@ -89,8 +97,11 @@  discard block
 block discarded – undo
89 97
 		$parts = [];
90 98
 		foreach ($primaryKey as $key) {
91 99
 			$pk = $data->$key;
92
-			if ($pk == null) $parts[] = $key . ' INTEGER'; 
93
-			else $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL';					
100
+			if ($pk == null) {
101
+				$parts[] = $key . ' INTEGER';
102
+			} else {
103
+				$parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL';
104
+			}
94 105
 		}
95 106
 		
96 107
 		$pkField = implode(', ', $parts) . ', PRIMARY KEY(' . implode(', ', $primaryKey) . ')';
@@ -99,8 +110,12 @@  discard block
 block discarded – undo
99 110
 		$this->pdo->query('CREATE TABLE ' . $table . ' (' . $pkField . ')');
100 111
 					
101 112
 		foreach ($data as $key => $value) {
102
-			if (is_array($value) || (is_object($value) && !($value instanceof \DateTime))) continue;
103
-			if (in_array($key, $primaryKey)) continue;
113
+			if (is_array($value) || (is_object($value) && !($value instanceof \DateTime))) {
114
+				continue;
115
+			}
116
+			if (in_array($key, $primaryKey)) {
117
+				continue;
118
+			}
104 119
 
105 120
 			$type = $this->getType($value);
106 121
 		
@@ -110,10 +125,14 @@  discard block
 block discarded – undo
110 125
 	
111 126
 
112 127
 	public function addIndex($table, array $fields) {
113
-		if (empty($fields)) return false;
128
+		if (empty($fields)) {
129
+			return false;
130
+		}
114 131
 		
115 132
 		//SQLite doesn't support ASC/DESC indexes, remove the keywords
116
-		foreach ($fields as &$field) $field = str_ireplace([' desc', ' asc'], '', $field);
133
+		foreach ($fields as &$field) {
134
+			$field = str_ireplace([' desc', ' asc'], '', $field);
135
+		}
117 136
 		sort($fields);
118 137
 		$fields = array_map('strtolower', $fields);
119 138
 		$fields = array_map('trim', $fields);
@@ -122,8 +141,7 @@  discard block
 block discarded – undo
122 141
 		
123 142
 		try {
124 143
 			$this->pdo->query('CREATE INDEX IF NOT EXISTS  ' . $keyName . ' ON ' . $table . ' (' . implode(', ', $fields) . ')');
125
-		}
126
-		catch (\Exception $e) {
144
+		} catch (\Exception $e) {
127 145
 			
128 146
 		}
129 147
 	}
Please login to merge, or discard this patch.
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -44,7 +44,7 @@  discard block
 block discarded – undo
44 44
 	}
45 45
 
46 46
 	private function tableExists($name) {
47
-		$result = $this->pdo->query('SELECT name FROM sqlite_master WHERE type="table" and name="'. $name.'"');
47
+		$result = $this->pdo->query('SELECT name FROM sqlite_master WHERE type="table" and name="' . $name . '"');
48 48
 		return count($result->fetchAll()) == 1;
49 49
 	}
50 50
 
@@ -63,7 +63,7 @@  discard block
 block discarded – undo
63 63
 		// SQLSTATE[HY000]: General error: 17 database schema has changed
64 64
 		$this->stmtCache->clearCache();
65 65
 
66
-		$affix = '_'.substr(md5($table), 0, 6);
66
+		$affix = '_' . substr(md5($table), 0, 6);
67 67
 		$this->createTable($table . $affix, $primaryKey, $data);
68 68
 		$fields = [];
69 69
 		foreach ($data as $key => $value) { $fields[] = $key; }
@@ -72,7 +72,7 @@  discard block
 block discarded – undo
72 72
 				$columns = implode(', ', $this->getColumns($table));			
73 73
 
74 74
 				$this->pdo->query('INSERT INTO ' . $this->quote($table . $affix) . '(' . $columns . ') SELECT ' . $columns . ' FROM ' . $this->quote($table));
75
-				$this->pdo->query('DROP TABLE IF EXISTS ' . $table );
75
+				$this->pdo->query('DROP TABLE IF EXISTS ' . $table);
76 76
 			}
77 77
 		}
78 78
 		catch (\PDOException $e) {
@@ -80,8 +80,8 @@  discard block
 block discarded – undo
80 80
 			echo $e->getMessage();
81 81
 		}
82 82
 
83
-		$this->pdo->query('DROP TABLE IF EXISTS ' . $table );
84
-		$this->pdo->query('ALTER TABLE ' . $table . $affix. ' RENAME TO '. $table );
83
+		$this->pdo->query('DROP TABLE IF EXISTS ' . $table);
84
+		$this->pdo->query('ALTER TABLE ' . $table . $affix . ' RENAME TO ' . $table);
85 85
 
86 86
 	}
87 87
 
@@ -95,7 +95,7 @@  discard block
 block discarded – undo
95 95
 		
96 96
 		$pkField = implode(', ', $parts) . ', PRIMARY KEY(' . implode(', ', $primaryKey) . ')';
97 97
 				
98
-		$this->pdo->query('DROP TABLE IF EXISTS ' . $table );
98
+		$this->pdo->query('DROP TABLE IF EXISTS ' . $table);
99 99
 		$this->pdo->query('CREATE TABLE ' . $table . ' (' . $pkField . ')');
100 100
 					
101 101
 		foreach ($data as $key => $value) {
Please login to merge, or discard this patch.
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/datasource/DatabaseSelect.php 3 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -94,7 +94,7 @@
 block discarded – undo
94 94
     }
95 95
 
96 96
     public function updateCache($data, $pkValue) {
97
-		if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data);
97
+		if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object)array_merge((array)$this->cache[$pkValue], (array)$data);
98 98
 		else $this->cache[$pkValue] = $data;
99 99
     }
100 100
 
Please login to merge, or discard this patch.
Indentation   +49 added lines, -49 removed lines patch added patch discarded remove patch
@@ -2,31 +2,31 @@  discard block
 block discarded – undo
2 2
 namespace Maphper\DataSource;
3 3
 
4 4
 class DatabaseSelect {
5
-    private $resultCache = [];
6
-    private $idCache = [];
7
-    private $selectBuilder;
8
-    private $whereBuilder;
9
-    private $adapter;
10
-    private $databaseModify;
11
-    private $defaultSort;
12
-    private $table;
13
-
14
-    public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify, $table, $defaultSort) {
15
-        $this->adapter = $adapter;
16
-        $this->databaseModify = $databaseModify;
17
-        $this->selectBuilder = new \Maphper\Lib\SelectBuilder();
18
-        $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder();
19
-        $this->defaultSort = $defaultSort;
20
-        $this->table = $table;
21
-    }
22
-
23
-    public function findById($id, $pk) {
5
+	private $resultCache = [];
6
+	private $idCache = [];
7
+	private $selectBuilder;
8
+	private $whereBuilder;
9
+	private $adapter;
10
+	private $databaseModify;
11
+	private $defaultSort;
12
+	private $table;
13
+
14
+	public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify, $table, $defaultSort) {
15
+		$this->adapter = $adapter;
16
+		$this->databaseModify = $databaseModify;
17
+		$this->selectBuilder = new \Maphper\Lib\SelectBuilder();
18
+		$this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder();
19
+		$this->defaultSort = $defaultSort;
20
+		$this->table = $table;
21
+	}
22
+
23
+	public function findById($id, $pk) {
24 24
 		if (!isset($this->idCache[$id])) {
25 25
 			try {
26 26
 				$result = $this->selectQuery($this->selectBuilder->select($this->table, $pk . ' = :id', [':id' => $id], ['limit' => 1]));
27 27
 			}
28 28
 			catch (\Exception $e) {
29
-                // Don't issue an error if it cannot be found since we return null
29
+				// Don't issue an error if it cannot be found since we return null
30 30
 			}
31 31
 
32 32
 			if (isset($result[0])) 	$this->idCache[$id] = $result[0];
@@ -35,7 +35,7 @@  discard block
 block discarded – undo
35 35
 		return $this->idCache[$id];
36 36
 	}
37 37
 
38
-    public function findByField(array $fields, $options = []) {
38
+	public function findByField(array $fields, $options = []) {
39 39
 		$cacheId = md5(serialize(func_get_args()));
40 40
 		if (!isset($this->resultCache[$cacheId])) {
41 41
 			$query = $this->whereBuilder->createSql($fields);
@@ -55,7 +55,7 @@  discard block
 block discarded – undo
55 55
 		return $this->resultCache[$cacheId];
56 56
 	}
57 57
 
58
-    public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) {
58
+	public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) {
59 59
 		//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
60 60
 		if (is_array($field)) $field = $field[0];
61 61
 		$query = $this->whereBuilder->createSql($criteria);
@@ -72,34 +72,34 @@  discard block
 block discarded – undo
72 72
 		}
73 73
 	}
74 74
 
75
-    private function determineAggregateResult($result, $group, $field) {
76
-        if ($group != null) {
77
-            $ret = [];
78
-            foreach ($result as $res) $ret[$res->$field] = $res->val;
79
-            return $ret;
80
-        }
81
-        else if (isset($result[0])) return $result[0]->val;
82
-        else return 0;
83
-    }
84
-
85
-    private function selectQuery(\Maphper\Lib\Query $query) {
86
-        return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ);
87
-    }
88
-
89
-    public function clearResultCache() {
90
-        $this->resultCache = [];
91
-    }
92
-
93
-    public function clearIDCache() {
94
-        $this->idCache = [];
95
-    }
96
-
97
-    public function updateCache($data, $pkValue) {
75
+	private function determineAggregateResult($result, $group, $field) {
76
+		if ($group != null) {
77
+			$ret = [];
78
+			foreach ($result as $res) $ret[$res->$field] = $res->val;
79
+			return $ret;
80
+		}
81
+		else if (isset($result[0])) return $result[0]->val;
82
+		else return 0;
83
+	}
84
+
85
+	private function selectQuery(\Maphper\Lib\Query $query) {
86
+		return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ);
87
+	}
88
+
89
+	public function clearResultCache() {
90
+		$this->resultCache = [];
91
+	}
92
+
93
+	public function clearIDCache() {
94
+		$this->idCache = [];
95
+	}
96
+
97
+	public function updateCache($data, $pkValue) {
98 98
 		if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data);
99 99
 		else $this->cache[$pkValue] = $data;
100
-    }
100
+	}
101 101
 
102
-    public function deleteIDFromCache($id) {
103
-        unset($this->idCache[$id]);
104
-    }
102
+	public function deleteIDFromCache($id) {
103
+		unset($this->idCache[$id]);
104
+	}
105 105
 }
Please login to merge, or discard this patch.
Braces   +26 added lines, -15 removed lines patch added patch discarded remove patch
@@ -24,13 +24,15 @@  discard block
 block discarded – undo
24 24
 		if (!isset($this->idCache[$id])) {
25 25
 			try {
26 26
 				$result = $this->selectQuery($this->selectBuilder->select($this->table, $pk . ' = :id', [':id' => $id], ['limit' => 1]));
27
-			}
28
-			catch (\Exception $e) {
27
+			} catch (\Exception $e) {
29 28
                 // Don't issue an error if it cannot be found since we return null
30 29
 			}
31 30
 
32
-			if (isset($result[0])) 	$this->idCache[$id] = $result[0];
33
-			else return null;
31
+			if (isset($result[0])) {
32
+				$this->idCache[$id] = $result[0];
33
+			} else {
34
+				return null;
35
+			}
34 36
 		}
35 37
 		return $this->idCache[$id];
36 38
 	}
@@ -40,14 +42,15 @@  discard block
 block discarded – undo
40 42
 		if (!isset($this->resultCache[$cacheId])) {
41 43
 			$query = $this->whereBuilder->createSql($fields);
42 44
 
43
-			if (!isset($options['order'])) $options['order'] = $this->defaultSort;
45
+			if (!isset($options['order'])) {
46
+				$options['order'] = $this->defaultSort;
47
+			}
44 48
 
45 49
 			try {
46 50
 				$this->resultCache[$cacheId] = $this->selectQuery($this->selectBuilder->select($this->table, $query['sql'], $query['args'], $options));
47 51
 				$this->databaseModify->addIndex(array_keys($query['args']));
48 52
 				$this->databaseModify->addIndex(explode(',', $options['order']));
49
-			}
50
-			catch (\Exception $e) {
53
+			} catch (\Exception $e) {
51 54
 				$this->errors[] = $e;
52 55
 				$this->resultCache[$cacheId] = [];
53 56
 			}
@@ -57,7 +60,9 @@  discard block
 block discarded – undo
57 60
 
58 61
     public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) {
59 62
 		//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
60
-		if (is_array($field)) $field = $field[0];
63
+		if (is_array($field)) {
64
+			$field = $field[0];
65
+		}
61 66
 		$query = $this->whereBuilder->createSql($criteria);
62 67
 
63 68
 		try {
@@ -66,8 +71,7 @@  discard block
 block discarded – undo
66 71
 			$result = $this->selectQuery($this->selectBuilder->aggregate($this->table, $function, $field, $query['sql'], $query['args'], $group));
67 72
 
68 73
 			return $this->determineAggregateResult($result, $group, $field);
69
-		}
70
-		catch (\Exception $e) {
74
+		} catch (\Exception $e) {
71 75
 			return $group ? [] : 0;
72 76
 		}
73 77
 	}
@@ -75,11 +79,15 @@  discard block
 block discarded – undo
75 79
     private function determineAggregateResult($result, $group, $field) {
76 80
         if ($group != null) {
77 81
             $ret = [];
78
-            foreach ($result as $res) $ret[$res->$field] = $res->val;
82
+            foreach ($result as $res) {
83
+            	$ret[$res->$field] = $res->val;
84
+            }
79 85
             return $ret;
86
+        } else if (isset($result[0])) {
87
+        	return $result[0]->val;
88
+        } else {
89
+        	return 0;
80 90
         }
81
-        else if (isset($result[0])) return $result[0]->val;
82
-        else return 0;
83 91
     }
84 92
 
85 93
     private function selectQuery(\Maphper\Lib\Query $query) {
@@ -95,8 +103,11 @@  discard block
 block discarded – undo
95 103
     }
96 104
 
97 105
     public function updateCache($data, $pkValue) {
98
-		if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data);
99
-		else $this->cache[$pkValue] = $data;
106
+		if (isset($this->cache[$pkValue])) {
107
+			$this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data);
108
+		} else {
109
+			$this->cache[$pkValue] = $data;
110
+		}
100 111
     }
101 112
 
102 113
     public function deleteIDFromCache($id) {
Please login to merge, or discard this patch.
maphper/datasource/DatabaseCrud.php 2 patches
Indentation   +50 added lines, -50 removed lines patch added patch discarded remove patch
@@ -2,30 +2,30 @@  discard block
 block discarded – undo
2 2
 namespace Maphper\DataSource;
3 3
 
4 4
 class DatabaseCrud {
5
-    private $crudBuilder;
6
-    private $whereBuilder;
7
-    private $adapter;
8
-    private $databaseModify;
9
-    private $databaseSelect;
10
-    private $table;
11
-    private $primaryKey;
12
-
13
-    public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify, DatabaseSelect $databaseSelect, $table, $primaryKey) {
14
-        $this->adapter = $adapter;
15
-        $this->databaseModify = $databaseModify;
16
-        $this->databaseSelect = $databaseSelect;
17
-        $this->crudBuilder = new \Maphper\Lib\CrudBuilder();
18
-        $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder();
19
-        $this->table = $table;
20
-        $this->primaryKey = $primaryKey;
21
-    }
22
-
23
-    public function deleteById($id) {
24
-        $this->adapter->query($this->crudBuilder->delete($this->table, [$this->primaryKey[0] . ' = :id'], [':id' => $id], 1));
25
-        $this->databaseSelect->deleteIDFromCache($id);
5
+	private $crudBuilder;
6
+	private $whereBuilder;
7
+	private $adapter;
8
+	private $databaseModify;
9
+	private $databaseSelect;
10
+	private $table;
11
+	private $primaryKey;
12
+
13
+	public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify, DatabaseSelect $databaseSelect, $table, $primaryKey) {
14
+		$this->adapter = $adapter;
15
+		$this->databaseModify = $databaseModify;
16
+		$this->databaseSelect = $databaseSelect;
17
+		$this->crudBuilder = new \Maphper\Lib\CrudBuilder();
18
+		$this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder();
19
+		$this->table = $table;
20
+		$this->primaryKey = $primaryKey;
26 21
 	}
27 22
 
28
-    public function deleteByField(array $fields, array $options = []) {
23
+	public function deleteById($id) {
24
+		$this->adapter->query($this->crudBuilder->delete($this->table, [$this->primaryKey[0] . ' = :id'], [':id' => $id], 1));
25
+		$this->databaseSelect->deleteIDFromCache($id);
26
+	}
27
+
28
+	public function deleteByField(array $fields, array $options = []) {
29 29
 		$query = $this->whereBuilder->createSql($fields);
30 30
 		$this->adapter->query($this->crudBuilder->delete($this->table, $query['sql'], $query['args'], $options['limit'], null, $options['order']));
31 31
 		$this->databaseModify->addIndex(array_keys($query['args']));
@@ -35,22 +35,22 @@  discard block
 block discarded – undo
35 35
 		$this->databaseSelect->clearResultCache();
36 36
 	}
37 37
 
38
-    private function getIfNew($data) {
39
-        $new = false;
40
-        foreach ($this->primaryKey as $k) {
41
-            if (empty($data->$k)) {
42
-                $data->$k = null;
43
-                $new = true;
44
-            }
45
-        }
46
-        return $new;
47
-    }
38
+	private function getIfNew($data) {
39
+		$new = false;
40
+		foreach ($this->primaryKey as $k) {
41
+			if (empty($data->$k)) {
42
+				$data->$k = null;
43
+				$new = true;
44
+			}
45
+		}
46
+		return $new;
47
+	}
48 48
 
49
-    public function save($data, $tryagain = true) {
50
-        $new = $this->getIfNew($data);
49
+	public function save($data, $tryagain = true) {
50
+		$new = $this->getIfNew($data);
51 51
 
52 52
 		try {
53
-            $result = $this->insert($this->table, $this->primaryKey, $data);
53
+			$result = $this->insert($this->table, $this->primaryKey, $data);
54 54
 
55 55
 			//If there was an error but PDO is silent, trigger the catch block anyway
56 56
 			if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table);
@@ -68,15 +68,15 @@  discard block
 block discarded – undo
68 68
 		$this->databaseSelect->updateCache($data, $data->{$this->primaryKey[0]});
69 69
 	}
70 70
 
71
-    private function updatePK($data, $new) {
72
-        if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId();
73
-    }
71
+	private function updatePK($data, $new) {
72
+		if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId();
73
+	}
74 74
 
75
-    private function checkIfUpdateWorked($data) {
76
-        $updateWhere = $this->whereBuilder->createSql($data);
77
-        $matched = $this->databaseSelect->findByField($updateWhere['args']);
78
-        if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints');
79
-    }
75
+	private function checkIfUpdateWorked($data) {
76
+		$updateWhere = $this->whereBuilder->createSql($data);
77
+		$matched = $this->databaseSelect->findByField($updateWhere['args']);
78
+		if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints');
79
+	}
80 80
 
81 81
 	private function insert($table, array $primaryKey, $data) {
82 82
 		$error = 0;
@@ -88,16 +88,16 @@  discard block
 block discarded – undo
88 88
 		}
89 89
 
90 90
  		if ($error || $result->errorCode() !== '00000') {
91
-            $result = $this->tryUpdate($table, $primaryKey, $data);
92
-        }
91
+			$result = $this->tryUpdate($table, $primaryKey, $data);
92
+		}
93 93
 
94 94
 		return $result;
95 95
 	}
96 96
 
97
-    private function tryUpdate($table, array $primaryKey, $data) {
98
-        $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data));
99
-        if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data);
97
+	private function tryUpdate($table, array $primaryKey, $data) {
98
+		$result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data));
99
+		if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data);
100 100
 
101
-        return $result;
102
-    }
101
+		return $result;
102
+	}
103 103
 }
Please login to merge, or discard this patch.
Braces   +17 added lines, -9 removed lines patch added patch discarded remove patch
@@ -53,10 +53,13 @@  discard block
 block discarded – undo
53 53
             $result = $this->insert($this->table, $this->primaryKey, $data);
54 54
 
55 55
 			//If there was an error but PDO is silent, trigger the catch block anyway
56
-			if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table);
57
-		}
58
-		catch (\Exception $e) {
59
-			if (!$this->databaseModify->getTryInsertAgain($tryagain)) throw $e;
56
+			if ($result->errorCode() !== '00000') {
57
+				throw new \Exception('Could not insert into ' . $this->table);
58
+			}
59
+		} catch (\Exception $e) {
60
+			if (!$this->databaseModify->getTryInsertAgain($tryagain)) {
61
+				throw $e;
62
+			}
60 63
 
61 64
 			$this->adapter->alterDatabase($this->table, $this->primaryKey, $data);
62 65
 			$this->save($data, false);
@@ -69,21 +72,24 @@  discard block
 block discarded – undo
69 72
 	}
70 73
 
71 74
     private function updatePK($data, $new) {
72
-        if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId();
75
+        if ($new && count($this->primaryKey) == 1) {
76
+        	$data->{$this->primaryKey[0]} = $this->adapter->lastInsertId();
77
+        }
73 78
     }
74 79
 
75 80
     private function checkIfUpdateWorked($data) {
76 81
         $updateWhere = $this->whereBuilder->createSql($data);
77 82
         $matched = $this->databaseSelect->findByField($updateWhere['args']);
78
-        if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints');
83
+        if (count($matched) == 0) {
84
+        	throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints');
85
+        }
79 86
     }
80 87
 
81 88
 	private function insert($table, array $primaryKey, $data) {
82 89
 		$error = 0;
83 90
 		try {
84 91
 			$result = $this->adapter->query($this->crudBuilder->insert($table, $data));
85
-		}
86
-		catch (\Exception $e) {
92
+		} catch (\Exception $e) {
87 93
 			$error = 1;
88 94
 		}
89 95
 
@@ -96,7 +102,9 @@  discard block
 block discarded – undo
96 102
 
97 103
     private function tryUpdate($table, array $primaryKey, $data) {
98 104
         $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data));
99
-        if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data);
105
+        if ($result->rowCount() === 0) {
106
+        	$this->checkIfUpdateWorked($data);
107
+        }
100 108
 
101 109
         return $result;
102 110
     }
Please login to merge, or discard this patch.