Passed
Push — master ( 380864...509b8c )
by Richard
01:39
created
maphper/datasource/mysqladapter.php 2 patches
Indentation   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -3,14 +3,14 @@  discard block
 block discarded – undo
3 3
 class MysqlAdapter implements DatabaseAdapter {
4 4
 	private $pdo;
5 5
 	private $stmtCache;
6
-    private $generalEditor;
6
+	private $generalEditor;
7 7
 
8 8
 	public function __construct(\PDO $pdo) {
9 9
 		$this->pdo = $pdo;
10 10
 		//Set to strict mode to detect 'out of range' errors, action at a distance but it needs to be set for all INSERT queries
11 11
 		$this->pdo->query('SET sql_mode = STRICT_ALL_TABLES');
12
-        $this->stmtCache = new StmtCache($pdo);
13
-        $this->generalEditor = new GeneralEditDatabase($this->pdo, ['short_string_max_len' => 191]);
12
+		$this->stmtCache = new StmtCache($pdo);
13
+		$this->generalEditor = new GeneralEditDatabase($this->pdo, ['short_string_max_len' => 191]);
14 14
 	}
15 15
 
16 16
 	public function quote($str) {
@@ -20,13 +20,13 @@  discard block
 block discarded – undo
20 20
 	public function query(\Maphper\Lib\Query $query) {
21 21
 		$stmt = $this->stmtCache->getCachedStmt($query->getSql());
22 22
 		$args = $query->getArgs();
23
-        $stmt->execute($args);
23
+		$stmt->execute($args);
24 24
 
25 25
 		return $stmt;
26 26
 	}
27 27
 
28
-    private function alterColumns($table, array $primaryKey, $data) {
29
-        foreach ($data as $key => $value) {
28
+	private function alterColumns($table, array $primaryKey, $data) {
29
+		foreach ($data as $key => $value) {
30 30
 			if ($this->generalEditor->isNotSavableType($value, $key, $primaryKey)) continue;
31 31
 
32 32
 			$type = $this->generalEditor->getType($value);
@@ -38,11 +38,11 @@  discard block
 block discarded – undo
38 38
 				$this->pdo->query('ALTER TABLE ' . $table . ' MODIFY ' . $this->quote($key) . ' ' . $type);
39 39
 			}
40 40
 		}
41
-    }
41
+	}
42 42
 
43 43
 	public function alterDatabase($table, array $primaryKey, $data) {
44 44
 		$this->generalEditor->createTable($table, $primaryKey, $data);
45
-        $this->alterColumns($table, $primaryKey, $data);
45
+		$this->alterColumns($table, $primaryKey, $data);
46 46
 	}
47 47
 
48 48
 	public function lastInsertId() {
Please login to merge, or discard this patch.
Braces   +10 added lines, -5 removed lines patch added patch discarded remove patch
@@ -27,14 +27,17 @@  discard block
 block discarded – undo
27 27
 
28 28
     private function alterColumns($table, array $primaryKey, $data) {
29 29
         foreach ($data as $key => $value) {
30
-			if ($this->generalEditor->isNotSavableType($value, $key, $primaryKey)) continue;
30
+			if ($this->generalEditor->isNotSavableType($value, $key, $primaryKey)) {
31
+				continue;
32
+			}
31 33
 
32 34
 			$type = $this->generalEditor->getType($value);
33 35
 
34 36
 			try {
35
-				if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) throw new \Exception('Could not alter table');
36
-			}
37
-			catch (\Exception $e) {
37
+				if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) {
38
+					throw new \Exception('Could not alter table');
39
+				}
40
+			} catch (\Exception $e) {
38 41
 				$this->pdo->query('ALTER TABLE ' . $table . ' MODIFY ' . $this->quote($key) . ' ' . $type);
39 42
 			}
40 43
 		}
@@ -57,7 +60,9 @@  discard block
 block discarded – undo
57 60
 		$keyName = $this->quote(implode('_', $fields));
58 61
 
59 62
 		$results = $this->pdo->query('SHOW INDEX FROM ' . $this->quote($table) . ' WHERE Key_Name = "' . $keyName . '"');
60
-		if ($results && count($results->fetchAll()) == 0)  $this->pdo->query('CREATE INDEX ' . $keyName . ' ON ' . $this->quote($table) . ' (' . implode(', ', $fields) . ')');
63
+		if ($results && count($results->fetchAll()) == 0) {
64
+			$this->pdo->query('CREATE INDEX ' . $keyName . ' ON ' . $this->quote($table) . ' (' . implode(', ', $fields) . ')');
65
+		}
61 66
 	}
62 67
 
63 68
 	public function optimiseColumns($table) {
Please login to merge, or discard this patch.
maphper/datasource/GeneralEditDatabase.php 2 patches
Indentation   +32 added lines, -32 removed lines patch added patch discarded remove patch
@@ -2,55 +2,55 @@
 block discarded – undo
2 2
 namespace Maphper\DataSource;
3 3
 
4 4
 class GeneralEditDatabase {
5
-    private $pdo;
6
-    private $dataTypes = [
7
-        'datetime' => 'DATETIME',
8
-        'int' => 'INT(11)',
9
-        'decimal' => 'DECIMAL',
10
-        'short_string' => 'VARCHAR',
11
-        'long_string' => 'LONGBLOG',
12
-        'short_string_max_len' => 255,
13
-        'other' => 'VARCHAR(255)',
5
+	private $pdo;
6
+	private $dataTypes = [
7
+		'datetime' => 'DATETIME',
8
+		'int' => 'INT(11)',
9
+		'decimal' => 'DECIMAL',
10
+		'short_string' => 'VARCHAR',
11
+		'long_string' => 'LONGBLOG',
12
+		'short_string_max_len' => 255,
13
+		'other' => 'VARCHAR(255)',
14 14
 
15
-        'pk_default' => 'INT(11) NOT NULL AUTO_INCREMENT'
16
-    ];
15
+		'pk_default' => 'INT(11) NOT NULL AUTO_INCREMENT'
16
+	];
17 17
 
18
-    public function __construct(\PDO $pdo, array $dataTypes) {
19
-        $this->pdo = $pdo;
20
-        $this->dataTypes = array_merge($this->dataTypes, $dataTypes);
21
-    }
18
+	public function __construct(\PDO $pdo, array $dataTypes) {
19
+		$this->pdo = $pdo;
20
+		$this->dataTypes = array_merge($this->dataTypes, $dataTypes);
21
+	}
22 22
 
23
-    public function quote($str) {
23
+	public function quote($str) {
24 24
 		return '`' . str_replace('.', '`.`', trim($str, '`')) . '`';
25 25
 	}
26 26
 
27
-    public function getType($val) {
27
+	public function getType($val) {
28 28
 		if ($val instanceof \DateTime) return $this->dataTypes['datetime'];
29 29
 		else if ($result = $this->doNumberTypes($val)) return $result;
30 30
 		else if ($result = $this->doStringTypes($val)) return $result;
31 31
 		else return $this->dataTypes['other'];
32 32
 	}
33 33
 
34
-    private function doNumberTypes($val) {
35
-        if (is_int($val)) return $this->dataTypes['int'];
34
+	private function doNumberTypes($val) {
35
+		if (is_int($val)) return $this->dataTypes['int'];
36 36
 		else if (is_double($val)) return $this->dataTypes['decimal'] . '(9,' . strlen($val) - strrpos($val, '.') - 1 . ')';
37
-        else return false;
38
-    }
37
+		else return false;
38
+	}
39 39
 
40
-    private function doStringTypes($val) {
41
-        if (!is_string($val)) return false;
42
-        if (strlen($val) <= $this->dataTypes['short_string_max_len'])
43
-            return $this->dataTypes['short_string'] . '(' . $this->dataTypes['short_string_max_len'] . ')';
40
+	private function doStringTypes($val) {
41
+		if (!is_string($val)) return false;
42
+		if (strlen($val) <= $this->dataTypes['short_string_max_len'])
43
+			return $this->dataTypes['short_string'] . '(' . $this->dataTypes['short_string_max_len'] . ')';
44 44
 		else return $this->dataTypes['long_string'];
45
-    }
45
+	}
46 46
 
47
-    public function isNotSavableType($value, $key, $primaryKey) {
48
-        return is_array($value) || (is_object($value) && !($value instanceof \DateTime)) ||
49
-                in_array($key, $primaryKey);
50
-    }
47
+	public function isNotSavableType($value, $key, $primaryKey) {
48
+		return is_array($value) || (is_object($value) && !($value instanceof \DateTime)) ||
49
+				in_array($key, $primaryKey);
50
+	}
51 51
 
52
-    //Alter the database so that it can store $data
53
-    public function createTable($table, array $primaryKey, $data) {
52
+	//Alter the database so that it can store $data
53
+	public function createTable($table, array $primaryKey, $data) {
54 54
 		$parts = [];
55 55
 		foreach ($primaryKey as $key) {
56 56
 			$pk = $data->$key;
Please login to merge, or discard this patch.
Braces   +29 added lines, -13 removed lines patch added patch discarded remove patch
@@ -25,23 +25,36 @@  discard block
 block discarded – undo
25 25
 	}
26 26
 
27 27
     public function getType($val) {
28
-		if ($val instanceof \DateTime) return $this->dataTypes['datetime'];
29
-		else if ($result = $this->doNumberTypes($val)) return $result;
30
-		else if ($result = $this->doStringTypes($val)) return $result;
31
-		else return $this->dataTypes['other'];
28
+		if ($val instanceof \DateTime) {
29
+			return $this->dataTypes['datetime'];
30
+		} else if ($result = $this->doNumberTypes($val)) {
31
+			return $result;
32
+		} else if ($result = $this->doStringTypes($val)) {
33
+			return $result;
34
+		} else {
35
+			return $this->dataTypes['other'];
36
+		}
32 37
 	}
33 38
 
34 39
     private function doNumberTypes($val) {
35
-        if (is_int($val)) return $this->dataTypes['int'];
36
-		else if (is_double($val)) return $this->dataTypes['decimal'] . '(9,' . strlen($val) - strrpos($val, '.') - 1 . ')';
37
-        else return false;
40
+        if (is_int($val)) {
41
+        	return $this->dataTypes['int'];
42
+        } else if (is_double($val)) {
43
+			return $this->dataTypes['decimal'] . '(9,' . strlen($val) - strrpos($val, '.') - 1 . ')';
44
+		} else {
45
+        	return false;
46
+        }
38 47
     }
39 48
 
40 49
     private function doStringTypes($val) {
41
-        if (!is_string($val)) return false;
42
-        if (strlen($val) <= $this->dataTypes['short_string_max_len'])
43
-            return $this->dataTypes['short_string'] . '(' . $this->dataTypes['short_string_max_len'] . ')';
44
-		else return $this->dataTypes['long_string'];
50
+        if (!is_string($val)) {
51
+        	return false;
52
+        }
53
+        if (strlen($val) <= $this->dataTypes['short_string_max_len']) {
54
+                    return $this->dataTypes['short_string'] . '(' . $this->dataTypes['short_string_max_len'] . ')';
55
+        } else {
56
+			return $this->dataTypes['long_string'];
57
+		}
45 58
     }
46 59
 
47 60
     public function isNotSavableType($value, $key, $primaryKey) {
@@ -54,8 +67,11 @@  discard block
 block discarded – undo
54 67
 		$parts = [];
55 68
 		foreach ($primaryKey as $key) {
56 69
 			$pk = $data->$key;
57
-			if ($pk == null) $parts[] = $key . ' ' . $this->dataTypes['pk_default'];
58
-			else $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL';
70
+			if ($pk == null) {
71
+				$parts[] = $key . ' ' . $this->dataTypes['pk_default'];
72
+			} else {
73
+				$parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL';
74
+			}
59 75
 		}
60 76
 
61 77
 		$pkField = implode(', ', $parts) . ', PRIMARY KEY(' . implode(', ', $primaryKey) . ')';
Please login to merge, or discard this patch.
maphper/datasource/sqliteadapter.php 3 patches
Indentation   +23 added lines, -23 removed lines patch added patch discarded remove patch
@@ -4,15 +4,15 @@  discard block
 block discarded – undo
4 4
 class SqliteAdapter implements DatabaseAdapter {
5 5
 	private $pdo;
6 6
 	private $stmtCache;
7
-    private $generalEditor;
7
+	private $generalEditor;
8 8
 
9 9
 	public function __construct(\PDO $pdo) {
10 10
 		$this->pdo = $pdo;
11
-        $this->stmtCache = new StmtCache($pdo);
12
-        $this->generalEditor = new GeneralEditDatabase($this->pdo, [
13
-            'int' => 'INTEGER',
14
-            'pk_default' => 'INTEGER NOT NULL',
15
-        ]);
11
+		$this->stmtCache = new StmtCache($pdo);
12
+		$this->generalEditor = new GeneralEditDatabase($this->pdo, [
13
+			'int' => 'INTEGER',
14
+			'pk_default' => 'INTEGER NOT NULL',
15
+		]);
16 16
 	}
17 17
 
18 18
 	public function quote($str) {
@@ -20,19 +20,19 @@  discard block
 block discarded – undo
20 20
 	}
21 21
 
22 22
 	public function query(\Maphper\Lib\Query $query) {
23
-        $stmt = $this->stmtCache->getCachedStmt($query->getSql());
23
+		$stmt = $this->stmtCache->getCachedStmt($query->getSql());
24 24
 		$args = $query->getArgs();
25 25
 
26
-        //Handle SQLite when PDO_ERRMODE is set to SILENT
27
-        if ($stmt === false) throw new \Exception('Invalid query');
26
+		//Handle SQLite when PDO_ERRMODE is set to SILENT
27
+		if ($stmt === false) throw new \Exception('Invalid query');
28 28
 
29
-        $stmt->execute($args);
30
-        if ($stmt->errorCode() !== '00000' && $stmt->errorInfo()[2] == 'database schema has changed') {
29
+		$stmt->execute($args);
30
+		if ($stmt->errorCode() !== '00000' && $stmt->errorInfo()[2] == 'database schema has changed') {
31 31
 			$this->stmtCache->deleteQueryFromCache($query->getSql());
32 32
 			return $this->query($query);
33
-        }
33
+		}
34 34
 
35
-        return $stmt;
35
+		return $stmt;
36 36
 	}
37 37
 
38 38
 	public function lastInsertId() {
@@ -59,11 +59,11 @@  discard block
 block discarded – undo
59 59
 		// SQLSTATE[HY000]: General error: 17 database schema has changed
60 60
 		$this->stmtCache->clearCache();
61 61
 
62
-        // Create temp table to create a new structure
62
+		// Create temp table to create a new structure
63 63
 		$affix = '_'.substr(md5($table), 0, 6);
64
-        $tempTable = $table . $affix;
64
+		$tempTable = $table . $affix;
65 65
 		$this->generalEditor->createTable($tempTable, $primaryKey, $data);
66
-        $this->alterColumns($tempTable, $primaryKey, $data);
66
+		$this->alterColumns($tempTable, $primaryKey, $data);
67 67
 		$this->copyTableData($table, $tempTable);
68 68
 
69 69
 		$this->pdo->query('DROP TABLE IF EXISTS ' . $table );
@@ -71,8 +71,8 @@  discard block
 block discarded – undo
71 71
 
72 72
 	}
73 73
 
74
-    private function copyTableData($tableFrom, $tableTo) {
75
-        try {
74
+	private function copyTableData($tableFrom, $tableTo) {
75
+		try {
76 76
 			if ($this->tableExists($tableFrom)) {
77 77
 				$columns = implode(', ', $this->getColumns($tableFrom));
78 78
 
@@ -83,19 +83,19 @@  discard block
 block discarded – undo
83 83
 			// No data to copy
84 84
 			echo $e->getMessage();
85 85
 		}
86
-    }
86
+	}
87 87
 
88
-    private function alterColumns($table, array $primaryKey, $data) {
89
-        foreach ($data as $key => $value) {
88
+	private function alterColumns($table, array $primaryKey, $data) {
89
+		foreach ($data as $key => $value) {
90 90
 			if ($this->generalEditor->isNotSavableType($value, $key, $primaryKey)) continue;
91 91
 
92 92
 			$type = $this->generalEditor->getType($value);
93 93
 
94 94
 			$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type);
95 95
 		}
96
-    }
96
+	}
97 97
 
98
-    public function addIndex($table, array $fields) {
98
+	public function addIndex($table, array $fields) {
99 99
 		if (empty($fields)) return false;
100 100
 
101 101
 		//SQLite doesn't support ASC/DESC indexes, remove the keywords
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -40,7 +40,7 @@  discard block
 block discarded – undo
40 40
 	}
41 41
 
42 42
 	private function tableExists($name) {
43
-		$result = $this->pdo->query('SELECT name FROM sqlite_master WHERE type="table" and name="'. $name.'"');
43
+		$result = $this->pdo->query('SELECT name FROM sqlite_master WHERE type="table" and name="' . $name . '"');
44 44
 		return count($result->fetchAll()) == 1;
45 45
 	}
46 46
 
@@ -60,14 +60,14 @@  discard block
 block discarded – undo
60 60
 		$this->stmtCache->clearCache();
61 61
 
62 62
         // Create temp table to create a new structure
63
-		$affix = '_'.substr(md5($table), 0, 6);
63
+		$affix = '_' . substr(md5($table), 0, 6);
64 64
         $tempTable = $table . $affix;
65 65
 		$this->generalEditor->createTable($tempTable, $primaryKey, $data);
66 66
         $this->alterColumns($tempTable, $primaryKey, $data);
67 67
 		$this->copyTableData($table, $tempTable);
68 68
 
69
-		$this->pdo->query('DROP TABLE IF EXISTS ' . $table );
70
-		$this->pdo->query('ALTER TABLE ' . $tempTable . ' RENAME TO '. $table );
69
+		$this->pdo->query('DROP TABLE IF EXISTS ' . $table);
70
+		$this->pdo->query('ALTER TABLE ' . $tempTable . ' RENAME TO ' . $table);
71 71
 
72 72
 	}
73 73
 
Please login to merge, or discard this patch.
Braces   +14 added lines, -8 removed lines patch added patch discarded remove patch
@@ -24,7 +24,9 @@  discard block
 block discarded – undo
24 24
 		$args = $query->getArgs();
25 25
 
26 26
         //Handle SQLite when PDO_ERRMODE is set to SILENT
27
-        if ($stmt === false) throw new \Exception('Invalid query');
27
+        if ($stmt === false) {
28
+        	throw new \Exception('Invalid query');
29
+        }
28 30
 
29 31
         $stmt->execute($args);
30 32
         if ($stmt->errorCode() !== '00000' && $stmt->errorInfo()[2] == 'database schema has changed') {
@@ -78,8 +80,7 @@  discard block
 block discarded – undo
78 80
 
79 81
 				$this->pdo->query('INSERT INTO ' . $this->quote($tableTo) . '(' . $columns . ') SELECT ' . $columns . ' FROM ' . $this->quote($tableFrom));
80 82
 			}
81
-		}
82
-		catch (\PDOException $e) {
83
+		} catch (\PDOException $e) {
83 84
 			// No data to copy
84 85
 			echo $e->getMessage();
85 86
 		}
@@ -87,7 +88,9 @@  discard block
 block discarded – undo
87 88
 
88 89
     private function alterColumns($table, array $primaryKey, $data) {
89 90
         foreach ($data as $key => $value) {
90
-			if ($this->generalEditor->isNotSavableType($value, $key, $primaryKey)) continue;
91
+			if ($this->generalEditor->isNotSavableType($value, $key, $primaryKey)) {
92
+				continue;
93
+			}
91 94
 
92 95
 			$type = $this->generalEditor->getType($value);
93 96
 
@@ -96,10 +99,14 @@  discard block
 block discarded – undo
96 99
     }
97 100
 
98 101
     public function addIndex($table, array $fields) {
99
-		if (empty($fields)) return false;
102
+		if (empty($fields)) {
103
+			return false;
104
+		}
100 105
 
101 106
 		//SQLite doesn't support ASC/DESC indexes, remove the keywords
102
-		foreach ($fields as &$field) $field = str_ireplace([' desc', ' asc'], '', $field);
107
+		foreach ($fields as &$field) {
108
+			$field = str_ireplace([' desc', ' asc'], '', $field);
109
+		}
103 110
 		sort($fields);
104 111
 		$fields = array_map('strtolower', $fields);
105 112
 		$fields = array_map('trim', $fields);
@@ -108,8 +115,7 @@  discard block
 block discarded – undo
108 115
 
109 116
 		try {
110 117
 			$this->pdo->query('CREATE INDEX IF NOT EXISTS  ' . $keyName . ' ON ' . $table . ' (' . implode(', ', $fields) . ')');
111
-		}
112
-		catch (\Exception $e) {
118
+		} catch (\Exception $e) {
113 119
 
114 120
 		}
115 121
 	}
Please login to merge, or discard this patch.