@@ -157,7 +157,7 @@ |
||
| 157 | 157 | |
| 158 | 158 | /** |
| 159 | 159 | * @param string $name |
| 160 | - * @param mixed $callback |
|
| 160 | + * @param string $callback |
|
| 161 | 161 | * @throws Exception |
| 162 | 162 | */ |
| 163 | 163 | private function registerUDFunction($name, $callback) { |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | */ |
| 80 | 80 | private function buildSchema($schema) { |
| 81 | 81 | $def = []; |
| 82 | - foreach($schema as $name => $type) { |
|
| 82 | + foreach ($schema as $name => $type) { |
|
| 83 | 83 | switch ($type) { |
| 84 | 84 | case 'BOOL': |
| 85 | 85 | $def[] = sprintf('CASE WHEN CAST(:'.$name.' AS INT) = 0 THEN \'false\' ELSE \'true\' END'); break; |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | $def[] = '\'"\'||md5(:'.$name.')||\'"\''; break; |
| 98 | 98 | } |
| 99 | 99 | } |
| 100 | - if(!count($def)) { |
|
| 100 | + if (!count($def)) { |
|
| 101 | 101 | throw new EmptySchemaException('Can\'t operate with empty schema'); |
| 102 | 102 | } |
| 103 | 103 | return join('||"|"||', $def); |
@@ -110,22 +110,22 @@ discard block |
||
| 110 | 110 | */ |
| 111 | 111 | private function buildConverter($schema) { |
| 112 | 112 | $def = []; |
| 113 | - foreach($schema as $name => $type) { |
|
| 113 | + foreach ($schema as $name => $type) { |
|
| 114 | 114 | switch ($type) { |
| 115 | 115 | case 'BOOL': |
| 116 | 116 | $def[$name] = 'boolval'; break; |
| 117 | 117 | case 'INT': |
| 118 | 118 | $def[$name] = 'intval'; break; |
| 119 | 119 | case 'FLOAT': |
| 120 | - $def[$name] = function ($value) { return number_format($value, 6, '.', ''); }; break; |
|
| 120 | + $def[$name] = function($value) { return number_format($value, 6, '.', ''); }; break; |
|
| 121 | 121 | case 'DOUBLE': |
| 122 | - $def[$name] = function ($value) { return number_format($value, 12, '.', ''); }; break; |
|
| 122 | + $def[$name] = function($value) { return number_format($value, 12, '.', ''); }; break; |
|
| 123 | 123 | case 'MONEY': |
| 124 | - $def[$name] = function ($value) { return number_format($value, 2, '.', ''); }; break; |
|
| 124 | + $def[$name] = function($value) { return number_format($value, 2, '.', ''); }; break; |
|
| 125 | 125 | case 'STRING': |
| 126 | - $def[$name] = function ($value) { return (string) $value; }; break; |
|
| 126 | + $def[$name] = function($value) { return (string) $value; }; break; |
|
| 127 | 127 | case 'MD5': |
| 128 | - $def[$name] = function ($value) { return md5((string) $value); }; break; |
|
| 128 | + $def[$name] = function($value) { return md5((string) $value); }; break; |
|
| 129 | 129 | } |
| 130 | 130 | } |
| 131 | 131 | return $def; |
@@ -134,11 +134,11 @@ discard block |
||
| 134 | 134 | /** |
| 135 | 135 | */ |
| 136 | 136 | private function compatibility() { |
| 137 | - if(!$this->testStatement('SELECT printf("%0.2f", 19.99999) AS res')) { |
|
| 137 | + if (!$this->testStatement('SELECT printf("%0.2f", 19.99999) AS res')) { |
|
| 138 | 138 | $this->registerUDFunction('printf', 'sprintf'); |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | - if(!$this->testStatement('SELECT md5("aaa") AS md5res')) { |
|
| 141 | + if (!$this->testStatement('SELECT md5("aaa") AS md5res')) { |
|
| 142 | 142 | $this->registerUDFunction('md5', 'md5'); |
| 143 | 143 | } |
| 144 | 144 | } |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | * @throws Exception |
| 162 | 162 | */ |
| 163 | 163 | private function registerUDFunction($name, $callback) { |
| 164 | - if(!method_exists($this->pdo, 'sqliteCreateFunction')) { |
|
| 164 | + if (!method_exists($this->pdo, 'sqliteCreateFunction')) { |
|
| 165 | 165 | throw new Exception('It is not possible to create user defined functions for rkr/data-diff\'s sqlite instance'); |
| 166 | 166 | } |
| 167 | 167 | call_user_func([$this->pdo, 'sqliteCreateFunction'], $name, $callback); |
@@ -170,7 +170,7 @@ discard block |
||
| 170 | 170 | /** |
| 171 | 171 | */ |
| 172 | 172 | private function initSqlite() { |
| 173 | - $tryThis = function ($query) { |
|
| 173 | + $tryThis = function($query) { |
|
| 174 | 174 | try { |
| 175 | 175 | $this->pdo->exec($query); |
| 176 | 176 | } catch (Exception $e) { |
@@ -195,11 +195,11 @@ discard block |
||
| 195 | 195 | * @return array |
| 196 | 196 | */ |
| 197 | 197 | private function defineOptionDefaults($options) { |
| 198 | - if(!array_key_exists('dsn', $options)) { |
|
| 198 | + if (!array_key_exists('dsn', $options)) { |
|
| 199 | 199 | $options['dsn'] = 'sqlite::memory:'; |
| 200 | 200 | } |
| 201 | - if(!array_key_exists('duplicate_key_handler', $options)) { |
|
| 202 | - $options['duplicate_key_handler'] = function (array $newData = null, array $oldData = null) { |
|
| 201 | + if (!array_key_exists('duplicate_key_handler', $options)) { |
|
| 202 | + $options['duplicate_key_handler'] = function(array $newData = null, array $oldData = null) { |
|
| 203 | 203 | return array_merge($oldData, $newData); |
| 204 | 204 | }; |
| 205 | 205 | } |
@@ -125,7 +125,7 @@ discard block |
||
| 125 | 125 | /** |
| 126 | 126 | * Get all rows, that are present in this store, but not in the other |
| 127 | 127 | * |
| 128 | - * @return Generator|DiffStorageStoreRow[] |
|
| 128 | + * @return Generator |
|
| 129 | 129 | */ |
| 130 | 130 | public function getNew() { |
| 131 | 131 | return $this->query(' |
@@ -149,7 +149,7 @@ discard block |
||
| 149 | 149 | /** |
| 150 | 150 | * Get all rows, that have a different value hash in the other store |
| 151 | 151 | * |
| 152 | - * @return Generator|DiffStorageStoreRow[] |
|
| 152 | + * @return Generator |
|
| 153 | 153 | */ |
| 154 | 154 | public function getChanged() { |
| 155 | 155 | return $this->query(' |
@@ -171,7 +171,7 @@ discard block |
||
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | /** |
| 174 | - * @return Generator|DiffStorageStoreRow[] |
|
| 174 | + * @return Generator |
|
| 175 | 175 | */ |
| 176 | 176 | public function getNewOrChanged() { |
| 177 | 177 | return $this->query(' |
@@ -195,7 +195,7 @@ discard block |
||
| 195 | 195 | /** |
| 196 | 196 | * Get all rows, that are present in the other store, but not in this |
| 197 | 197 | * |
| 198 | - * @return Generator|DiffStorageStoreRow[] |
|
| 198 | + * @return Generator |
|
| 199 | 199 | */ |
| 200 | 200 | public function getMissing() { |
| 201 | 201 | return $this->query(' |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | |
| 228 | 228 | /** |
| 229 | 229 | * @param string $query |
| 230 | - * @return Generator|DiffStorageStoreRow[] |
|
| 230 | + * @return Generator |
|
| 231 | 231 | */ |
| 232 | 232 | private function query($query) { |
| 233 | 233 | $stmt = $this->pdo->query($query); |
@@ -241,7 +241,7 @@ discard block |
||
| 241 | 241 | } |
| 242 | 242 | |
| 243 | 243 | /** |
| 244 | - * @return Traversable|array[] |
|
| 244 | + * @return Generator |
|
| 245 | 245 | */ |
| 246 | 246 | public function getIterator() { |
| 247 | 247 | $query = ' |
@@ -59,10 +59,10 @@ discard block |
||
| 59 | 59 | */ |
| 60 | 60 | public function addRow(array $data, array $translation = null, $duplicateKeyHandler = null) { |
| 61 | 61 | $data = $this->translate($data, $translation); |
| 62 | - if($duplicateKeyHandler === null) { |
|
| 62 | + if ($duplicateKeyHandler === null) { |
|
| 63 | 63 | $duplicateKeyHandler = $this->duplicateKeyHandler; |
| 64 | 64 | } |
| 65 | - $buildMetaData = function (array $data, array $keys) { |
|
| 65 | + $buildMetaData = function(array $data, array $keys) { |
|
| 66 | 66 | $metaData = $data; |
| 67 | 67 | $metaData = array_diff_key($metaData, array_diff_key($metaData, $keys)); |
| 68 | 68 | $metaData['___data'] = json_encode($data); |
@@ -73,13 +73,13 @@ discard block |
||
| 73 | 73 | $metaData = $buildMetaData($data, $this->converter); |
| 74 | 74 | $this->insertStmt->execute($metaData); |
| 75 | 75 | } catch (\PDOException $e) { |
| 76 | - if(strpos($e->getMessage(), 'UNIQUE constraint failed') !== false) { |
|
| 76 | + if (strpos($e->getMessage(), 'UNIQUE constraint failed') !== false) { |
|
| 77 | 77 | $metaData = $buildMetaData($data, $this->converter); |
| 78 | 78 | unset($metaData['___data']); |
| 79 | 79 | unset($metaData['___sort']); |
| 80 | 80 | $this->selectStmt->execute($metaData); |
| 81 | 81 | $oldData = $this->selectStmt->fetch(PDO::FETCH_COLUMN, 0); |
| 82 | - if($oldData === null) { |
|
| 82 | + if ($oldData === null) { |
|
| 83 | 83 | $oldData = []; |
| 84 | 84 | } else { |
| 85 | 85 | $oldData = json_decode($oldData, true); |
@@ -101,7 +101,7 @@ discard block |
||
| 101 | 101 | * @return $this |
| 102 | 102 | */ |
| 103 | 103 | public function addRows($rows, array $translation = null, $duplicateKeyHandler = null) { |
| 104 | - foreach($rows as $row) { |
|
| 104 | + foreach ($rows as $row) { |
|
| 105 | 105 | $this->addRow($row, $translation, $duplicateKeyHandler); |
| 106 | 106 | } |
| 107 | 107 | return $this; |
@@ -112,11 +112,11 @@ discard block |
||
| 112 | 112 | */ |
| 113 | 113 | public function hasAnyChanges() { |
| 114 | 114 | /** @noinspection PhpUnusedLocalVariableInspection */ |
| 115 | - foreach($this->getNewOrChanged() as $_) { |
|
| 115 | + foreach ($this->getNewOrChanged() as $_) { |
|
| 116 | 116 | return true; |
| 117 | 117 | } |
| 118 | 118 | /** @noinspection PhpUnusedLocalVariableInspection */ |
| 119 | - foreach($this->getMissing() as $_) { |
|
| 119 | + foreach ($this->getMissing() as $_) { |
|
| 120 | 120 | return true; |
| 121 | 121 | } |
| 122 | 122 | return false; |
@@ -232,7 +232,7 @@ discard block |
||
| 232 | 232 | private function query($query) { |
| 233 | 233 | $stmt = $this->pdo->query($query); |
| 234 | 234 | $stmt->execute(['sA' => $this->storeA, 'sB' => $this->storeB]); |
| 235 | - while($row = $stmt->fetch(PDO::FETCH_NUM)) { |
|
| 235 | + while ($row = $stmt->fetch(PDO::FETCH_NUM)) { |
|
| 236 | 236 | $d = json_decode($row[1], true); |
| 237 | 237 | $f = json_decode($row[2], true); |
| 238 | 238 | yield new DiffStorageStoreRow($d, $f, $this->converter); |
@@ -256,7 +256,7 @@ discard block |
||
| 256 | 256 | '; |
| 257 | 257 | $stmt = $this->pdo->query($query); |
| 258 | 258 | $stmt->execute(['s' => $this->storeA]); |
| 259 | - while($row = $stmt->fetch(PDO::FETCH_NUM)) { |
|
| 259 | + while ($row = $stmt->fetch(PDO::FETCH_NUM)) { |
|
| 260 | 260 | $row = json_decode($row[0], true); |
| 261 | 261 | $row = new DiffStorageStoreRow($row, [], $this->converter); |
| 262 | 262 | yield $row->getData(); |
@@ -270,10 +270,10 @@ discard block |
||
| 270 | 270 | * @return array |
| 271 | 271 | */ |
| 272 | 272 | private function translate(array $data, array $translation = null) { |
| 273 | - if($translation !== null) { |
|
| 273 | + if ($translation !== null) { |
|
| 274 | 274 | $result = []; |
| 275 | - foreach($data as $key => $value) { |
|
| 276 | - if(array_key_exists($key, $translation)) { |
|
| 275 | + foreach ($data as $key => $value) { |
|
| 276 | + if (array_key_exists($key, $translation)) { |
|
| 277 | 277 | $key = $translation[$key]; |
| 278 | 278 | } |
| 279 | 279 | $result[$key] = $value; |
@@ -8,7 +8,7 @@ |
||
| 8 | 8 | * @param array $options |
| 9 | 9 | */ |
| 10 | 10 | public function __construct(array $keySchema, array $valueSchema, array $options = []) { |
| 11 | - if(!array_key_exists('dsn', $options)) { |
|
| 11 | + if (!array_key_exists('dsn', $options)) { |
|
| 12 | 12 | $options['dsn'] = 'sqlite::memory:'; |
| 13 | 13 | } |
| 14 | 14 | parent::__construct($keySchema, $valueSchema, $options); |
@@ -9,7 +9,7 @@ discard block |
||
| 9 | 9 | * @param array $options |
| 10 | 10 | */ |
| 11 | 11 | public function __construct($filename = null, array $keySchema, array $valueSchema, array $options = []) { |
| 12 | - if($filename === null) { |
|
| 12 | + if ($filename === null) { |
|
| 13 | 13 | $filename = tempnam(sys_get_temp_dir(), 'data-diff-'); |
| 14 | 14 | } |
| 15 | 15 | $this->createFile($filename); |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | try { |
| 26 | 26 | $fp = fopen($filename, 'w+'); |
| 27 | 27 | } finally { |
| 28 | - if(is_resource($fp)) { |
|
| 28 | + if (is_resource($fp)) { |
|
| 29 | 29 | fclose($fp); |
| 30 | 30 | } |
| 31 | 31 | } |
@@ -22,9 +22,9 @@ discard block |
||
| 22 | 22 | $this->row = is_array($row) ? $row : []; |
| 23 | 23 | $this->converter = $converter; |
| 24 | 24 | $this->foreignRow = is_array($foreignRow) ? $foreignRow : []; |
| 25 | - if($row !== null) { |
|
| 25 | + if ($row !== null) { |
|
| 26 | 26 | $this->data = $row; |
| 27 | - } elseif($foreignRow !== null) { |
|
| 27 | + } elseif ($foreignRow !== null) { |
|
| 28 | 28 | $this->data = $foreignRow; |
| 29 | 29 | } |
| 30 | 30 | } |
@@ -59,23 +59,23 @@ discard block |
||
| 59 | 59 | */ |
| 60 | 60 | public function getDiff(array $fields = null) { |
| 61 | 61 | $diff = []; |
| 62 | - $diffFn = function ($keysA) use (&$diff, $fields) { |
|
| 63 | - foreach($keysA as $key) { |
|
| 64 | - if($fields !== null && !in_array($key, $fields)) { |
|
| 62 | + $diffFn = function($keysA) use (&$diff, $fields) { |
|
| 63 | + foreach ($keysA as $key) { |
|
| 64 | + if ($fields !== null && !in_array($key, $fields)) { |
|
| 65 | 65 | continue; |
| 66 | 66 | } |
| 67 | - if(!array_key_exists($key, $this->foreignRow)) { |
|
| 67 | + if (!array_key_exists($key, $this->foreignRow)) { |
|
| 68 | 68 | $diff[$key] = ['local' => $this->row[$key], 'foreign' => null]; |
| 69 | - } elseif(!array_key_exists($key, $this->row)) { |
|
| 69 | + } elseif (!array_key_exists($key, $this->row)) { |
|
| 70 | 70 | $diff[$key] = ['local' => null, 'foreign' => $this->foreignRow[$key]]; |
| 71 | 71 | } else { |
| 72 | 72 | $v1 = $this->row[$key]; |
| 73 | 73 | $v2 = $this->foreignRow[$key]; |
| 74 | - if(array_key_exists($key, $this->converter)) { |
|
| 74 | + if (array_key_exists($key, $this->converter)) { |
|
| 75 | 75 | $v1 = call_user_func($this->converter[$key], $v1); |
| 76 | 76 | $v2 = call_user_func($this->converter[$key], $v2); |
| 77 | 77 | } |
| 78 | - if(json_encode($v1) !== json_encode($v2)) { |
|
| 78 | + if (json_encode($v1) !== json_encode($v2)) { |
|
| 79 | 79 | $diff[$key] = ['local' => $this->row[$key], 'foreign' => $this->foreignRow[$key]]; |
| 80 | 80 | } |
| 81 | 81 | } |
@@ -96,9 +96,9 @@ discard block |
||
| 96 | 96 | */ |
| 97 | 97 | public function getDiffFormatted(array $fields = null, $format = null) { |
| 98 | 98 | $diff = $this->getDiff($fields); |
| 99 | - if($format === null) { |
|
| 99 | + if ($format === null) { |
|
| 100 | 100 | $result = []; |
| 101 | - foreach($diff as $fieldName => $values) { |
|
| 101 | + foreach ($diff as $fieldName => $values) { |
|
| 102 | 102 | $result[] = sprintf("%s: %s -> %s", $fieldName, $values['foreign'], $values['local']); |
| 103 | 103 | } |
| 104 | 104 | return join(', ', $result); |
@@ -126,7 +126,7 @@ discard block |
||
| 126 | 126 | * @return mixed |
| 127 | 127 | */ |
| 128 | 128 | public function offsetGet($offset) { |
| 129 | - if($this->offsetExists($offset)) { |
|
| 129 | + if ($this->offsetExists($offset)) { |
|
| 130 | 130 | return $this->data[$offset]; |
| 131 | 131 | } |
| 132 | 132 | return null; |
@@ -146,7 +146,7 @@ discard block |
||
| 146 | 146 | * @return void |
| 147 | 147 | */ |
| 148 | 148 | public function offsetUnset($offset) { |
| 149 | - if($this->offsetExists($offset)) { |
|
| 149 | + if ($this->offsetExists($offset)) { |
|
| 150 | 150 | unset($this->data[$offset]); |
| 151 | 151 | } |
| 152 | 152 | } |
@@ -157,7 +157,7 @@ discard block |
||
| 157 | 157 | */ |
| 158 | 158 | private function formatRow($row) { |
| 159 | 159 | $schema = $this->converter; |
| 160 | - $schema = array_map(function () { return null; }, $schema); |
|
| 160 | + $schema = array_map(function() { return null; }, $schema); |
|
| 161 | 161 | $row = array_merge($schema, $row); |
| 162 | 162 | return $row; |
| 163 | 163 | } |
@@ -168,13 +168,13 @@ discard block |
||
| 168 | 168 | * @return array |
| 169 | 169 | */ |
| 170 | 170 | private function applyOptions(array $row, array $options) { |
| 171 | - if(count($options) < 1) { |
|
| 171 | + if (count($options) < 1) { |
|
| 172 | 172 | return $row; |
| 173 | 173 | } |
| 174 | - if(array_key_exists('keys', $options)) { |
|
| 174 | + if (array_key_exists('keys', $options)) { |
|
| 175 | 175 | $row = array_intersect_key($row, array_combine($options['keys'], $options['keys'])); |
| 176 | 176 | } |
| 177 | - if(array_key_exists('ignore', $options)) { |
|
| 177 | + if (array_key_exists('ignore', $options)) { |
|
| 178 | 178 | $row = array_diff_key($row, array_combine($options['ignore'], $options['ignore'])); |
| 179 | 179 | } |
| 180 | 180 | return $row; |