Total Complexity | 65 |
Total Lines | 280 |
Duplicated Lines | 0 % |
Changes | 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 |
||
9 | abstract class QuickInsertFunctions |
||
10 | { |
||
11 | public static $entityManager; |
||
12 | public static $connection; |
||
13 | protected static $mock = []; |
||
14 | protected static $metadata = []; |
||
15 | protected static $tableName; |
||
16 | protected static $identifier; |
||
17 | |||
18 | protected static function buildExtra($extra) |
||
19 | { |
||
20 | $methods = [ |
||
21 | 'GROUP BY', |
||
22 | 'HAVING', |
||
23 | 'ORDER BY', |
||
24 | ]; |
||
25 | $sql = ''; |
||
26 | |||
27 | foreach ($methods as $method) { |
||
28 | if (isset($extra[$method])) { |
||
29 | $sql .= ' '.$method.' '; |
||
30 | if (\is_array($extra[$method])) { |
||
31 | $sql .= implode(' ', $extra[$method]).' '; |
||
32 | } else { |
||
33 | $sql .= $extra[$method].' '; |
||
34 | } |
||
35 | } |
||
36 | } |
||
37 | self::getLimit($extra, $sql); |
||
38 | |||
39 | return Helper::oneSpace($sql); |
||
40 | } |
||
41 | |||
42 | private static function getLimit($extra = [], &$sql) |
||
43 | { |
||
44 | if (isset($extra['LIMIT']) && \is_array($extra['LIMIT'])) { |
||
45 | if (isset($extra['LIMIT'][1])) { |
||
46 | list($offset, $limit) = $extra['LIMIT']; |
||
47 | } else { |
||
48 | $offset = 0; |
||
49 | $limit = $extra['LIMIT'][0]; |
||
50 | } |
||
51 | $sql = sprintf('%sLIMIT %s, %s', $sql, $offset, $limit); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | protected static function buildWhere($tableName, array $where) |
||
56 | { |
||
57 | $whereSql = ''; |
||
58 | if (!empty($where)) { |
||
59 | $path = ' WHERE '; |
||
60 | foreach ($where as $key => $value) { |
||
61 | if (!\is_array($value)) { |
||
62 | if (isset(self::$mock[$tableName][$key])) { |
||
63 | $whereSql .= $path.self::$mock[$tableName][$key].' = '.self::slashes($tableName, $key, $value); |
||
64 | } else { |
||
65 | $whereSql .= $path.$key.' = '.self::slashes($tableName, $key, $value); |
||
66 | } |
||
67 | } else { |
||
68 | $whereSql .= $path.$value[0]; |
||
69 | } |
||
70 | |||
71 | if ($value === reset($where)) { |
||
72 | $path = ' AND '; |
||
73 | } |
||
74 | } |
||
75 | } |
||
76 | |||
77 | return $whereSql; |
||
78 | } |
||
79 | |||
80 | protected static function slashes($tableName, $key, $value) |
||
81 | { |
||
82 | if ($value instanceof \DateTime) { |
||
83 | $result = "'".addslashes(trim($value->format('Y-m-d H:i:s')))."'"; |
||
84 | } else { |
||
85 | $result = self::numeric($tableName, $key, $value) ? (int)$value : "'".addslashes(trim($value))."'"; |
||
86 | } |
||
87 | |||
88 | $trim = trim($result); |
||
89 | if ($trim === '' || $trim === "''") { |
||
90 | $result = null; |
||
91 | } |
||
92 | |||
93 | return $result; |
||
94 | } |
||
95 | |||
96 | protected static function numeric($tableName, $key, $value) |
||
97 | { |
||
98 | $intTypes = [ |
||
99 | 'boolean', |
||
100 | 'integer', |
||
101 | 'longint', |
||
102 | ]; |
||
103 | $flip = []; |
||
104 | if (isset(self::$mock[$tableName])) { |
||
105 | $flip = array_flip(self::$mock[$tableName]); |
||
106 | } |
||
107 | |||
108 | if (isset(self::$metadata[$tableName], $flip[$key])) { |
||
109 | if (\in_array(self::$metadata[$tableName]->getFieldMapping($flip[$key])['type'], $intTypes, false)) { |
||
110 | return true; |
||
111 | } |
||
112 | |||
113 | return false; |
||
114 | } |
||
115 | |||
116 | return \is_numeric($value) || \is_bool($value); |
||
117 | } |
||
118 | |||
119 | protected static function getTable(&$object, &$tableName, &$columns, &$type, $manager = null, array $extraFields = []) |
||
120 | { |
||
121 | self::init($manager); |
||
122 | if (\is_object($object)) { |
||
123 | self::extract($object); |
||
124 | $tableName = self::$tableName; |
||
125 | $columns = self::$mock[$tableName] ?: []; |
||
126 | $type = 'object'; |
||
127 | } else { |
||
128 | $tableName = $object['table_name']; |
||
129 | unset($object['table_name']); |
||
130 | $type = 'table'; |
||
131 | $columns = array_keys($object) ?: []; |
||
132 | } |
||
133 | |||
134 | if (isset($extraFields[$tableName])) { |
||
135 | $columns = array_merge($columns, $extraFields[$tableName]); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | public static function init($manager = null) |
||
140 | { |
||
141 | if (self::$connection) { |
||
142 | return; |
||
143 | } |
||
144 | global $kernel; |
||
145 | |||
146 | if (AppCache::class === \get_class($kernel)) { |
||
147 | $kernel = $kernel->getKernel(); |
||
148 | } |
||
149 | $container = $kernel->getContainer(); |
||
150 | |||
151 | $manager = $manager ?: $container->getParameter('sludio_helper.entity.manager'); |
||
152 | if (\is_object($manager)) { |
||
153 | self::$entityManager = $manager; |
||
154 | } else { |
||
155 | self::$entityManager = $container->get('doctrine')->getManager($manager); |
||
156 | } |
||
157 | self::$connection = self::$entityManager->getConnection(); |
||
158 | } |
||
159 | |||
160 | protected static function extract($object) |
||
161 | { |
||
162 | self::init(false); |
||
163 | $data = self::extractExt(self::$entityManager->getMetadataFactory()->getMetadataFor(\get_class($object))); |
||
164 | |||
165 | self::$mock = $data['mock']; |
||
166 | self::$tableName = $data['table']; |
||
167 | self::$metadata[$data['table']] = $data['meta']; |
||
168 | self::$identifier = $data['identifier']; |
||
169 | } |
||
170 | |||
171 | public static function extractExt(ClassMetadata $metadata) |
||
172 | { |
||
173 | $fields = $metadata->getFieldNames(); |
||
174 | $columns = $metadata->getColumnNames(); |
||
175 | $table = $metadata->getTableName(); |
||
176 | $identifier = null; |
||
177 | |||
178 | $result = []; |
||
179 | foreach ($fields as $key => $field) { |
||
180 | /** @var $columns array */ |
||
181 | foreach ($columns as $key2 => $column) { |
||
182 | if ($key === $key2) { |
||
183 | $result[$table][$field] = $column; |
||
184 | if ($field === $metadata->getIdentifier()[0]) { |
||
185 | $identifier = $column; |
||
186 | } |
||
187 | } |
||
188 | } |
||
189 | } |
||
190 | |||
191 | $data = [ |
||
192 | 'mock' => $result, |
||
193 | 'table' => $table, |
||
194 | 'meta' => $metadata, |
||
195 | 'identifier' => $identifier, |
||
196 | ]; |
||
197 | |||
198 | return $data; |
||
199 | } |
||
200 | |||
201 | protected static function parseUpdateResult($object, $type, $id, $tableName, array $result = null) |
||
202 | { |
||
203 | $data = []; |
||
204 | if (!empty($result)) { |
||
205 | foreach ($result as $key => $value) { |
||
206 | $content = self::value($object, $key, $type, $tableName, false); |
||
207 | if ($id && !\in_array($content, [ |
||
208 | null, |
||
209 | $value, |
||
210 | ], true)) { |
||
211 | $data[$key] = $content; |
||
212 | } |
||
213 | } |
||
214 | } |
||
215 | |||
216 | return $data; |
||
217 | } |
||
218 | |||
219 | protected static function value($object, $variable, $type, $tableName, $check = true) |
||
220 | { |
||
221 | $value = null; |
||
222 | if ($type === 'object') { |
||
223 | $value = $object->{'get'.ucfirst(Helper::toCamelCase($variable))}(); |
||
224 | } else { |
||
225 | if (isset($object[$variable])) { |
||
226 | $value = $object[$variable]; |
||
227 | } |
||
228 | } |
||
229 | |||
230 | if ($check) { |
||
231 | self::slashes($tableName, $variable, $value); |
||
232 | } |
||
233 | |||
234 | return $value; |
||
235 | } |
||
236 | |||
237 | protected static function makeValues($tableName, array $data = []) |
||
238 | { |
||
239 | $values = ''; |
||
240 | if (!empty($data)) { |
||
241 | foreach ($data as $key => $value) { |
||
242 | $values .= self::slashes($tableName, $key, $value).','; |
||
243 | } |
||
244 | } |
||
245 | |||
246 | return \substr($values, 0, -1); |
||
247 | } |
||
248 | |||
249 | protected static function parsePersistColumns(array $columns = [], $object, $type, $tableName, &$idd) |
||
269 | } |
||
270 | |||
271 | protected static function filterGetResult(array $result = null, array $fields = [], $one = false) |
||
289 | } |
||
290 | } |
||
291 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths