| Total Complexity | 131 |
| Total Lines | 495 |
| Duplicated Lines | 12.53 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like QuickInsertRepository often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use QuickInsertRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class QuickInsertRepository |
||
| 6 | { |
||
| 7 | private static $mock = []; |
||
| 8 | private static $metadata = []; |
||
| 9 | private static $tableName; |
||
| 10 | |||
| 11 | public static $entityManager; |
||
| 12 | public static $connection; |
||
| 13 | public static $container; |
||
| 14 | |||
| 15 | public static function init($noFkCheck = false, $manager = null) |
||
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | public static function close($noFkCheck = false) |
||
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | public static function isEmpty($variable) |
||
| 55 | } |
||
| 56 | |||
| 57 | private static function extract($object) |
||
| 58 | { |
||
| 59 | self::init(false); |
||
| 60 | $data = self::extractExt($object, self::$entityManager); |
||
| 61 | |||
| 62 | self::$mock = $data['mock']; |
||
| 63 | self::$tableName = $data['table']; |
||
| 64 | self::$metadata[$data['table']] = $data['meta']; |
||
| 65 | } |
||
| 66 | |||
| 67 | public static function extractExt($object, $entityManager) |
||
| 68 | { |
||
| 69 | $metadata = $entityManager->getClassMetadata(get_class($object)); |
||
| 70 | |||
| 71 | $fields = $metadata->getFieldNames(); |
||
| 72 | $columns = $metadata->getColumnNames(); |
||
| 73 | $table = $metadata->getTableName(); |
||
| 74 | |||
| 75 | $result = []; |
||
| 76 | foreach ($fields as $key => $field) { |
||
| 77 | foreach ($columns as $key2 => $column) { |
||
| 78 | if ($key === $key2) { |
||
| 79 | $result[$table][$field] = $column; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | $data = [ |
||
| 85 | 'mock' => $result, |
||
| 86 | 'table' => $table, |
||
| 87 | 'meta' => $metadata, |
||
| 88 | ]; |
||
| 89 | |||
| 90 | return $data; |
||
| 91 | } |
||
| 92 | |||
| 93 | private static function buildExtra($extra) |
||
| 131 | } |
||
| 132 | |||
| 133 | private static function buildWhere($tableName, $where) |
||
| 134 | { |
||
| 135 | $whereSql = $fvalue = $fkey = ''; |
||
| 136 | if ($where && is_array($where)) { |
||
| 137 | $skip = false; |
||
| 138 | foreach ($where as $key => $value) { |
||
| 139 | $fkey = $key; |
||
| 140 | if (is_array($value)) { |
||
| 141 | $skip = true; |
||
| 142 | $fvalue = trim($value[0]); |
||
| 143 | } else { |
||
| 144 | $fvalue = trim($value); |
||
| 145 | } |
||
| 146 | break; |
||
| 147 | } |
||
| 148 | View Code Duplication | if (!$skip && isset(self::$mock[$tableName][$fkey])) { |
|
| 149 | if (is_numeric($fvalue)) { |
||
| 150 | $whereSql .= ' WHERE '.self::$mock[$tableName][$fkey]." = $fvalue"; |
||
| 151 | } else { |
||
| 152 | $whereSql .= ' WHERE '.self::$mock[$tableName][$fkey]." = '".addslashes(trim($fvalue))."'"; |
||
| 153 | } |
||
| 154 | } else { |
||
| 155 | if (!$skip && is_numeric($fvalue)) { |
||
| 156 | $whereSql .= ' WHERE '.$fkey." = $fvalue"; |
||
| 157 | } elseif (!$skip && !is_numeric($fvalue)) { |
||
| 158 | $whereSql .= ' WHERE '.$fkey." = '".addslashes(trim($fvalue))."'"; |
||
| 159 | } elseif ($skip && is_numeric($fkey)) { |
||
| 160 | $whereSql .= " WHERE $fvalue"; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | unset($where[$fkey]); |
||
| 164 | if ($where && is_array($where)) { |
||
| 165 | foreach ($where as $key => $value) { |
||
| 166 | $skip = is_array($value); |
||
| 167 | View Code Duplication | if (!$skip && isset(self::$mock[$tableName][$key])) { |
|
| 168 | if (is_numeric($value)) { |
||
| 169 | $whereSql .= ' AND '.self::$mock[$tableName][$key]." = $value"; |
||
| 170 | } else { |
||
| 171 | $whereSql .= ' AND '.self::$mock[$tableName][$key]." = '".addslashes(trim($value))."'"; |
||
| 172 | } |
||
| 173 | } else { |
||
| 174 | if (!$skip && is_numeric($value)) { |
||
| 175 | $whereSql .= ' AND '.$key." = $value"; |
||
| 176 | } elseif (!$skip && !is_numeric($value)) { |
||
| 177 | $whereSql .= ' AND '.$key." = '".addslashes(trim($value))."'"; |
||
| 178 | } elseif ($skip && is_numeric($key)) { |
||
| 179 | $whereSql .= " AND {$value[0]}"; |
||
| 180 | } |
||
| 181 | } |
||
| 182 | } |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | return $whereSql; |
||
| 187 | } |
||
| 188 | |||
| 189 | public static function findNextId($tableName, &$out = null) |
||
| 190 | { |
||
| 191 | $sql = " |
||
| 192 | SELECT |
||
| 193 | AUTO_INCREMENT |
||
| 194 | FROM |
||
| 195 | information_schema.tables |
||
| 196 | WHERE |
||
| 197 | table_name = '".$tableName."' |
||
| 198 | AND |
||
| 199 | table_schema = DATABASE() |
||
| 200 | "; |
||
| 201 | if ($out) { |
||
| 202 | $out = $sql; |
||
| 203 | } |
||
| 204 | $sth = self::$connection->prepare($sql); |
||
| 205 | $sth->execute(); |
||
| 206 | $result = $sth->fetch(); |
||
| 207 | |||
| 208 | if (isset($result['AUTO_INCREMENT'])) { |
||
| 209 | return (int)$result['AUTO_INCREMENT']; |
||
| 210 | } |
||
| 211 | |||
| 212 | return 1; |
||
| 213 | } |
||
| 214 | |||
| 215 | public static function findNextIdExt($object, $entityManager, &$out = null) |
||
| 221 | } |
||
| 222 | |||
| 223 | public static function get($object, $one = false, $where = [], $noFkCheck = true, $fields = [], $manager = null, $extra = [], &$out = null) |
||
| 224 | { |
||
| 225 | self::init($noFkCheck, $manager); |
||
| 226 | self::getTable($object, $tableName, $columns, $type); |
||
| 227 | |||
| 228 | $whereSql = self::buildWhere($tableName, $where); |
||
| 229 | $select = (isset($extra['MODE']) ? 'SELECT '.$extra['MODE'] : 'SELECT').' '; |
||
| 230 | if (!$fields) { |
||
| 231 | $sql = $select.'id FROM '.$tableName.' '.$whereSql; |
||
| 232 | } else { |
||
| 233 | $sql = $select.(implode(', ', $fields)).' FROM '.$tableName.' '.$whereSql; |
||
| 234 | } |
||
| 235 | if (!empty($extra)) { |
||
| 236 | $extraSql = self::buildExtra($extra); |
||
| 237 | $sql .= $extraSql; |
||
| 238 | } |
||
| 239 | if ($out) { |
||
| 240 | $out = $sql; |
||
| 241 | } |
||
| 242 | $sth = self::$connection->prepare($sql); |
||
| 243 | $sth->execute(); |
||
| 244 | $result = $sth->fetchAll(); |
||
| 245 | if ($one && $result) { |
||
| 246 | if (!$fields) { |
||
| 247 | return intval($result[0]['id']); |
||
| 248 | } else { |
||
| 249 | if (count($fields) === 1 && $fields[0] !== '*') { |
||
| 250 | return $result[0][$fields[0]]; |
||
| 251 | } else { |
||
| 252 | return $result[0]; |
||
| 253 | } |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | self::close($noFkCheck); |
||
| 258 | if ($one || !$result) { |
||
| 259 | return null; |
||
| 260 | } |
||
| 261 | |||
| 262 | $field = null; |
||
| 263 | if (!$fields) { |
||
| 264 | $field = 'id'; |
||
| 265 | } elseif (count($fields) === 1 && $fields[0] !== '*') { |
||
| 266 | $field = $fields[0]; |
||
| 267 | } |
||
| 268 | |||
| 269 | if ($field) { |
||
| 270 | foreach ($result as &$res) { |
||
| 271 | $res = $res[$field]; |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | return $result; |
||
| 276 | } |
||
| 277 | |||
| 278 | private static function getTable(&$object, &$tableName, &$columns, &$type) |
||
| 279 | { |
||
| 280 | if (is_object($object)) { |
||
| 281 | self::extract($object); |
||
| 282 | $tableName = self::$tableName; |
||
| 283 | $columns = self::$mock[$tableName] ?: []; |
||
| 284 | $type = 'object'; |
||
| 285 | } else { |
||
| 286 | $tableName = $object['table_name']; |
||
| 287 | unset($object['table_name']); |
||
| 288 | $type = 'table'; |
||
| 289 | $columns = array_keys($object) ?: []; |
||
| 290 | } |
||
| 291 | } |
||
| 292 | |||
| 293 | public static function persist($object, $full = false, $extraFields = [], $noFkCheck = false, $manager = null, &$out = null) |
||
| 294 | { |
||
| 295 | self::init($noFkCheck, $manager); |
||
| 296 | self::getTable($object, $tableName, $columns, $type); |
||
| 297 | |||
| 298 | $id = self::findNextId($tableName); |
||
| 299 | $keys = []; |
||
| 300 | $values = []; |
||
| 301 | |||
| 302 | View Code Duplication | if (!empty($extraFields) && isset($extraFields[$tableName])) { |
|
| 303 | $columns = array_merge($columns, $extraFields[$tableName]); |
||
| 304 | } |
||
| 305 | |||
| 306 | if ($type === 'object') { |
||
| 307 | foreach ($columns as $value => $key) { |
||
| 308 | $variable = null; |
||
| 309 | if (!is_array($key) && !is_array($value)) { |
||
| 310 | if ($object->{'get'.ucfirst($value)}() instanceof \DateTime) { |
||
| 311 | $variable = "'".addslashes(trim($object->{'get'.ucfirst($value)}()->format('Y-m-d H:i:s')))."'"; |
||
| 312 | } else { |
||
| 313 | $variable = "'".addslashes(trim($object->{'get'.ucfirst($value)}()))."'"; |
||
| 314 | } |
||
| 315 | View Code Duplication | if (trim($variable) === '' || trim($variable) === "''" || (is_numeric($variable) && $variable === 0)) { |
|
| 316 | $variable = null; |
||
| 317 | } |
||
| 318 | View Code Duplication | if ($variable !== null) { |
|
| 319 | $values[] = $variable; |
||
| 320 | $keys[] = $key; |
||
| 321 | if ($key === 'id') { |
||
| 322 | $idd = $object->{'get'.ucfirst($value)}(); |
||
| 323 | } |
||
| 324 | } |
||
| 325 | } |
||
| 326 | } |
||
| 327 | } else { |
||
| 328 | foreach ($columns as $value => $key) { |
||
| 329 | $variable = null; |
||
| 330 | if (!is_array($key) && !is_array($value) && isset($object[$value])) { |
||
| 331 | if ($object[$value] instanceof \DateTime) { |
||
| 332 | $variable = "'".addslashes(trim($object[$value]->format('Y-m-d H:i:s')))."'"; |
||
| 333 | } else { |
||
| 334 | $variable = "'".addslashes(trim($object[$value]))."'"; |
||
| 335 | } |
||
| 336 | View Code Duplication | if (trim($variable) === '' || trim($variable) === "''" || (is_numeric($variable) && $variable === 0)) { |
|
| 337 | $variable = null; |
||
| 338 | } |
||
| 339 | View Code Duplication | if ($variable !== null) { |
|
| 340 | $values[] = $variable; |
||
| 341 | $keys[] = $key; |
||
| 342 | if ($key === 'id') { |
||
| 343 | $idd = $object[$value]; |
||
| 344 | } |
||
| 345 | } |
||
| 346 | } |
||
| 347 | } |
||
| 348 | } |
||
| 349 | |||
| 350 | $sql = null; |
||
| 351 | if (!$full && !self::isEmpty($values)) { |
||
| 352 | $sql = ' |
||
| 353 | INSERT INTO |
||
| 354 | '.$tableName.' |
||
| 355 | (id, '.implode(',', $keys).") |
||
| 356 | VALUES |
||
| 357 | ({$id},".implode(',', $values).') |
||
| 358 | '; |
||
| 359 | } elseif ($full && !self::isEmpty($values)) { |
||
| 360 | $id = $idd; |
||
| 361 | $sql = ' |
||
| 362 | INSERT INTO |
||
| 363 | '.$tableName.' |
||
| 364 | ('.implode(',', $keys).") |
||
| 365 | VALUES |
||
| 366 | (".implode(',', $values).') |
||
| 367 | '; |
||
| 368 | } else { |
||
| 369 | $id = null; |
||
| 370 | } |
||
| 371 | if ($sql !== null && $id) { |
||
| 372 | if ($out) { |
||
| 373 | $out = $sql; |
||
| 374 | } |
||
| 375 | $sth = self::$connection->prepare($sql); |
||
| 376 | $sth->execute(); |
||
| 377 | } |
||
| 378 | |||
| 379 | self::close($noFkCheck); |
||
| 380 | |||
| 381 | return $id; |
||
| 382 | } |
||
| 383 | |||
| 384 | public static function update($id, $object, $extraFields = [], $noFkCheck = false, $manager = null, &$out = null) |
||
| 456 | } |
||
| 457 | |||
| 458 | public static function delete($object, $where = [], $noFkCheck = false, $manager = null, &$out = null) |
||
| 459 | { |
||
| 460 | self::init($noFkCheck, $manager); |
||
| 461 | self::getTable($object, $tableName, $columns, $type); |
||
| 462 | |||
| 463 | $whereSql = self::buildWhere($tableName, $where); |
||
| 464 | $sql = 'DELETE FROM '.$tableName.' '.$whereSql; |
||
| 465 | if ($out) { |
||
| 466 | $out = $sql; |
||
| 467 | } |
||
| 468 | $sth = self::$connection->prepare($sql); |
||
| 469 | $sth->execute(); |
||
| 470 | |||
| 471 | self::close($noFkCheck); |
||
| 472 | } |
||
| 473 | |||
| 474 | public static function link($object, $data, $noFkCheck = false, $manager = null, &$out = null) |
||
| 500 | } |
||
| 501 | } |
||
| 502 |
Even though PHP does not care about the name of your methods, it is generally a good practice to choose method names which can be easily understood by other human readers.