Passed
Push — master ( e9743f...7b0700 )
by Richard
01:43
created
maphper/lib/entity.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -21,7 +21,7 @@
 block discarded – undo
21 21
 	public function wrap($relations, $object, $siblings = []) {
22 22
 		//see if any relations need overwriting
23 23
 		foreach ($relations as $name => $relation) {
24
-			if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) {
24
+			if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation)) {
25 25
 				//After overwriting the relation, does the parent object ($object) need overwriting as well?
26 26
 				if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object;
27 27
 			}
Please login to merge, or discard this patch.
Braces   +3 added lines, -1 removed lines patch added patch discarded remove patch
@@ -23,7 +23,9 @@
 block discarded – undo
23 23
 		foreach ($relations as $name => $relation) {
24 24
 			if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) {
25 25
 				//After overwriting the relation, does the parent object ($object) need overwriting as well?
26
-				if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object;
26
+				if ($relation->overwrite($object, $object->$name)) {
27
+					$this->parent[] = $object;
28
+				}
27 29
 			}
28 30
 
29 31
 			$object->$name = $relation->getData($object, $siblings);
Please login to merge, or discard this patch.
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/mysqladapter.php 2 patches
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -27,7 +27,7 @@
 block discarded – undo
27 27
 	public function query(\Maphper\Lib\Query $query) {
28 28
 		$stmt = $this->getCachedStmt($query->getSql());
29 29
 		$args = $query->getArgs();
30
-        $stmt->execute($args);
30
+		$stmt->execute($args);
31 31
 
32 32
 		if (strpos(trim($query->getSql()), 'SELECT') === 0) return $stmt->fetchAll(\PDO::FETCH_OBJ);
33 33
 		else return $stmt;
Please login to merge, or discard this patch.
Braces   +38 added lines, -17 removed lines patch added patch discarded remove patch
@@ -16,10 +16,13 @@  discard block
 block discarded – undo
16 16
 
17 17
 	private function getCachedStmt($sql) {
18 18
 		$queryId = md5($sql);
19
-		if (isset($this->queryCache[$queryId])) $stmt = $this->queryCache[$queryId];
20
-		else {
19
+		if (isset($this->queryCache[$queryId])) {
20
+			$stmt = $this->queryCache[$queryId];
21
+		} else {
21 22
 			$stmt = $this->pdo->prepare($sql, [\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY]);
22
-			if ($stmt) $this->queryCache[$queryId] = $stmt;
23
+			if ($stmt) {
24
+				$this->queryCache[$queryId] = $stmt;
25
+			}
23 26
 		}
24 27
 		return $stmt;
25 28
 	}
@@ -29,15 +32,23 @@  discard block
 block discarded – undo
29 32
 		$args = $query->getArgs();
30 33
         $stmt->execute($args);
31 34
 
32
-		if (strpos(trim($query->getSql()), 'SELECT') === 0) return $stmt->fetchAll(\PDO::FETCH_OBJ);
33
-		else return $stmt;
35
+		if (strpos(trim($query->getSql()), 'SELECT') === 0) {
36
+			return $stmt->fetchAll(\PDO::FETCH_OBJ);
37
+		} else {
38
+			return $stmt;
39
+		}
34 40
 	}
35 41
 
36 42
 	private function getType($val) {
37
-		if ($val instanceof \DateTime) return 'DATETIME';
38
-		else if (is_int($val)) return  'INT(11)';
39
-		else if (is_double($val)) return 'DECIMAL(9,' . strlen($val) - strrpos($val, '.') - 1 . ')';
40
-		else if (is_string($val)) return strlen($val) < 192 ? 'VARCHAR(191)' : 'LONGBLOB';
43
+		if ($val instanceof \DateTime) {
44
+			return 'DATETIME';
45
+		} else if (is_int($val)) {
46
+			return  'INT(11)';
47
+		} else if (is_double($val)) {
48
+			return 'DECIMAL(9,' . strlen($val) - strrpos($val, '.') - 1 . ')';
49
+		} else if (is_string($val)) {
50
+			return strlen($val) < 192 ? 'VARCHAR(191)' : 'LONGBLOB';
51
+		}
41 52
 		return 'VARCHAR(191)';
42 53
 	}
43 54
 
@@ -46,8 +57,11 @@  discard block
 block discarded – undo
46 57
 		$parts = [];
47 58
 		foreach ($primaryKey as $key) {
48 59
 			$pk = $data->$key;
49
-			if ($pk == null) $parts[] = $key . ' INT(11) NOT NULL AUTO_INCREMENT';
50
-			else $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL';
60
+			if ($pk == null) {
61
+				$parts[] = $key . ' INT(11) NOT NULL AUTO_INCREMENT';
62
+			} else {
63
+				$parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL';
64
+			}
51 65
 		}
52 66
 
53 67
 		$pkField = implode(', ', $parts) . ', PRIMARY KEY(' . implode(', ', $primaryKey) . ')';
@@ -58,15 +72,20 @@  discard block
 block discarded – undo
58 72
 		$this->createTable($table, $primaryKey, $data);
59 73
 
60 74
 		foreach ($data as $key => $value) {
61
-			if (is_array($value) || (is_object($value) && !($value instanceof \DateTime))) continue;
62
-			if (in_array($key, $primaryKey)) continue;
75
+			if (is_array($value) || (is_object($value) && !($value instanceof \DateTime))) {
76
+				continue;
77
+			}
78
+			if (in_array($key, $primaryKey)) {
79
+				continue;
80
+			}
63 81
 
64 82
 			$type = $this->getType($value);
65 83
 
66 84
 			try {
67
-				if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) throw new \Exception('Could not alter table');
68
-			}
69
-			catch (\Exception $e) {
85
+				if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) {
86
+					throw new \Exception('Could not alter table');
87
+				}
88
+			} catch (\Exception $e) {
70 89
 				$this->pdo->query('ALTER TABLE ' . $table . ' MODIFY ' . $this->quote($key) . ' ' . $type);
71 90
 			}
72 91
 		}
@@ -84,7 +103,9 @@  discard block
 block discarded – undo
84 103
 		$keyName = $this->quote(implode('_', $fields));
85 104
 
86 105
 		$results = $this->pdo->query('SHOW INDEX FROM ' . $this->quote($table) . ' WHERE Key_Name = "' . $keyName . '"');
87
-		if ($results && count($results->fetchAll()) == 0)  $this->pdo->query('CREATE INDEX ' . $keyName . ' ON ' . $this->quote($table) . ' (' . implode(', ', $fields) . ')');
106
+		if ($results && count($results->fetchAll()) == 0) {
107
+			$this->pdo->query('CREATE INDEX ' . $keyName . ' ON ' . $this->quote($table) . ' (' . implode(', ', $fields) . ')');
108
+		}
88 109
 	}
89 110
 
90 111
 	public function optimiseColumns($table) {
Please login to merge, or discard this patch.
maphper/datasource/sqliteadapter.php 3 patches
Indentation   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -13,7 +13,7 @@  discard block
 block discarded – undo
13 13
 		return '`' . str_replace('.', '`.`', trim($str, '`')) . '`';
14 14
 	}
15 15
 
16
-    private function getCachedStmt($sql) {
16
+	private function getCachedStmt($sql) {
17 17
 		$queryId = md5($sql);
18 18
 		if (isset($this->queryCache[$queryId])) $stmt = $this->queryCache[$queryId];
19 19
 		else {
@@ -25,20 +25,20 @@  discard block
 block discarded – undo
25 25
 
26 26
 	public function query(\Maphper\Lib\Query $query) {
27 27
 		$queryId = md5($query->getSql());
28
-        $stmt = $this->getCachedStmt($query->getSql());
28
+		$stmt = $this->getCachedStmt($query->getSql());
29 29
 		$args = $query->getArgs();
30 30
 
31
-        //Handle SQLite when PDO_ERRMODE is set to SILENT
32
-        if ($stmt === false) throw new \Exception('Invalid query');
31
+		//Handle SQLite when PDO_ERRMODE is set to SILENT
32
+		if ($stmt === false) throw new \Exception('Invalid query');
33 33
 
34
-        $stmt->execute($args);
35
-        if ($stmt->errorCode() !== '00000' && $stmt->errorInfo()[2] == 'database schema has changed') {
34
+		$stmt->execute($args);
35
+		if ($stmt->errorCode() !== '00000' && $stmt->errorInfo()[2] == 'database schema has changed') {
36 36
 			unset($this->queryCache[$queryId]);
37 37
 			return $this->query($query);
38
-        }
38
+		}
39 39
 
40
-        if (substr($query->getSql(), 0, 6) === 'SELECT') return $stmt->fetchAll(\PDO::FETCH_OBJ);
41
-        else return $stmt;
40
+		if (substr($query->getSql(), 0, 6) === 'SELECT') return $stmt->fetchAll(\PDO::FETCH_OBJ);
41
+		else return $stmt;
42 42
 	}
43 43
 	
44 44
 	public function lastInsertId() {
Please login to merge, or discard this patch.
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -55,7 +55,7 @@  discard block
 block discarded – undo
55 55
 	}
56 56
 
57 57
 	private function tableExists($name) {
58
-		$result = $this->pdo->query('SELECT name FROM sqlite_master WHERE type="table" and name="'. $name.'"');
58
+		$result = $this->pdo->query('SELECT name FROM sqlite_master WHERE type="table" and name="' . $name . '"');
59 59
 		return count($result->fetchAll()) == 1;
60 60
 	}
61 61
 
@@ -74,7 +74,7 @@  discard block
 block discarded – undo
74 74
 		// SQLSTATE[HY000]: General error: 17 database schema has changed
75 75
 		$this->queryCache = [];
76 76
 
77
-		$affix = '_'.substr(md5($table), 0, 6);
77
+		$affix = '_' . substr(md5($table), 0, 6);
78 78
 		$this->createTable($table . $affix, $primaryKey, $data);
79 79
 		$fields = [];
80 80
 		foreach ($data as $key => $value) { $fields[] = $key; }
@@ -83,7 +83,7 @@  discard block
 block discarded – undo
83 83
 				$columns = implode(', ', $this->getColumns($table));			
84 84
 
85 85
 				$this->pdo->query('INSERT INTO ' . $this->quote($table . $affix) . '(' . $columns . ') SELECT ' . $columns . ' FROM ' . $this->quote($table));
86
-				$this->pdo->query('DROP TABLE IF EXISTS ' . $table );
86
+				$this->pdo->query('DROP TABLE IF EXISTS ' . $table);
87 87
 			}
88 88
 		}
89 89
 		catch (\PDOException $e) {
@@ -91,8 +91,8 @@  discard block
 block discarded – undo
91 91
 			echo $e->getMessage();
92 92
 		}
93 93
 
94
-		$this->pdo->query('DROP TABLE IF EXISTS ' . $table );
95
-		$this->pdo->query('ALTER TABLE ' . $table . $affix. ' RENAME TO '. $table );
94
+		$this->pdo->query('DROP TABLE IF EXISTS ' . $table);
95
+		$this->pdo->query('ALTER TABLE ' . $table . $affix . ' RENAME TO ' . $table);
96 96
 
97 97
 	}
98 98
 
@@ -106,7 +106,7 @@  discard block
 block discarded – undo
106 106
 		
107 107
 		$pkField = implode(', ', $parts) . ', PRIMARY KEY(' . implode(', ', $primaryKey) . ')';
108 108
 				
109
-		$this->pdo->query('DROP TABLE IF EXISTS ' . $table );
109
+		$this->pdo->query('DROP TABLE IF EXISTS ' . $table);
110 110
 		$this->pdo->query('CREATE TABLE ' . $table . ' (' . $pkField . ')');
111 111
 					
112 112
 		foreach ($data as $key => $value) {
Please login to merge, or discard this patch.
Braces   +46 added lines, -22 removed lines patch added patch discarded remove patch
@@ -15,10 +15,13 @@  discard block
 block discarded – undo
15 15
 
16 16
     private function getCachedStmt($sql) {
17 17
 		$queryId = md5($sql);
18
-		if (isset($this->queryCache[$queryId])) $stmt = $this->queryCache[$queryId];
19
-		else {
18
+		if (isset($this->queryCache[$queryId])) {
19
+			$stmt = $this->queryCache[$queryId];
20
+		} else {
20 21
 			$stmt = $this->pdo->prepare($sql, [\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY]);
21
-			if ($stmt) $this->queryCache[$queryId] = $stmt;
22
+			if ($stmt) {
23
+				$this->queryCache[$queryId] = $stmt;
24
+			}
22 25
 		}
23 26
 		return $stmt;
24 27
 	}
@@ -29,7 +32,9 @@  discard block
 block discarded – undo
29 32
 		$args = $query->getArgs();
30 33
 
31 34
         //Handle SQLite when PDO_ERRMODE is set to SILENT
32
-        if ($stmt === false) throw new \Exception('Invalid query');
35
+        if ($stmt === false) {
36
+        	throw new \Exception('Invalid query');
37
+        }
33 38
 
34 39
         $stmt->execute($args);
35 40
         if ($stmt->errorCode() !== '00000' && $stmt->errorInfo()[2] == 'database schema has changed') {
@@ -37,8 +42,11 @@  discard block
 block discarded – undo
37 42
 			return $this->query($query);
38 43
         }
39 44
 
40
-        if (substr($query->getSql(), 0, 6) === 'SELECT') return $stmt->fetchAll(\PDO::FETCH_OBJ);
41
-        else return $stmt;
45
+        if (substr($query->getSql(), 0, 6) === 'SELECT') {
46
+        	return $stmt->fetchAll(\PDO::FETCH_OBJ);
47
+        } else {
48
+        	return $stmt;
49
+        }
42 50
 	}
43 51
 	
44 52
 	public function lastInsertId() {
@@ -46,12 +54,19 @@  discard block
 block discarded – undo
46 54
 	}
47 55
 	
48 56
 	private function getType($val) {
49
-		if ($val instanceof \DateTime) return 'DATETIME';
50
-		else if (is_int($val)) return  'INTEGER';
51
-		else if (is_double($val)) return 'DECIMAL(9,' . strlen($val) - strrpos($val, '.') - 1 . ')';
52
-		else if (is_string($val) && strlen($val) < 256) return 'VARCHAR(255)';
53
-		else if (is_string($val) && strlen($val) > 256) return 'LONGBLOG';
54
-		else return 'VARCHAR(255)';		
57
+		if ($val instanceof \DateTime) {
58
+			return 'DATETIME';
59
+		} else if (is_int($val)) {
60
+			return  'INTEGER';
61
+		} else if (is_double($val)) {
62
+			return 'DECIMAL(9,' . strlen($val) - strrpos($val, '.') - 1 . ')';
63
+		} else if (is_string($val) && strlen($val) < 256) {
64
+			return 'VARCHAR(255)';
65
+		} else if (is_string($val) && strlen($val) > 256) {
66
+			return 'LONGBLOG';
67
+		} else {
68
+			return 'VARCHAR(255)';
69
+		}
55 70
 	}
56 71
 
57 72
 	private function tableExists($name) {
@@ -85,8 +100,7 @@  discard block
 block discarded – undo
85 100
 				$this->pdo->query('INSERT INTO ' . $this->quote($table . $affix) . '(' . $columns . ') SELECT ' . $columns . ' FROM ' . $this->quote($table));
86 101
 				$this->pdo->query('DROP TABLE IF EXISTS ' . $table );
87 102
 			}
88
-		}
89
-		catch (\PDOException $e) {
103
+		} catch (\PDOException $e) {
90 104
 			// No data to copy
91 105
 			echo $e->getMessage();
92 106
 		}
@@ -100,8 +114,11 @@  discard block
 block discarded – undo
100 114
 		$parts = [];
101 115
 		foreach ($primaryKey as $key) {
102 116
 			$pk = $data->$key;
103
-			if ($pk == null) $parts[] = $key . ' INTEGER'; 
104
-			else $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL';					
117
+			if ($pk == null) {
118
+				$parts[] = $key . ' INTEGER';
119
+			} else {
120
+				$parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL';
121
+			}
105 122
 		}
106 123
 		
107 124
 		$pkField = implode(', ', $parts) . ', PRIMARY KEY(' . implode(', ', $primaryKey) . ')';
@@ -110,8 +127,12 @@  discard block
 block discarded – undo
110 127
 		$this->pdo->query('CREATE TABLE ' . $table . ' (' . $pkField . ')');
111 128
 					
112 129
 		foreach ($data as $key => $value) {
113
-			if (is_array($value) || (is_object($value) && !($value instanceof \DateTime))) continue;
114
-			if (in_array($key, $primaryKey)) continue;
130
+			if (is_array($value) || (is_object($value) && !($value instanceof \DateTime))) {
131
+				continue;
132
+			}
133
+			if (in_array($key, $primaryKey)) {
134
+				continue;
135
+			}
115 136
 
116 137
 			$type = $this->getType($value);
117 138
 		
@@ -121,10 +142,14 @@  discard block
 block discarded – undo
121 142
 	
122 143
 
123 144
 	public function addIndex($table, array $fields) {
124
-		if (empty($fields)) return false;
145
+		if (empty($fields)) {
146
+			return false;
147
+		}
125 148
 		
126 149
 		//SQLite doesn't support ASC/DESC indexes, remove the keywords
127
-		foreach ($fields as &$field) $field = str_ireplace([' desc', ' asc'], '', $field);
150
+		foreach ($fields as &$field) {
151
+			$field = str_ireplace([' desc', ' asc'], '', $field);
152
+		}
128 153
 		sort($fields);
129 154
 		$fields = array_map('strtolower', $fields);
130 155
 		$fields = array_map('trim', $fields);
@@ -133,8 +158,7 @@  discard block
 block discarded – undo
133 158
 		
134 159
 		try {
135 160
 			$this->pdo->query('CREATE INDEX IF NOT EXISTS  ' . $keyName . ' ON ' . $table . ' (' . implode(', ', $fields) . ')');
136
-		}
137
-		catch (\Exception $e) {
161
+		} catch (\Exception $e) {
138 162
 			
139 163
 		}
140 164
 	}
Please login to merge, or discard this patch.
maphper/lib/selectbuilder.php 3 patches
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -30,10 +30,10 @@  discard block
 block discarded – undo
30 30
 		$sql = [];
31 31
 
32 32
 		foreach ($fields as $key => $value) {
33
-            if ($value instanceof \DateTime) {
34
-    			if ($value->format('H:i:s')  == '00:00:00') $value = $value->format('Y-m-d');
35
-    			else $value = $value->format('Y-m-d H:i:s');
36
-    		}
33
+			if ($value instanceof \DateTime) {
34
+				if ($value->format('H:i:s')  == '00:00:00') $value = $value->format('Y-m-d');
35
+				else $value = $value->format('Y-m-d H:i:s');
36
+			}
37 37
 
38 38
 			if (is_numeric($key) && is_array($value)) {
39 39
 				$result = $this->createSql($value, $key);
@@ -85,17 +85,17 @@  discard block
 block discarded – undo
85 85
 		return ['args' => $args, 'sql' => [$query]];
86 86
 	}
87 87
 
88
-    private function getOperator($mode) {
89
-        $operator = "";
88
+	private function getOperator($mode) {
89
+		$operator = "";
90 90
 
91
-        if (\Maphper\Maphper::FIND_NOCASE & $mode) $operator = 'LIKE';
92
-        else if (\Maphper\Maphper::FIND_BIT & $mode) $operator = '&';
93
-        else if (\Maphper\Maphper::FIND_GREATER & $mode) $operator = '>';
94
-        else if (\Maphper\Maphper::FIND_LESS & $mode) $operator = '<';
95
-        else if (\Maphper\Maphper::FIND_NOT & $mode) $operator = '!=';
91
+		if (\Maphper\Maphper::FIND_NOCASE & $mode) $operator = 'LIKE';
92
+		else if (\Maphper\Maphper::FIND_BIT & $mode) $operator = '&';
93
+		else if (\Maphper\Maphper::FIND_GREATER & $mode) $operator = '>';
94
+		else if (\Maphper\Maphper::FIND_LESS & $mode) $operator = '<';
95
+		else if (\Maphper\Maphper::FIND_NOT & $mode) $operator = '!=';
96 96
 
97
-        if (\Maphper\Maphper::FIND_EXACT & $mode) $operator .= '=';
97
+		if (\Maphper\Maphper::FIND_EXACT & $mode) $operator .= '=';
98 98
 
99
-        return $operator;
100
-    }
99
+		return $operator;
100
+	}
101 101
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -25,13 +25,13 @@  discard block
 block discarded – undo
25 25
 
26 26
 
27 27
 	//Needs to be broken up into better methods
28
-	public function createSql($fields, $mode){
28
+	public function createSql($fields, $mode) {
29 29
 		$args = [];
30 30
 		$sql = [];
31 31
 
32 32
 		foreach ($fields as $key => $value) {
33 33
             if ($value instanceof \DateTime) {
34
-    			if ($value->format('H:i:s')  == '00:00:00') $value = $value->format('Y-m-d');
34
+    			if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d');
35 35
     			else $value = $value->format('Y-m-d H:i:s');
36 36
     		}
37 37
 
@@ -55,7 +55,7 @@  discard block
 block discarded – undo
55 55
 					$inSql[] = ':' . $key . $i;
56 56
 				}
57 57
 				if (count($inSql) == 0) return [];
58
-				else $sql[] = $key . ' IN ( ' .  implode(', ', $inSql) . ')';
58
+				else $sql[] = $key . ' IN ( ' . implode(', ', $inSql) . ')';
59 59
 			}
60 60
 			else if ($value === NULL) {
61 61
 				$nullSql = $key . ' IS ';
Please login to merge, or discard this patch.
Braces   +59 added lines, -32 removed lines patch added patch discarded remove patch
@@ -9,17 +9,23 @@  discard block
 block discarded – undo
9 9
 
10 10
 		if (isset($options['offset'])) {
11 11
 			$offset = ' OFFSET ' . $options['offset'];
12
-			if (!$limit) $limit = ' LIMIT  1000';
12
+			if (!$limit) {
13
+				$limit = ' LIMIT  1000';
14
+			}
15
+		} else {
16
+			$offset = '';
13 17
 		}
14
-		else $offset = '';
15 18
 
16 19
 		$order = isset($options['order']) ? ' ORDER BY ' . $options['order'] : '';
17 20
 		return new Query('SELECT * FROM ' . $table . ' ' . $where . $order . $limit . $offset, $args);
18 21
 	}
19 22
 
20 23
 	public function aggregate($table, $function, $field, $where, $args, $group) {
21
-		if ($group == true) $groupBy = ' GROUP BY ' . $field;
22
-		else $groupBy = '';
24
+		if ($group == true) {
25
+			$groupBy = ' GROUP BY ' . $field;
26
+		} else {
27
+			$groupBy = '';
28
+		}
23 29
 		return new Query('SELECT ' . $function . '(' . $field . ') as val, ' . $field . '   FROM ' . $table . ($where[0] != null ? ' WHERE ' : '') . implode(' AND ', $where) . ' ' . $groupBy, $args);
24 30
 	}
25 31
 
@@ -31,70 +37,91 @@  discard block
 block discarded – undo
31 37
 
32 38
 		foreach ($fields as $key => $value) {
33 39
             if ($value instanceof \DateTime) {
34
-    			if ($value->format('H:i:s')  == '00:00:00') $value = $value->format('Y-m-d');
35
-    			else $value = $value->format('Y-m-d H:i:s');
40
+    			if ($value->format('H:i:s')  == '00:00:00') {
41
+    				$value = $value->format('Y-m-d');
42
+    			} else {
43
+    				$value = $value->format('Y-m-d H:i:s');
44
+    			}
36 45
     		}
37 46
 
38 47
 			if (is_numeric($key) && is_array($value)) {
39 48
 				$result = $this->createSql($value, $key);
40
-				foreach ($result['args'] as $arg_key => $arg) $args[$arg_key] = $arg;
41
-				foreach ($result['sql'] as $arg) $sql[] = $arg;
42
-			}
43
-			else if (\Maphper\Maphper::FIND_BETWEEN & $mode) {
49
+				foreach ($result['args'] as $arg_key => $arg) {
50
+					$args[$arg_key] = $arg;
51
+				}
52
+				foreach ($result['sql'] as $arg) {
53
+					$sql[] = $arg;
54
+				}
55
+			} else if (\Maphper\Maphper::FIND_BETWEEN & $mode) {
44 56
 				$sql[] = $key . '>= :' . $key . 'from';
45 57
 				$sql[] = $key . ' <= :' . $key . 'to';
46 58
 
47 59
 				$args[$key . 'from'] = $value[0];
48 60
 				$args[$key . 'to'] = $value[1];
49
-			}
50
-			else if (!is_numeric($key) && is_array($value)) {
61
+			} else if (!is_numeric($key) && is_array($value)) {
51 62
 				$inSql = [];
52 63
 				$count = count($value);
53 64
 				for ($i = 0; $i < $count; $i++) {
54 65
 					$args[$key . $i] = $value[$i];
55 66
 					$inSql[] = ':' . $key . $i;
56 67
 				}
57
-				if (count($inSql) == 0) return [];
58
-				else $sql[] = $key . ' IN ( ' .  implode(', ', $inSql) . ')';
59
-			}
60
-			else if ($value === NULL) {
68
+				if (count($inSql) == 0) {
69
+					return [];
70
+				} else {
71
+					$sql[] = $key . ' IN ( ' .  implode(', ', $inSql) . ')';
72
+				}
73
+			} else if ($value === NULL) {
61 74
 				$nullSql = $key . ' IS ';
62
-				if (\Maphper\Maphper::FIND_NOT & $mode) $nullSql .= 'NOT ';
75
+				if (\Maphper\Maphper::FIND_NOT & $mode) {
76
+					$nullSql .= 'NOT ';
77
+				}
63 78
 				$sql[] = $nullSql . 'NULL';
64
-			}
65
-			else {
79
+			} else {
66 80
 
67 81
 				if (\Maphper\Maphper::FIND_LIKE & $mode) {
68 82
 					$operator = 'LIKE';
69 83
 					$value = '%' . $value . '%';
70
-				}
71
-				else if (\Maphper\Maphper::FIND_STARTS & $mode) {
84
+				} else if (\Maphper\Maphper::FIND_STARTS & $mode) {
72 85
 					$operator = 'LIKE';
73 86
 					$value = $value . '%';
87
+				} else {
88
+					$operator = $this->getOperator($mode);
74 89
 				}
75
-				else $operator = $this->getOperator($mode);
76 90
 
77 91
 				$args[$key] = $value;
78 92
 				$sql[] = $key . ' ' . $operator . ' :' . $key;
79 93
 			}
80 94
 		}
81 95
 
82
-		if (\Maphper\Maphper::FIND_OR & $mode) $query = implode(' OR  ', $sql);
83
-		else $query = implode(' AND ', $sql);
84
-		if (!empty($query)) $query = '(' . $query . ')';
96
+		if (\Maphper\Maphper::FIND_OR & $mode) {
97
+			$query = implode(' OR  ', $sql);
98
+		} else {
99
+			$query = implode(' AND ', $sql);
100
+		}
101
+		if (!empty($query)) {
102
+			$query = '(' . $query . ')';
103
+		}
85 104
 		return ['args' => $args, 'sql' => [$query]];
86 105
 	}
87 106
 
88 107
     private function getOperator($mode) {
89 108
         $operator = "";
90 109
 
91
-        if (\Maphper\Maphper::FIND_NOCASE & $mode) $operator = 'LIKE';
92
-        else if (\Maphper\Maphper::FIND_BIT & $mode) $operator = '&';
93
-        else if (\Maphper\Maphper::FIND_GREATER & $mode) $operator = '>';
94
-        else if (\Maphper\Maphper::FIND_LESS & $mode) $operator = '<';
95
-        else if (\Maphper\Maphper::FIND_NOT & $mode) $operator = '!=';
96
-
97
-        if (\Maphper\Maphper::FIND_EXACT & $mode) $operator .= '=';
110
+        if (\Maphper\Maphper::FIND_NOCASE & $mode) {
111
+        	$operator = 'LIKE';
112
+        } else if (\Maphper\Maphper::FIND_BIT & $mode) {
113
+        	$operator = '&';
114
+        } else if (\Maphper\Maphper::FIND_GREATER & $mode) {
115
+        	$operator = '>';
116
+        } else if (\Maphper\Maphper::FIND_LESS & $mode) {
117
+        	$operator = '<';
118
+        } else if (\Maphper\Maphper::FIND_NOT & $mode) {
119
+        	$operator = '!=';
120
+        }
121
+
122
+        if (\Maphper\Maphper::FIND_EXACT & $mode) {
123
+        	$operator .= '=';
124
+        }
98 125
 
99 126
         return $operator;
100 127
     }
Please login to merge, or discard this patch.
maphper/datasource/database.php 3 patches
Indentation   +22 added lines, -22 removed lines patch added patch discarded remove patch
@@ -114,7 +114,7 @@  discard block
 block discarded – undo
114 114
 		else $limit = '';
115 115
 
116 116
 		$query = $this->selectBuilder->createSql($fields, $mode);
117
-        $query['sql'] = array_filter($query['sql']);
117
+		$query['sql'] = array_filter($query['sql']);
118 118
 		$this->adapter->query($this->crudBuilder->delete($this->table, $query['sql'], $query['args'], $limit));
119 119
 		$this->addIndex(array_keys($query['args']));
120 120
 
@@ -123,23 +123,23 @@  discard block
 block discarded – undo
123 123
 		$this->resultCache = [];
124 124
 	}
125 125
 
126
-    private function getIfNew($data) {
127
-        $new = false;
128
-        foreach ($this->primaryKey as $k) {
129
-            if (empty($data->$k)) {
130
-                $data->$k = null;
131
-                $new = true;
132
-            }
133
-        }
134
-        return $new;
135
-    }
126
+	private function getIfNew($data) {
127
+		$new = false;
128
+		foreach ($this->primaryKey as $k) {
129
+			if (empty($data->$k)) {
130
+				$data->$k = null;
131
+				$new = true;
132
+			}
133
+		}
134
+		return $new;
135
+	}
136 136
 
137 137
 	public function save($data, $tryagain = true) {
138 138
 		$tryagain = $tryagain && self::EDIT_STRUCTURE & $this->alterDb;
139
-        $new = $this->getIfNew($data);
139
+		$new = $this->getIfNew($data);
140 140
 
141 141
 		try {
142
-            $result = $this->insert($this->table, $this->primaryKey, $data);
142
+			$result = $this->insert($this->table, $this->primaryKey, $data);
143 143
 
144 144
 			//If there was an error but PDO is silent, trigger the catch block anyway
145 145
 			if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table);
@@ -158,18 +158,18 @@  discard block
 block discarded – undo
158 158
 		$this->updateCache($data);
159 159
 	}
160 160
 
161
-    private function checkIfUpdateWorked($data) {
162
-        $updateWhere = $this->crudBuilder->update($this->table, $this->primaryKey, $data);
163
-        $matched = $this->findByField($updateWhere->getArgs());
161
+	private function checkIfUpdateWorked($data) {
162
+		$updateWhere = $this->crudBuilder->update($this->table, $this->primaryKey, $data);
163
+		$matched = $this->findByField($updateWhere->getArgs());
164 164
 
165
-        if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints');
166
-    }
165
+		if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints');
166
+	}
167 167
 
168
-    private function updateCache($data) {
169
-        $pkValue = $data->{$this->primaryKey[0]};
168
+	private function updateCache($data) {
169
+		$pkValue = $data->{$this->primaryKey[0]};
170 170
 		if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data);
171 171
 		else $this->cache[$pkValue] = $data;
172
-    }
172
+	}
173 173
 
174 174
 	private function insert($table, array $primaryKey, $data) {
175 175
 		$error = 0;
@@ -184,7 +184,7 @@  discard block
 block discarded – undo
184 184
  			$result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data));
185 185
  		}
186 186
 
187
-        if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data);
187
+		if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data);
188 188
 
189 189
 		return $result;
190 190
 	}
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -25,13 +25,13 @@  discard block
 block discarded – undo
25 25
 		$this->crudBuilder = new \Maphper\Lib\CrudBuilder();
26 26
 		$this->selectBuilder = new \Maphper\Lib\SelectBuilder();
27 27
 
28
-		$this->fields = implode(',', array_map([$this->adapter, 'quote'], (array) $this->options->read('fields')));
28
+		$this->fields = implode(',', array_map([$this->adapter, 'quote'], (array)$this->options->read('fields')));
29 29
 
30
-		$this->defaultSort = $this->options->read('defaultSort') !== false ? $this->options->read('defaultSort')  : implode(', ', $this->primaryKey);
30
+		$this->defaultSort = $this->options->read('defaultSort') !== false ? $this->options->read('defaultSort') : implode(', ', $this->primaryKey);
31 31
 
32 32
 		$this->alterDb = $this->options->getEditMode();
33 33
 
34
-		if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($table);
34
+		if (self::EDIT_OPTIMISE & $this->alterDb && rand(0, 500) == 1) $this->adapter->optimiseColumns($table);
35 35
 	}
36 36
 
37 37
 	public function getPrimaryKey() {
@@ -167,7 +167,7 @@  discard block
 block discarded – undo
167 167
 
168 168
     private function updateCache($data) {
169 169
         $pkValue = $data->{$this->primaryKey[0]};
170
-		if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data);
170
+		if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object)array_merge((array)$this->cache[$pkValue], (array)$data);
171 171
 		else $this->cache[$pkValue] = $data;
172 172
     }
173 173
 
Please login to merge, or discard this patch.
Braces   +57 added lines, -30 removed lines patch added patch discarded remove patch
@@ -31,7 +31,9 @@  discard block
 block discarded – undo
31 31
 
32 32
 		$this->alterDb = $this->options->getEditMode();
33 33
 
34
-		if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($table);
34
+		if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) {
35
+			$this->adapter->optimiseColumns($table);
36
+		}
35 37
 	}
36 38
 
37 39
 	public function getPrimaryKey() {
@@ -47,20 +49,24 @@  discard block
 block discarded – undo
47 49
 		if (!isset($this->cache[$id])) {
48 50
 			try {
49 51
 				$result = $this->adapter->query($this->selectBuilder->select($this->table, [$this->getPrimaryKey()[0] . ' = :id'], [':id' => $id], ['limit' => 1]));
50
-			}
51
-			catch (\Exception $e) {
52
+			} catch (\Exception $e) {
52 53
 				$this->errors[] = $e;
53 54
 			}
54 55
 
55
-			if (isset($result[0])) 	$this->cache[$id] = $result[0];
56
-			else return null;
56
+			if (isset($result[0])) {
57
+				$this->cache[$id] = $result[0];
58
+			} else {
59
+				return null;
60
+			}
57 61
 		}
58 62
 		return $this->cache[$id];
59 63
 	}
60 64
 
61 65
 	public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) {
62 66
 		//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
63
-		if (is_array($field)) $field = $field[0];
67
+		if (is_array($field)) {
68
+			$field = $field[0];
69
+		}
64 70
 		$query = $this->selectBuilder->createSql($criteria, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND);
65 71
 
66 72
 		try {
@@ -68,22 +74,27 @@  discard block
 block discarded – undo
68 74
 			$this->addIndex(explode(',', $group));
69 75
 			$result = $this->adapter->query($this->selectBuilder->aggregate($this->table, $function, $field, $query['sql'], $query['args'], $group));
70 76
 
71
-			if (isset($result[0]) && $group == null) return $result[0]->val;
72
-			else if ($group != null) {
77
+			if (isset($result[0]) && $group == null) {
78
+				return $result[0]->val;
79
+			} else if ($group != null) {
73 80
 				$ret = [];
74
-				foreach ($result as $res) $ret[$res->$field] = $res->val;
81
+				foreach ($result as $res) {
82
+					$ret[$res->$field] = $res->val;
83
+				}
75 84
 				return $ret;
85
+			} else {
86
+				return 0;
76 87
 			}
77
-			else return 0;
78
-		}
79
-		catch (\Exception $e) {
88
+		} catch (\Exception $e) {
80 89
 			$this->errors[] = $e;
81 90
 			return $group ? [] : 0;
82 91
 		}
83 92
 	}
84 93
 
85 94
 	private function addIndex($args) {
86
-		if (self::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args);
95
+		if (self::EDIT_INDEX & $this->alterDb) {
96
+			$this->adapter->addIndex($this->table, $args);
97
+		}
87 98
 	}
88 99
 
89 100
 	public function findByField(array $fields, $options = []) {
@@ -91,7 +102,9 @@  discard block
 block discarded – undo
91 102
 		if (!isset($this->resultCache[$cacheId])) {
92 103
 			$query = $this->selectBuilder->createSql($fields, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND);
93 104
 
94
-			if (!isset($options['order'])) $options['order'] = $this->defaultSort;
105
+			if (!isset($options['order'])) {
106
+				$options['order'] = $this->defaultSort;
107
+			}
95 108
 
96 109
 			$query['sql'] = array_filter($query['sql']);
97 110
 
@@ -99,8 +112,7 @@  discard block
 block discarded – undo
99 112
 				$this->resultCache[$cacheId] = $this->adapter->query($this->selectBuilder->select($this->table, $query['sql'], $query['args'], $options));
100 113
 				$this->addIndex(array_keys($query['args']));
101 114
 				$this->addIndex(explode(',', $options['order']));
102
-			}
103
-			catch (\Exception $e) {
115
+			} catch (\Exception $e) {
104 116
 				$this->errors[] = $e;
105 117
 				$this->resultCache[$cacheId] = [];
106 118
 			}
@@ -109,9 +121,14 @@  discard block
 block discarded – undo
109 121
 	}
110 122
 
111 123
 	public function deleteByField(array $fields, array $options = [], $mode = null) {
112
-		if ($mode == null) $mode = \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND;
113
-		if (isset($options['limit']) != null) $limit = ' LIMIT ' . $options['limit'];
114
-		else $limit = '';
124
+		if ($mode == null) {
125
+			$mode = \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND;
126
+		}
127
+		if (isset($options['limit']) != null) {
128
+			$limit = ' LIMIT ' . $options['limit'];
129
+		} else {
130
+			$limit = '';
131
+		}
115 132
 
116 133
 		$query = $this->selectBuilder->createSql($fields, $mode);
117 134
         $query['sql'] = array_filter($query['sql']);
@@ -142,17 +159,21 @@  discard block
 block discarded – undo
142 159
             $result = $this->insert($this->table, $this->primaryKey, $data);
143 160
 
144 161
 			//If there was an error but PDO is silent, trigger the catch block anyway
145
-			if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table);
146
-		}
147
-		catch (\Exception $e) {
162
+			if ($result->errorCode() !== '00000') {
163
+				throw new \Exception('Could not insert into ' . $this->table);
164
+			}
165
+		} catch (\Exception $e) {
148 166
 			if ($tryagain) {
149 167
 				$this->adapter->alterDatabase($this->table, $this->primaryKey, $data);
150 168
 				$this->save($data, false);
169
+			} else {
170
+				throw $e;
151 171
 			}
152
-			else throw $e;
153 172
 		}
154 173
 
155
-		if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId();
174
+		if ($new && count($this->primaryKey) == 1) {
175
+			$data->{$this->primaryKey[0]} = $this->adapter->lastInsertId();
176
+		}
156 177
 		//Something has changed, clear any cached results as they may now be incorrect
157 178
 		$this->resultCache = [];
158 179
 		$this->updateCache($data);
@@ -162,21 +183,25 @@  discard block
 block discarded – undo
162 183
         $updateWhere = $this->crudBuilder->update($this->table, $this->primaryKey, $data);
163 184
         $matched = $this->findByField($updateWhere->getArgs());
164 185
 
165
-        if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints');
186
+        if (count($matched) == 0) {
187
+        	throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints');
188
+        }
166 189
     }
167 190
 
168 191
     private function updateCache($data) {
169 192
         $pkValue = $data->{$this->primaryKey[0]};
170
-		if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data);
171
-		else $this->cache[$pkValue] = $data;
193
+		if (isset($this->cache[$pkValue])) {
194
+			$this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data);
195
+		} else {
196
+			$this->cache[$pkValue] = $data;
197
+		}
172 198
     }
173 199
 
174 200
 	private function insert($table, array $primaryKey, $data) {
175 201
 		$error = 0;
176 202
 		try {
177 203
 			$result = $this->adapter->query($this->crudBuilder->insert($table, $data));
178
-		}
179
-		catch (\Exception $e) {
204
+		} catch (\Exception $e) {
180 205
 			$error = 1;
181 206
 		}
182 207
 
@@ -184,7 +209,9 @@  discard block
 block discarded – undo
184 209
  			$result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data));
185 210
  		}
186 211
 
187
-        if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data);
212
+        if ($result->rowCount() === 0) {
213
+        	$this->checkIfUpdateWorked($data);
214
+        }
188 215
 
189 216
 		return $result;
190 217
 	}
Please login to merge, or discard this patch.