Passed
Pull Request — master (#69)
by Christian
02:08
created
Maphper/DataSource/DatabaseSelect.php 1 patch
Braces   +46 added lines, -22 removed lines patch added patch discarded remove patch
@@ -25,17 +25,22 @@  discard block
 block discarded – undo
25 25
 		if (($this->cacheMode && !isset($this->idCache[$id])) || !$this->cacheMode) {
26 26
 			try {
27 27
 				$result = $this->selectQuery($this->selectBuilder->select($this->table, $pk . ' = :id', [':id' => $id], ['limit' => 1]));
28
-			}
29
-			catch (\Exception $e) {
28
+			} catch (\Exception $e) {
30 29
                 // Don't issue an error if it cannot be found since we return null
31 30
 			}
32 31
 
33
-			if (isset($result[0])) $result = $result[0];
34
-			else return null;
32
+			if (isset($result[0])) {
33
+				$result = $result[0];
34
+			} else {
35
+				return null;
36
+			}
35 37
 		}
36 38
 
37
-    if (!$this->cacheMode) return $result;
38
-		else return $this->idCache[$id] = $result;
39
+    if (!$this->cacheMode) {
40
+    	return $result;
41
+    } else {
42
+			return $this->idCache[$id] = $result;
43
+		}
39 44
 	}
40 45
 
41 46
     public function findByField(array $fields, $options = []) {
@@ -44,22 +49,27 @@  discard block
 block discarded – undo
44 49
     if (($this->cacheMode && !isset($this->resultCache[$cacheId])) || !$this->cacheMode) {
45 50
 			$query = $this->whereBuilder->createSql($fields);
46 51
 
47
-			if (!isset($options['order'])) $options['order'] = $this->defaultSort;
52
+			if (!isset($options['order'])) {
53
+				$options['order'] = $this->defaultSort;
54
+			}
48 55
 
49 56
 			try {
50 57
 				$result = $this->selectQuery($this->selectBuilder->select($this->table, $query['sql'], $query['args'], $options));
51 58
 				$this->databaseModify->addIndex(array_keys($query['args']));
52 59
 				$this->databaseModify->addIndex(explode(',', $options['order']));
53
-			}
54
-			catch (\Exception $e) {
60
+			} catch (\Exception $e) {
55 61
 				$this->errors[] = $e;
56 62
 				$result = [];
57 63
 			}
58 64
 		}
59 65
 
60 66
     if ($this->cacheMode) {
61
-      if (isset($result)) $this->resultCache[$cacheId] = $result;
62
-      if (isset($this->resultCache[$cacheId])) return $this->resultCache[$cacheId];
67
+      if (isset($result)) {
68
+      	$this->resultCache[$cacheId] = $result;
69
+      }
70
+      if (isset($this->resultCache[$cacheId])) {
71
+      	return $this->resultCache[$cacheId];
72
+      }
63 73
     }
64 74
 
65 75
     return $result;
@@ -67,7 +77,9 @@  discard block
 block discarded – undo
67 77
 
68 78
     public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) {
69 79
 		//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
70
-		if (is_array($field)) $field = $field[0];
80
+		if (is_array($field)) {
81
+			$field = $field[0];
82
+		}
71 83
 		$query = $this->whereBuilder->createSql($criteria);
72 84
 
73 85
 		try {
@@ -76,8 +88,7 @@  discard block
 block discarded – undo
76 88
 			$result = $this->selectQuery($this->selectBuilder->aggregate($this->table, $function, $field, $query['sql'], $query['args'], $group));
77 89
 
78 90
 			return $this->determineAggregateResult($result, $group, $field);
79
-		}
80
-		catch (\Exception $e) {
91
+		} catch (\Exception $e) {
81 92
 			return $group ? [] : 0;
82 93
 		}
83 94
 	}
@@ -85,11 +96,15 @@  discard block
 block discarded – undo
85 96
     private function determineAggregateResult($result, $group, $field) {
86 97
         if ($group != null) {
87 98
             $ret = [];
88
-            foreach ($result as $res) $ret[$res->$field] = $res->val;
99
+            foreach ($result as $res) {
100
+            	$ret[$res->$field] = $res->val;
101
+            }
89 102
             return $ret;
103
+        } else if (isset($result[0])) {
104
+        	return $result[0]->val;
105
+        } else {
106
+        	return 0;
90 107
         }
91
-        else if (isset($result[0])) return $result[0]->val;
92
-        else return 0;
93 108
     }
94 109
 
95 110
     private function selectQuery(\Maphper\Lib\Query $query) {
@@ -97,21 +112,30 @@  discard block
 block discarded – undo
97 112
     }
98 113
 
99 114
     public function clearResultCache() {
100
-        if ($this->cacheMode) $this->resultCache = [];
115
+        if ($this->cacheMode) {
116
+        	$this->resultCache = [];
117
+        }
101 118
     }
102 119
 
103 120
     public function clearIDCache() {
104
-        if ($this->cacheMode) $this->idCache = [];
121
+        if ($this->cacheMode) {
122
+        	$this->idCache = [];
123
+        }
105 124
     }
106 125
 
107 126
     public function updateCache($data, $pkValue) {
108 127
         if ($this->cacheMode) {
109
-  		    if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data);
110
-  		    else $this->cache[$pkValue] = $data;
128
+  		    if (isset($this->cache[$pkValue])) {
129
+  		    	$this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data);
130
+  		    } else {
131
+  		    	$this->cache[$pkValue] = $data;
132
+  		    }
111 133
         }
112 134
     }
113 135
 
114 136
     public function deleteIDFromCache($id) {
115
-        if ($this->cacheMode) unset($this->idCache[$id]);
137
+        if ($this->cacheMode) {
138
+        	unset($this->idCache[$id]);
139
+        }
116 140
     }
117 141
 }
Please login to merge, or discard this patch.