| Total Complexity | 40 |
| Total Lines | 188 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like QuickInsertFunctions 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 QuickInsertFunctions, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | abstract class QuickInsertFunctions |
||
| 9 | { |
||
| 10 | protected static $mock = []; |
||
| 11 | protected static $metadata = []; |
||
| 12 | protected static $tableName; |
||
| 13 | protected static $identifier; |
||
| 14 | |||
| 15 | public static $entityManager; |
||
| 16 | public static $connection; |
||
| 17 | |||
| 18 | public static function init($manager = null) |
||
| 19 | { |
||
| 20 | if (self::$connection) { |
||
| 21 | return; |
||
| 22 | } |
||
| 23 | global $kernel; |
||
| 24 | |||
| 25 | if ('AppCache' === \get_class($kernel)) { |
||
| 26 | $kernel = $kernel->getKernel(); |
||
| 27 | } |
||
| 28 | $container = $kernel->getContainer(); |
||
| 29 | |||
| 30 | $manager = $manager ?: $container->getParameter('sludio_helper.entity.manager'); |
||
| 31 | if(\is_object($manager)){ |
||
| 32 | self::$entityManager = $manager; |
||
| 33 | } else { |
||
| 34 | self::$entityManager = $container->get('doctrine')->getManager($manager); |
||
| 35 | } |
||
| 36 | self::$connection = self::$entityManager->getConnection(); |
||
| 37 | } |
||
| 38 | |||
| 39 | protected static function extract($object) |
||
| 48 | } |
||
| 49 | |||
| 50 | public static function extractExt(ClassMetadata $metadata) |
||
| 51 | { |
||
| 52 | $fields = $metadata->getFieldNames(); |
||
| 53 | $columns = $metadata->getColumnNames(); |
||
| 54 | $table = $metadata->getTableName(); |
||
| 55 | $identifier = null; |
||
| 56 | |||
| 57 | $result = []; |
||
| 58 | foreach ($fields as $key => $field) { |
||
| 59 | /** @var $columns array */ |
||
| 60 | foreach ($columns as $key2 => $column) { |
||
| 61 | if ($key === $key2) { |
||
| 62 | $result[$table][$field] = $column; |
||
| 63 | if ($field === $metadata->getIdentifier()[0]) { |
||
| 64 | $identifier = $column; |
||
| 65 | } |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | $data = [ |
||
| 71 | 'mock' => $result, |
||
| 72 | 'table' => $table, |
||
| 73 | 'meta' => $metadata, |
||
| 74 | 'identifier' => $identifier, |
||
| 75 | ]; |
||
| 76 | |||
| 77 | return $data; |
||
| 78 | } |
||
| 79 | |||
| 80 | protected static function buildExtra($extra) |
||
| 81 | { |
||
| 82 | $methods = [ |
||
| 83 | 'GROUP BY', |
||
| 84 | 'HAVING', |
||
| 85 | 'ORDER BY', |
||
| 86 | ]; |
||
| 87 | $sql = ''; |
||
| 88 | |||
| 89 | foreach ($methods as $method) { |
||
| 90 | if (isset($extra[$method])) { |
||
| 91 | $sql .= ' '.$method.' '; |
||
| 92 | if (\is_array($extra[$method])) { |
||
| 93 | $sql .= implode(' ', $extra[$method]).' '; |
||
| 94 | } else { |
||
| 95 | $sql .= $extra[$method].' '; |
||
| 96 | } |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | if (isset($extra['LIMIT']) && \is_array($extra['LIMIT'])) { |
||
| 101 | if (isset($extra['LIMIT'][1])) { |
||
| 102 | list($offset, $limit) = $extra['LIMIT']; |
||
| 103 | } else { |
||
| 104 | $offset = 0; |
||
| 105 | $limit = $extra['LIMIT'][0]; |
||
| 106 | } |
||
| 107 | $sql = sprintf('%sLIMIT %s, %s', $sql, $offset, $limit); |
||
| 108 | } |
||
| 109 | |||
| 110 | return Helper::oneSpace($sql); |
||
| 111 | } |
||
| 112 | |||
| 113 | protected static function numeric($tableName, $key, $value) |
||
| 114 | { |
||
| 115 | $intTypes = [ |
||
| 116 | 'boolean', |
||
| 117 | 'integer', |
||
| 118 | 'longint', |
||
| 119 | ]; |
||
| 120 | $flip = []; |
||
| 121 | if(isset(self::$mock[$tableName])) { |
||
| 122 | $flip = array_flip(self::$mock[$tableName]); |
||
| 123 | } |
||
| 124 | |||
| 125 | if (isset(self::$metadata[$tableName], $flip[$key])) { |
||
| 126 | if (\in_array(self::$metadata[$tableName]->getFieldMapping($flip[$key])['type'], $intTypes, false)) { |
||
| 127 | return true; |
||
| 128 | } |
||
| 129 | |||
| 130 | return false; |
||
| 131 | } |
||
| 132 | |||
| 133 | return is_numeric($value); |
||
| 134 | } |
||
| 135 | |||
| 136 | protected static function buildWhere($tableName, array $where) |
||
| 158 | } |
||
| 159 | |||
| 160 | protected static function getTable(&$object, &$tableName, &$columns, &$type, $manager = null, array $extraFields = []) |
||
| 161 | { |
||
| 162 | self::init($manager); |
||
| 163 | if (\is_object($object)) { |
||
| 164 | self::extract($object); |
||
| 165 | $tableName = self::$tableName; |
||
| 166 | $columns = self::$mock[$tableName] ?: []; |
||
| 167 | $type = 'object'; |
||
| 168 | } else { |
||
| 169 | $tableName = $object['table_name']; |
||
| 170 | unset($object['table_name']); |
||
| 171 | $type = 'table'; |
||
| 172 | $columns = array_keys($object) ?: []; |
||
| 173 | } |
||
| 174 | |||
| 175 | if (isset($extraFields[$tableName])) { |
||
| 176 | $columns = array_merge($columns, $extraFields[$tableName]); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | protected static function value($object, $variable, $type, $check = true) |
||
| 196 | } |
||
| 197 | } |
||
| 198 |