Total Complexity | 48 |
Total Lines | 221 |
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 |
||
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) |
||
49 | } |
||
50 | |||
51 | protected static function buildWhere($tableName, array $where) |
||
52 | { |
||
53 | $whereSql = ''; |
||
54 | if (!empty($where)) { |
||
55 | reset($where); |
||
56 | $first = key($where); |
||
57 | $path = ' WHERE '; |
||
58 | foreach ($where as $key => $value) { |
||
59 | if (!\is_array($value) && isset(self::$mock[$tableName][$key])) { |
||
60 | $whereSql .= $path.self::$mock[$tableName][$key].' = '.self::slashes($tableName, $key, $value); |
||
61 | } elseif (\is_array($value)) { |
||
62 | $whereSql .= $path.$value[0]; |
||
63 | } else { |
||
64 | $whereSql .= $path.$key.' = '.self::slashes($tableName, $key, $value); |
||
65 | } |
||
66 | if ($key === $first) { |
||
67 | $path = ' AND '; |
||
68 | } |
||
69 | } |
||
70 | } |
||
71 | |||
72 | return $whereSql; |
||
73 | } |
||
74 | |||
75 | protected static function slashes($tableName, $key, $value) |
||
76 | { |
||
77 | if ($value instanceof \DateTime) { |
||
78 | $result = "'".addslashes(trim($value->format('Y-m-d H:i:s')))."'"; |
||
79 | } else { |
||
80 | $result = self::numeric($tableName, $key, $value) ? $value : "'".addslashes(trim($value))."'"; |
||
81 | } |
||
82 | |||
83 | $trim = trim($result); |
||
84 | if ($trim === '' || $trim === "''") { |
||
85 | $result = null; |
||
86 | } |
||
87 | |||
88 | return $result; |
||
89 | } |
||
90 | |||
91 | protected static function numeric($tableName, $key, $value) |
||
92 | { |
||
93 | $intTypes = [ |
||
94 | 'boolean', |
||
95 | 'integer', |
||
96 | 'longint', |
||
97 | ]; |
||
98 | $flip = []; |
||
99 | if (isset(self::$mock[$tableName])) { |
||
100 | $flip = array_flip(self::$mock[$tableName]); |
||
101 | } |
||
102 | |||
103 | if (isset(self::$metadata[$tableName], $flip[$key])) { |
||
104 | if (\in_array(self::$metadata[$tableName]->getFieldMapping($flip[$key])['type'], $intTypes, false)) { |
||
105 | return true; |
||
106 | } |
||
107 | |||
108 | return false; |
||
109 | } |
||
110 | |||
111 | return is_numeric($value); |
||
112 | } |
||
113 | |||
114 | protected static function getTable(&$object, &$tableName, &$columns, &$type, $manager = null, array $extraFields = []) |
||
115 | { |
||
116 | self::init($manager); |
||
117 | if (\is_object($object)) { |
||
118 | self::extract($object); |
||
119 | $tableName = self::$tableName; |
||
120 | $columns = self::$mock[$tableName] ?: []; |
||
121 | $type = 'object'; |
||
122 | } else { |
||
123 | $tableName = $object['table_name']; |
||
124 | unset($object['table_name']); |
||
125 | $type = 'table'; |
||
126 | $columns = array_keys($object) ?: []; |
||
127 | } |
||
128 | |||
129 | if (isset($extraFields[$tableName])) { |
||
130 | $columns = array_merge($columns, $extraFields[$tableName]); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | public static function init($manager = null) |
||
135 | { |
||
136 | if (self::$connection) { |
||
137 | return; |
||
138 | } |
||
139 | global $kernel; |
||
140 | |||
141 | if (AppCache::class === \get_class($kernel)) { |
||
142 | $kernel = $kernel->getKernel(); |
||
143 | } |
||
144 | $container = $kernel->getContainer(); |
||
145 | |||
146 | $manager = $manager ?: $container->getParameter('sludio_helper.entity.manager'); |
||
147 | if (\is_object($manager)) { |
||
148 | self::$entityManager = $manager; |
||
149 | } else { |
||
150 | self::$entityManager = $container->get('doctrine')->getManager($manager); |
||
151 | } |
||
152 | self::$connection = self::$entityManager->getConnection(); |
||
153 | } |
||
154 | |||
155 | protected static function extract($object) |
||
164 | } |
||
165 | |||
166 | public static function extractExt(ClassMetadata $metadata) |
||
167 | { |
||
168 | $fields = $metadata->getFieldNames(); |
||
169 | $columns = $metadata->getColumnNames(); |
||
170 | $table = $metadata->getTableName(); |
||
171 | $identifier = null; |
||
172 | |||
173 | $result = []; |
||
174 | foreach ($fields as $key => $field) { |
||
175 | /** @var $columns array */ |
||
176 | foreach ($columns as $key2 => $column) { |
||
177 | if ($key === $key2) { |
||
178 | $result[$table][$field] = $column; |
||
179 | if ($field === $metadata->getIdentifier()[0]) { |
||
180 | $identifier = $column; |
||
181 | } |
||
182 | } |
||
183 | } |
||
184 | } |
||
185 | |||
186 | $data = [ |
||
187 | 'mock' => $result, |
||
188 | 'table' => $table, |
||
189 | 'meta' => $metadata, |
||
190 | 'identifier' => $identifier, |
||
191 | ]; |
||
192 | |||
193 | return $data; |
||
194 | } |
||
195 | |||
196 | protected static function parseUpdateResult($object, $type, $id, $tableName, array $result = null) |
||
212 | } |
||
213 | |||
214 | protected static function value($object, $variable, $type, $tableName, $check = true) |
||
230 | } |
||
231 | } |
||
232 |
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