Passed
Pull Request — master (#70)
by Christian
02:13 queued 50s
created
Maphper/DataSource/GeneralEditDatabase.php 1 patch
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 \DateTimeInterface) 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 \DateTimeInterface)) ||
49
-                in_array($key, $primaryKey);
50
-    }
47
+	public function isNotSavableType($value, $key, $primaryKey) {
48
+		return is_array($value) || (is_object($value) && !($value instanceof \DateTimeInterface)) ||
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.
Maphper/Lib/DateInjector.php 1 patch
Indentation   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -18,15 +18,15 @@  discard block
 block discarded – undo
18 18
 		return $obj;
19 19
 	}
20 20
 
21
-    private function tryToGetDateObjFromString($obj) {
22
-        try {
23
-            $date = new \DateTimeImmutable($obj);
21
+	private function tryToGetDateObjFromString($obj) {
22
+		try {
23
+			$date = new \DateTimeImmutable($obj);
24 24
 			if ($this->dateMatchesFormats($date, $obj)) $obj = $date;
25
-        }
26
-        catch (\Exception $e) {	//Doesn't need to do anything as the try/catch is working out whether $obj is a date
27
-        }
28
-        return $obj;
29
-    }
25
+		}
26
+		catch (\Exception $e) {	//Doesn't need to do anything as the try/catch is working out whether $obj is a date
27
+		}
28
+		return $obj;
29
+	}
30 30
 
31 31
 	private function dateMatchesFormats($date, $str) {
32 32
 		foreach ($this->dateFormats as list($format, $len)) {
@@ -35,17 +35,17 @@  discard block
 block discarded – undo
35 35
 		return false;
36 36
 	}
37 37
 
38
-    private function isIterable($obj) {
39
-        return is_array($obj) || (is_object($obj) && ($obj instanceof \Iterator));
40
-    }
38
+	private function isIterable($obj) {
39
+		return is_array($obj) || (is_object($obj) && ($obj instanceof \Iterator));
40
+	}
41 41
 
42
-    private function isPossiblyDateString($obj) {
43
-        return is_string($obj) && isset($obj[0]) && is_numeric($obj[0]) && strlen($obj) <= 20;
44
-    }
42
+	private function isPossiblyDateString($obj) {
43
+		return is_string($obj) && isset($obj[0]) && is_numeric($obj[0]) && strlen($obj) <= 20;
44
+	}
45 45
 
46 46
 	private function checkCache($obj, $reset) {
47 47
 		if ($reset) $this->processCache = new \SplObjectStorage();
48
-        if (!is_object($obj)) return false;
48
+		if (!is_object($obj)) return false;
49 49
 
50 50
 		if ($this->processCache->contains($obj)) return $obj;
51 51
 		else $this->processCache->attach($obj, true);
Please login to merge, or discard this patch.
Maphper/Lib/VisibilityOverride.php 1 patch
Indentation   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -11,9 +11,9 @@  discard block
 block discarded – undo
11 11
 			$this->writeClosure = function ($field, $value) use ($object) { $object->$field = $value; };
12 12
 		}
13 13
 		else {
14
-            $visOverride = $this;
14
+			$visOverride = $this;
15 15
 			$this->readClosure = function() use ($visOverride) {
16
-                return (object) array_filter(get_object_vars($this), [$visOverride, 'isReturnableDataType']);
16
+				return (object) array_filter(get_object_vars($this), [$visOverride, 'isReturnableDataType']);
17 17
 			};
18 18
 			$this->readClosure = $this->readClosure->bindTo($object, $object);
19 19
 
@@ -23,9 +23,9 @@  discard block
 block discarded – undo
23 23
 
24 24
 	}
25 25
 
26
-    public function isReturnableDataType($v) {
27
-        return is_scalar($v) || is_null($v) || (is_object($v) && $v instanceof \DateTimeInterface);
28
-    }
26
+	public function isReturnableDataType($v) {
27
+		return is_scalar($v) || is_null($v) || (is_object($v) && $v instanceof \DateTimeInterface);
28
+	}
29 29
 
30 30
 	public function getProperties() {
31 31
 		return ($this->readClosure)();
Please login to merge, or discard this patch.
Maphper/DataSource/Database.php 1 patch
Indentation   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -9,8 +9,8 @@  discard block
 block discarded – undo
9 9
 	private $table;
10 10
 
11 11
 	private $fields = '*';
12
-    private $databaseSelect;
13
-    private $databaseCrud;
12
+	private $databaseSelect;
13
+	private $databaseCrud;
14 14
 
15 15
 	public function __construct($db, $table, $primaryKey = 'id', array $options = []) {
16 16
 		$options = new DatabaseOptions($db, $options);
@@ -23,10 +23,10 @@  discard block
 block discarded – undo
23 23
 
24 24
 		$defaultSort = $options->read('defaultSort') !== false ? $options->read('defaultSort')  : implode(', ', $this->primaryKey);
25 25
 
26
-        $databaseModify = new DatabaseModify($adapter, $options->getEditMode(), $table);
26
+		$databaseModify = new DatabaseModify($adapter, $options->getEditMode(), $table);
27 27
 
28
-        $this->databaseSelect = new DatabaseSelect($adapter, $databaseModify, $table, $defaultSort, $options->getCacheMode());
29
-        $this->databaseCrud = new DatabaseCrud($adapter, $databaseModify, $this->databaseSelect, $table, $this->primaryKey);
28
+		$this->databaseSelect = new DatabaseSelect($adapter, $databaseModify, $table, $defaultSort, $options->getCacheMode());
29
+		$this->databaseCrud = new DatabaseCrud($adapter, $databaseModify, $this->databaseSelect, $table, $this->primaryKey);
30 30
 
31 31
 		$databaseModify->optimizeColumns();
32 32
 	}
@@ -52,7 +52,7 @@  discard block
 block discarded – undo
52 52
 	}
53 53
 
54 54
 	public function findByField(array $fields, $options = []) {
55
-        return $this->databaseSelect->findByField($fields, $options);
55
+		return $this->databaseSelect->findByField($fields, $options);
56 56
 	}
57 57
 
58 58
 	public function deleteByField(array $fields, array $options = []) {
@@ -60,6 +60,6 @@  discard block
 block discarded – undo
60 60
 	}
61 61
 
62 62
 	public function save($data) {
63
-        $this->databaseCrud->save($data, true);
63
+		$this->databaseCrud->save($data, true);
64 64
 	}
65 65
 }
Please login to merge, or discard this patch.
Maphper/DataSource/DatabaseSelect.php 1 patch
Indentation   +83 added lines, -83 removed lines patch added patch discarded remove patch
@@ -2,41 +2,41 @@  discard block
 block discarded – undo
2 2
 namespace Maphper\DataSource;
3 3
 
4 4
 class DatabaseSelect {
5
-    private $resultCache = [];
6
-    private $idCache = [];
7
-    private $idCacheTime = [];
8
-    private $selectBuilder;
9
-    private $whereBuilder;
10
-    private $adapter;
11
-    private $databaseModify;
12
-    private $defaultSort;
13
-    private $table;
14
-
15
-    public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify, $table, $defaultSort, $cacheMode) {
16
-        $this->adapter = $adapter;
17
-        $this->databaseModify = $databaseModify;
18
-        $this->selectBuilder = new \Maphper\Lib\SelectBuilder();
19
-        $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder();
20
-        $this->defaultSort = $defaultSort;
21
-        $this->cacheMode = $cacheMode;
22
-        $this->table = $table;
23
-    }
24
-
25
-    public function getColumns($table) {
26
-      return $this->adapter->getColumns($table);
27
-    }
28
-
29
-    private function cacheUpdateRequired($id) {
30
-      if ($this->cacheMode > 0) {
31
-        if (!isset($this->idCacheTime[$id])) return true; // Cache time has not been set for first time
32
-
33
-        if (time() - $this->idCacheTime[$id] > $this->cacheMode) return true; // Cache time has expired
34
-      }
35
-
36
-      if (!isset($this->idCache[$id])) return true; // Cache has not been set for first time
37
-
38
-      return false;
39
-    }
5
+	private $resultCache = [];
6
+	private $idCache = [];
7
+	private $idCacheTime = [];
8
+	private $selectBuilder;
9
+	private $whereBuilder;
10
+	private $adapter;
11
+	private $databaseModify;
12
+	private $defaultSort;
13
+	private $table;
14
+
15
+	public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify, $table, $defaultSort, $cacheMode) {
16
+		$this->adapter = $adapter;
17
+		$this->databaseModify = $databaseModify;
18
+		$this->selectBuilder = new \Maphper\Lib\SelectBuilder();
19
+		$this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder();
20
+		$this->defaultSort = $defaultSort;
21
+		$this->cacheMode = $cacheMode;
22
+		$this->table = $table;
23
+	}
24
+
25
+	public function getColumns($table) {
26
+	  return $this->adapter->getColumns($table);
27
+	}
28
+
29
+	private function cacheUpdateRequired($id) {
30
+	  if ($this->cacheMode > 0) {
31
+		if (!isset($this->idCacheTime[$id])) return true; // Cache time has not been set for first time
32
+
33
+		if (time() - $this->idCacheTime[$id] > $this->cacheMode) return true; // Cache time has expired
34
+	  }
35
+
36
+	  if (!isset($this->idCache[$id])) return true; // Cache has not been set for first time
37
+
38
+	  return false;
39
+	}
40 40
 
41 41
   public function findById($id, $pk) {
42 42
 		if ($this->cacheMode < 0 || $this->cacheUpdateRequired($id)) {
@@ -44,19 +44,19 @@  discard block
 block discarded – undo
44 44
 				$result = $this->selectQuery($this->selectBuilder->select($this->table, $pk . ' = :id', [':id' => $id], ['limit' => 1]));
45 45
 			}
46 46
 			catch (\Exception $e) {
47
-                // Don't issue an error if it cannot be found since we return null
47
+				// Don't issue an error if it cannot be found since we return null
48 48
 			}
49 49
 
50 50
 			if (isset($result[0])) $result = $result[0];
51 51
 			else return null;
52 52
 		}
53 53
 
54
-    if ($this->cacheMode < 0) return $result; // Cache mode is off
54
+	if ($this->cacheMode < 0) return $result; // Cache mode is off
55 55
 
56
-    if ($this->cacheUpdateRequired()) {
57
-      $this->idCache[$id] = $result;
58
-      $this->idCacheTime[$id] = time();
59
-    }
56
+	if ($this->cacheUpdateRequired()) {
57
+	  $this->idCache[$id] = $result;
58
+	  $this->idCacheTime[$id] = time();
59
+	}
60 60
 
61 61
 		return $this->idCache[$id];
62 62
 	}
@@ -64,7 +64,7 @@  discard block
 block discarded – undo
64 64
   public function findByField(array $fields, $options = []) {
65 65
 		$cacheId = md5(serialize(func_get_args()));
66 66
 
67
-    if ($this->cacheMode < 0 || $this->cacheUpdateRequired($cacheId)) {
67
+	if ($this->cacheMode < 0 || $this->cacheUpdateRequired($cacheId)) {
68 68
 			$query = $this->whereBuilder->createSql($fields);
69 69
 
70 70
 			if (!isset($options['order'])) $options['order'] = $this->defaultSort;
@@ -80,17 +80,17 @@  discard block
 block discarded – undo
80 80
 			}
81 81
 		}
82 82
 
83
-    if ($this->cacheMode < 0) return $result; // Cache mode is off
83
+	if ($this->cacheMode < 0) return $result; // Cache mode is off
84 84
 
85
-    if ($this->cacheUpdateRequired($cacheId)) {
86
-      $this->idCache[$cacheId] = $result;
87
-      $this->idCacheTime[$cacheId] = time();
88
-    }
85
+	if ($this->cacheUpdateRequired($cacheId)) {
86
+	  $this->idCache[$cacheId] = $result;
87
+	  $this->idCacheTime[$cacheId] = time();
88
+	}
89 89
 
90 90
 		return $this->idCache[$cacheId];
91 91
 	}
92 92
 
93
-    public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) {
93
+	public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) {
94 94
 		//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
95 95
 		if (is_array($field)) $field = $field[0];
96 96
 		$query = $this->whereBuilder->createSql($criteria);
@@ -107,39 +107,39 @@  discard block
 block discarded – undo
107 107
 		}
108 108
 	}
109 109
 
110
-    private function determineAggregateResult($result, $group, $field) {
111
-        if ($group != null) {
112
-            $ret = [];
113
-            foreach ($result as $res) $ret[$res->$field] = $res->val;
114
-            return $ret;
115
-        }
116
-        else if (isset($result[0])) return $result[0]->val;
117
-        else return 0;
118
-    }
119
-
120
-    private function selectQuery(\Maphper\Lib\Query $query) {
121
-        return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ);
122
-    }
123
-
124
-    public function clearResultCache() {
125
-        if ($this->cacheMode >= 0) $this->resultCache = [];
126
-    }
127
-
128
-    public function clearIDCache() {
129
-        if ($this->cacheMode >= 0) $this->idCache = [];
130
-    }
131
-
132
-    public function updateCache($data, $pkValue) {
133
-        if ($this->cacheMode >= 0) {
134
-  		    if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data);
135
-  		    else $this->cache[$pkValue] = $data;
136
-        }
137
-    }
138
-
139
-    public function deleteIDFromCache($id) {
140
-        if ($this->cacheMode >= 0) {
141
-          unset($this->idCache[$id]);
142
-          unset($this->idCacheTime[$id]);
143
-        }
144
-    }
110
+	private function determineAggregateResult($result, $group, $field) {
111
+		if ($group != null) {
112
+			$ret = [];
113
+			foreach ($result as $res) $ret[$res->$field] = $res->val;
114
+			return $ret;
115
+		}
116
+		else if (isset($result[0])) return $result[0]->val;
117
+		else return 0;
118
+	}
119
+
120
+	private function selectQuery(\Maphper\Lib\Query $query) {
121
+		return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ);
122
+	}
123
+
124
+	public function clearResultCache() {
125
+		if ($this->cacheMode >= 0) $this->resultCache = [];
126
+	}
127
+
128
+	public function clearIDCache() {
129
+		if ($this->cacheMode >= 0) $this->idCache = [];
130
+	}
131
+
132
+	public function updateCache($data, $pkValue) {
133
+		if ($this->cacheMode >= 0) {
134
+  			if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data);
135
+  			else $this->cache[$pkValue] = $data;
136
+		}
137
+	}
138
+
139
+	public function deleteIDFromCache($id) {
140
+		if ($this->cacheMode >= 0) {
141
+		  unset($this->idCache[$id]);
142
+		  unset($this->idCacheTime[$id]);
143
+		}
144
+	}
145 145
 }
Please login to merge, or discard this patch.
Maphper/DataSource/MysqlAdapter.php 1 patch
Indentation   +16 added lines, -16 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,32 +20,32 @@  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);
33 33
 			$this->tryAlteringColumn($table, $key, $type);
34 34
 		}
35
-    }
35
+	}
36 36
 
37
-    private function tryAlteringColumn($table, $key, $type) {
38
-        try {
39
-            if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) throw new \Exception('Could not alter table');
40
-        }
41
-        catch (\Exception $e) {
42
-            $this->pdo->query('ALTER TABLE ' . $table . ' MODIFY ' . $this->quote($key) . ' ' . $type);
43
-        }
44
-    }
37
+	private function tryAlteringColumn($table, $key, $type) {
38
+		try {
39
+			if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) throw new \Exception('Could not alter table');
40
+		}
41
+		catch (\Exception $e) {
42
+			$this->pdo->query('ALTER TABLE ' . $table . ' MODIFY ' . $this->quote($key) . ' ' . $type);
43
+		}
44
+	}
45 45
 
46 46
 	public function alterDatabase($table, array $primaryKey, $data) {
47 47
 		$this->generalEditor->createTable($table, $primaryKey, $data);
48
-        $this->alterColumns($table, $primaryKey, $data);
48
+		$this->alterColumns($table, $primaryKey, $data);
49 49
 	}
50 50
 
51 51
 	public function getColumns($table) {
Please login to merge, or discard this patch.