| Total Complexity | 40 |
| Total Lines | 178 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
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 |
||
| 8 | class QuickInsertRepository extends QuickInsertFunctions |
||
| 9 | { |
||
| 10 | public static function findNextIdExt(ClassMetadata $metadata, $manager = null) |
||
| 11 | { |
||
| 12 | self::init($manager); |
||
| 13 | $data = self::extractExt($metadata); |
||
| 14 | |||
| 15 | return self::findNextId($data['table']); |
||
| 16 | } |
||
| 17 | |||
| 18 | public static function findNextId($tableName) |
||
| 24 | } |
||
| 25 | |||
| 26 | public static function get($object, $one = false, array $where = [], array $fields = [], $manager = null, array $extra = []) |
||
| 27 | { |
||
| 28 | self::getTable($object, $tableName, $columns, $type, $manager); |
||
| 29 | |||
| 30 | $select = sprintf('SELECT %s ', isset($extra['MODE']) ? $extra['MODE'] : ''); |
||
| 31 | $fields = $fields ?: ['id']; |
||
|
|
|||
| 32 | $sql = $select.implode(', ', $fields).' FROM '.$tableName.self::buildWhere($tableName, $where).self::buildExtra($extra); |
||
| 33 | |||
| 34 | $result = self::runSQL($sql) ?: null; |
||
| 35 | |||
| 36 | if ($result) { |
||
| 37 | $field = null; |
||
| 38 | if (\count($fields) === 1 && $fields[0] !== '*') { |
||
| 39 | $field = $fields[0]; |
||
| 40 | } |
||
| 41 | if ($field !== null) { |
||
| 42 | if (!$one) { |
||
| 43 | /** @var $result array */ |
||
| 44 | foreach ($result as &$res) { |
||
| 45 | $res = $res[$field]; |
||
| 46 | } |
||
| 47 | } else { |
||
| 48 | $result = $result[0][$field]; |
||
| 49 | } |
||
| 50 | } elseif ($one) { |
||
| 51 | $result = $result[0]; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | return $result; |
||
| 56 | } |
||
| 57 | |||
| 58 | public static function runSQL($sql, $noFkCheck = true, $manager = null, $skip = false) |
||
| 59 | { |
||
| 60 | $sql = trim(preg_replace('/\s+/', ' ', $sql)); |
||
| 61 | self::init($manager); |
||
| 62 | if (!$skip) { |
||
| 63 | self::setFK(0, $noFkCheck); |
||
| 64 | } |
||
| 65 | |||
| 66 | $sth = self::$connection->prepare($sql); |
||
| 67 | $sth->execute(); |
||
| 68 | |||
| 69 | if (!$skip) { |
||
| 70 | self::setFK(1, $noFkCheck); |
||
| 71 | } |
||
| 72 | if (0 === strpos($sql, 'SELECT')) { |
||
| 73 | return $sth->fetchAll(); |
||
| 74 | } |
||
| 75 | |||
| 76 | return true; |
||
| 77 | } |
||
| 78 | |||
| 79 | public static function setFK($fkCheck = 0, $noFkCheck = false) |
||
| 80 | { |
||
| 81 | if (!$noFkCheck) { |
||
| 82 | self::runSQL("SET FOREIGN_KEY_CHECKS = $fkCheck", false, null, true); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | public static function update($id, $object, array $extraFields = [], $noFkCheck = false, $manager = null) |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | public static function delete($object, array $where = [], $noFkCheck = false, $manager = null) |
||
| 125 | { |
||
| 126 | self::getTable($object, $tableName, $columns, $type, $manager); |
||
| 127 | |||
| 128 | $sql = sprintf('DELETE FROM %s%s', $tableName, self::buildWhere($tableName, $where)); |
||
| 129 | self::runSQL($sql, $noFkCheck); |
||
| 130 | } |
||
| 131 | |||
| 132 | public static function link($object, $data, $noFkCheck = false, $manager = null) |
||
| 138 | } |
||
| 139 | |||
| 140 | public static function persist($object, $full = false, array $extraFields = [], $noFkCheck = false, $manager = null) |
||
| 186 | } |
||
| 187 | } |
||
| 188 |