Passed
Push — master ( 66acfb...380864 )
by Richard
01:44
created
maphper/lib/dateinjector.php 1 patch
Braces   +25 added lines, -11 removed lines patch added patch discarded remove patch
@@ -6,19 +6,26 @@  discard block
 block discarded – undo
6 6
 
7 7
 	public function replaceDates($obj, $reset = true) {
8 8
 		//prevent infinite recursion, only process each object once
9
-		if ($this->checkCache($obj, $reset)) return $obj;
10
-
11
-		if ($this->isIterable($obj)) foreach ($obj as &$o) $o = $this->replaceDates($o, false);
12
-		if ($this->isPossiblyDateString($obj)) $obj = $this->tryToGetDateObjFromString($obj);
9
+		if ($this->checkCache($obj, $reset)) {
10
+			return $obj;
11
+		}
12
+
13
+		if ($this->isIterable($obj)) {
14
+			foreach ($obj as &$o) $o = $this->replaceDates($o, false);
15
+		}
16
+		if ($this->isPossiblyDateString($obj)) {
17
+			$obj = $this->tryToGetDateObjFromString($obj);
18
+		}
13 19
 		return $obj;
14 20
 	}
15 21
 
16 22
     private function tryToGetDateObjFromString($obj) {
17 23
         try {
18 24
             $date = new \DateTime($obj);
19
-            if ($date->format('Y-m-d H:i:s') == substr($obj, 0, 20) || $date->format('Y-m-d') == substr($obj, 0, 10)) $obj = $date;
20
-        }
21
-        catch (\Exception $e) {	//Doesn't need to do anything as the try/catch is working out whether $obj is a date
25
+            if ($date->format('Y-m-d H:i:s') == substr($obj, 0, 20) || $date->format('Y-m-d') == substr($obj, 0, 10)) {
26
+            	$obj = $date;
27
+            }
28
+        } catch (\Exception $e) {	//Doesn't need to do anything as the try/catch is working out whether $obj is a date
22 29
         }
23 30
         return $obj;
24 31
     }
@@ -32,11 +39,18 @@  discard block
 block discarded – undo
32 39
     }
33 40
 
34 41
 	private function checkCache($obj, $reset) {
35
-		if ($reset) $this->processCache = new \SplObjectStorage();
36
-        if (!is_object($obj)) return false;
42
+		if ($reset) {
43
+			$this->processCache = new \SplObjectStorage();
44
+		}
45
+        if (!is_object($obj)) {
46
+        	return false;
47
+        }
37 48
 
38
-		if ($this->processCache->contains($obj)) return $obj;
39
-		else $this->processCache->attach($obj, true);
49
+		if ($this->processCache->contains($obj)) {
50
+			return $obj;
51
+		} else {
52
+			$this->processCache->attach($obj, true);
53
+		}
40 54
 
41 55
 		return false;
42 56
 	}
Please login to merge, or discard this patch.
maphper/lib/visibilityoverride.php 1 patch
Braces   +1 added lines, -2 removed lines patch added patch discarded remove patch
@@ -9,8 +9,7 @@
 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
             $visOverride = $this;
15 14
 			$this->readClosure = function() use ($visOverride) {
16 15
                 return (object) array_filter(get_object_vars($this), [$visOverride, 'isReturnableDataType']);
Please login to merge, or discard this patch.
maphper/lib/entity.php 1 patch
Braces   +6 added lines, -2 removed lines patch added patch discarded remove patch
@@ -21,14 +21,18 @@
 block discarded – undo
21 21
 
22 22
 	public function wrap($relations, $object, $siblings = []) {
23 23
 		//see if any relations need overwriting
24
-		foreach ($relations as $name => $relation) $this->addRelationData($object, $name, $relation, $siblings);
24
+		foreach ($relations as $name => $relation) {
25
+			$this->addRelationData($object, $name, $relation, $siblings);
26
+		}
25 27
 		return $object;
26 28
 	}
27 29
 
28 30
     private function addRelationData($object, $name, $relation, $siblings) {
29 31
         if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) {
30 32
             //After overwriting the relation, does the parent object ($object) need overwriting as well?
31
-            if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object;
33
+            if ($relation->overwrite($object, $object->$name)) {
34
+            	$this->parent[] = $object;
35
+            }
32 36
         }
33 37
 
34 38
         $object->$name = $relation->getData($object, $siblings);
Please login to merge, or discard this patch.
maphper/lib/Sql/WhereBuilder.php 1 patch
Braces   +25 added lines, -10 removed lines patch added patch discarded remove patch
@@ -13,7 +13,9 @@  discard block
 block discarded – undo
13 13
             'Maphper\Lib\Sql\GeneralOperator'
14 14
         ];
15 15
 
16
-        foreach ($defaultConditionals as $conditional) $this->addConditional(new $conditional);
16
+        foreach ($defaultConditionals as $conditional) {
17
+        	$this->addConditional(new $conditional);
18
+        }
17 19
     }
18 20
 
19 21
     public function addConditional(WhereConditional $conditional) {
@@ -27,7 +29,9 @@  discard block
 block discarded – undo
27 29
         foreach ($fields as $key => $value) {
28 30
             $value = $this->convertDates($value);
29 31
 
30
-            if (is_object($value)) continue;
32
+            if (is_object($value)) {
33
+            	continue;
34
+            }
31 35
 			$result = $this->getResult($key, $value, $mode);
32 36
             $sql = array_merge($sql, (array)$result['sql']);
33 37
             $args = array_merge($args, $result['args']);
@@ -40,29 +44,40 @@  discard block
 block discarded – undo
40 44
      * Either get sql from a conditional or call createSql again because the mode needs to be changed
41 45
      */
42 46
     private function getResult($key, $value, $mode) {
43
-        if (is_numeric($key) && is_array($value)) return $this->createSql($value, $key);
47
+        if (is_numeric($key) && is_array($value)) {
48
+        	return $this->createSql($value, $key);
49
+        }
44 50
         return $this->getConditional($key, $value, $mode);
45 51
     }
46 52
 
47 53
     private function sqlArrayToString($sql, $mode) {
48
-        if (\Maphper\Maphper::FIND_OR & $mode) $query = implode(' OR  ', $sql);
49
-		else $query = implode(' AND ', $sql);
50
-		if (!empty($query)) $query = '(' . $query . ')';
54
+        if (\Maphper\Maphper::FIND_OR & $mode) {
55
+        	$query = implode(' OR  ', $sql);
56
+        } else {
57
+			$query = implode(' AND ', $sql);
58
+		}
59
+		if (!empty($query)) {
60
+			$query = '(' . $query . ')';
61
+		}
51 62
         return $query;
52 63
     }
53 64
 
54 65
     private function getConditional($key, $value, $mode) {
55 66
         foreach ($this->conditionals as $conditional) {
56
-            if ($conditional->matches($key, $value, $mode))
57
-                return $conditional->getSql($key, $value, $mode);
67
+            if ($conditional->matches($key, $value, $mode)) {
68
+                            return $conditional->getSql($key, $value, $mode);
69
+            }
58 70
         }
59 71
         throw new \Exception("Invalid WHERE query");
60 72
     }
61 73
 
62 74
     private function convertDates($value) {
63 75
         if ($value instanceof \DateTime) {
64
-            if ($value->format('H:i:s')  == '00:00:00') $value = $value->format('Y-m-d');
65
-            else $value = $value->format('Y-m-d H:i:s');
76
+            if ($value->format('H:i:s')  == '00:00:00') {
77
+            	$value = $value->format('Y-m-d');
78
+            } else {
79
+            	$value = $value->format('Y-m-d H:i:s');
80
+            }
66 81
         }
67 82
         return $value;
68 83
     }
Please login to merge, or discard this patch.
maphper/lib/ArrayFilter.php 1 patch
Braces   +26 added lines, -14 removed lines patch added patch discarded remove patch
@@ -21,8 +21,11 @@  discard block
 block discarded – undo
21 21
             foreach ($fields as $key => $val) {
22 22
                 $currentFieldResult = $this->getIfFieldMatches($key, $val, $data, $mode);
23 23
 
24
-                if (Maphper::FIND_OR & $mode && $currentFieldResult === true) return true;
25
-                else if (Maphper::FIND_OR ^ $mode && $currentFieldResult === false) return false;
24
+                if (Maphper::FIND_OR & $mode && $currentFieldResult === true) {
25
+                	return true;
26
+                } else if (Maphper::FIND_OR ^ $mode && $currentFieldResult === false) {
27
+                	return false;
28
+                }
26 29
             }
27 30
             return (bool)(Maphper::FIND_OR ^ $mode);
28 31
         };
@@ -31,12 +34,13 @@  discard block
 block discarded – undo
31 34
     private function getIfFieldMatches($key, $val, $data, $mode) {
32 35
         if ($this->shouldRunDeepSearch($key, $val)) {
33 36
             return $this->getSearchFieldFunction($val, $key)($data);
37
+        } else if (!isset($data->$key)) {
38
+        	return false;
39
+        } else if ($this->isInArraySearch($mode, $key, $val)) {
40
+                    return in_array($data->$key, $val);
41
+        } else {
42
+                    return $this->processFilter($mode, $val, $data->$key);
34 43
         }
35
-        else if (!isset($data->$key)) return false;
36
-        else if ($this->isInArraySearch($mode, $key, $val))
37
-            return in_array($data->$key, $val);
38
-        else
39
-            return $this->processFilter($mode, $val, $data->$key);
40 44
     }
41 45
 
42 46
     private function shouldRunDeepSearch($key, $val) {
@@ -48,13 +52,21 @@  discard block
 block discarded – undo
48 52
     }
49 53
 
50 54
     private function processFilter($mode, $expected, $actual) {
51
-        if (Maphper::FIND_NOT & $mode) return $expected != $actual;
52
-        else if ((Maphper::FIND_GREATER | Maphper::FIND_EXACT) === $mode) return $expected <= $actual;
53
-        else if ((Maphper::FIND_LESS | Maphper::FIND_EXACT) === $mode) return $expected >= $actual;
54
-        else if (Maphper::FIND_GREATER & $mode) return $expected < $actual;
55
-        else if (Maphper::FIND_LESS & $mode) return $expected > $actual;
56
-        else if (Maphper::FIND_BETWEEN & $mode) return $expected[0] <= $actual && $actual <= $expected[1];
57
-        else if (Maphper::FIND_NOCASE & $mode) return strtolower($expected) == strtolower($actual);
55
+        if (Maphper::FIND_NOT & $mode) {
56
+        	return $expected != $actual;
57
+        } else if ((Maphper::FIND_GREATER | Maphper::FIND_EXACT) === $mode) {
58
+        	return $expected <= $actual;
59
+        } else if ((Maphper::FIND_LESS | Maphper::FIND_EXACT) === $mode) {
60
+        	return $expected >= $actual;
61
+        } else if (Maphper::FIND_GREATER & $mode) {
62
+        	return $expected < $actual;
63
+        } else if (Maphper::FIND_LESS & $mode) {
64
+        	return $expected > $actual;
65
+        } else if (Maphper::FIND_BETWEEN & $mode) {
66
+        	return $expected[0] <= $actual && $actual <= $expected[1];
67
+        } else if (Maphper::FIND_NOCASE & $mode) {
68
+        	return strtolower($expected) == strtolower($actual);
69
+        }
58 70
         return $expected == $actual;
59 71
     }
60 72
 }
Please login to merge, or discard this patch.
maphper/datasource/mysqladapter.php 1 patch
Braces   +24 added lines, -11 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) . ')';
@@ -46,14 +54,17 @@  discard block
 block discarded – undo
46 54
 
47 55
     private function alterColumns($table, array $primaryKey, $data) {
48 56
         foreach ($data as $key => $value) {
49
-			if ($this->isNotSavableType($value, $key, $primaryKey)) continue;
57
+			if ($this->isNotSavableType($value, $key, $primaryKey)) {
58
+				continue;
59
+			}
50 60
 
51 61
 			$type = $this->getType($value);
52 62
 
53 63
 			try {
54
-				if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) throw new \Exception('Could not alter table');
55
-			}
56
-			catch (\Exception $e) {
64
+				if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) {
65
+					throw new \Exception('Could not alter table');
66
+				}
67
+			} catch (\Exception $e) {
57 68
 				$this->pdo->query('ALTER TABLE ' . $table . ' MODIFY ' . $this->quote($key) . ' ' . $type);
58 69
 			}
59 70
 		}
@@ -81,7 +92,9 @@  discard block
 block discarded – undo
81 92
 		$keyName = $this->quote(implode('_', $fields));
82 93
 
83 94
 		$results = $this->pdo->query('SHOW INDEX FROM ' . $this->quote($table) . ' WHERE Key_Name = "' . $keyName . '"');
84
-		if ($results && count($results->fetchAll()) == 0)  $this->pdo->query('CREATE INDEX ' . $keyName . ' ON ' . $this->quote($table) . ' (' . implode(', ', $fields) . ')');
95
+		if ($results && count($results->fetchAll()) == 0) {
96
+			$this->pdo->query('CREATE INDEX ' . $keyName . ' ON ' . $this->quote($table) . ' (' . implode(', ', $fields) . ')');
97
+		}
85 98
 	}
86 99
 
87 100
 	public function optimiseColumns($table) {
Please login to merge, or discard this patch.
maphper/datasource/sqliteadapter.php 1 patch
Braces   +32 added lines, -16 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) {
@@ -82,8 +91,7 @@  discard block
 block discarded – undo
82 91
 
83 92
 				$this->pdo->query('INSERT INTO ' . $this->quote($tableTo) . '(' . $columns . ') SELECT ' . $columns . ' FROM ' . $this->quote($tableFrom));
84 93
 			}
85
-		}
86
-		catch (\PDOException $e) {
94
+		} catch (\PDOException $e) {
87 95
 			// No data to copy
88 96
 			echo $e->getMessage();
89 97
 		}
@@ -93,8 +101,11 @@  discard block
 block discarded – undo
93 101
 		$parts = [];
94 102
 		foreach ($primaryKey as $key) {
95 103
 			$pk = $data->$key;
96
-			if ($pk == null) $parts[] = $key . ' INTEGER'; 
97
-			else $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL';					
104
+			if ($pk == null) {
105
+				$parts[] = $key . ' INTEGER';
106
+			} else {
107
+				$parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL';
108
+			}
98 109
 		}
99 110
 		
100 111
 		$pkField = implode(', ', $parts) . ', PRIMARY KEY(' . implode(', ', $primaryKey) . ')';
@@ -104,7 +115,9 @@  discard block
 block discarded – undo
104 115
 
105 116
     private function alterColumns($table, array $primaryKey, $data) {
106 117
         foreach ($data as $key => $value) {
107
-			if ($this->isNotSavableType($value, $key, $primaryKey)) continue;
118
+			if ($this->isNotSavableType($value, $key, $primaryKey)) {
119
+				continue;
120
+			}
108 121
 
109 122
 			$type = $this->getType($value);
110 123
 		
@@ -118,10 +131,14 @@  discard block
 block discarded – undo
118 131
     }
119 132
 
120 133
 	public function addIndex($table, array $fields) {
121
-		if (empty($fields)) return false;
134
+		if (empty($fields)) {
135
+			return false;
136
+		}
122 137
 		
123 138
 		//SQLite doesn't support ASC/DESC indexes, remove the keywords
124
-		foreach ($fields as &$field) $field = str_ireplace([' desc', ' asc'], '', $field);
139
+		foreach ($fields as &$field) {
140
+			$field = str_ireplace([' desc', ' asc'], '', $field);
141
+		}
125 142
 		sort($fields);
126 143
 		$fields = array_map('strtolower', $fields);
127 144
 		$fields = array_map('trim', $fields);
@@ -130,8 +147,7 @@  discard block
 block discarded – undo
130 147
 		
131 148
 		try {
132 149
 			$this->pdo->query('CREATE INDEX IF NOT EXISTS  ' . $keyName . ' ON ' . $table . ' (' . implode(', ', $fields) . ')');
133
-		}
134
-		catch (\Exception $e) {
150
+		} catch (\Exception $e) {
135 151
 			
136 152
 		}
137 153
 	}
Please login to merge, or discard this patch.