Total Complexity | 86 |
Total Lines | 424 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Complex classes like CheckData 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 CheckData, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class CheckData |
||
36 | { |
||
37 | /** |
||
38 | * @var mixed |
||
39 | */ |
||
40 | private $cf = null; |
||
41 | |||
42 | /** |
||
43 | * @var mixed |
||
44 | */ |
||
45 | private $modId = null; |
||
46 | |||
47 | /** |
||
48 | * @var mixed |
||
49 | */ |
||
50 | private $tables = null; |
||
51 | |||
52 | /** |
||
53 | * @var mixed |
||
54 | */ |
||
55 | private $infos = []; |
||
56 | |||
57 | |||
58 | |||
59 | /** |
||
60 | * @public function constructor |
||
61 | * @param null |
||
62 | */ |
||
63 | public function __construct() |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @static function getInstance |
||
70 | * |
||
71 | * @param null |
||
72 | * |
||
73 | * @return Modulebuilder\Files\CheckData |
||
74 | */ |
||
75 | public static function getInstance() |
||
76 | { |
||
77 | static $instance = false; |
||
78 | if (!$instance) { |
||
79 | $instance = new self(); |
||
80 | } |
||
81 | |||
82 | return $instance; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @public function getCheckResult |
||
87 | * |
||
88 | * @param $module |
||
89 | * @return array |
||
90 | */ |
||
91 | public function getCheckPreBuilding($module) |
||
92 | { |
||
93 | $this->modId = $module->getVar('mod_id'); |
||
94 | $this->tables = $this->cf->getTableTables($this->modId); |
||
95 | $this->infos = []; |
||
96 | |||
97 | $this->getCheckBlock(); |
||
98 | $this->getCheckBroken(); |
||
99 | $this->getCheckComments(); |
||
100 | $this->getCheckUserpage(); |
||
101 | $this->getCheckRating(); |
||
102 | $this->getCheckReads(); |
||
103 | $this->getCheckSQL(); |
||
104 | |||
105 | return $this->infos; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @public function getCheckBroken |
||
110 | * |
||
111 | * @return array|bool |
||
112 | */ |
||
113 | private function getCheckBroken() |
||
114 | { |
||
115 | foreach (\array_keys($this->tables) as $t) { |
||
116 | if (1 == $this->tables[$t]->getVar('table_broken')) { |
||
117 | $tableId = $this->tables[$t]->getVar('table_id'); |
||
118 | $tableName = $this->tables[$t]->getVar('table_name'); |
||
119 | $fields = $this->cf->getTableFields($this->modId, $tableId); |
||
120 | $fieldSatus = ''; |
||
121 | |||
122 | foreach (\array_keys($fields) as $f) { |
||
123 | $fieldName = $fields[$f]->getVar('field_name'); |
||
124 | if (Constants::FIELD_ELE_SELECTSTATUS == $fields[$f]->getVar('field_element')) { |
||
125 | $fieldSatus = $fieldName; |
||
126 | } |
||
127 | } |
||
128 | // check whether each table with handling "broken" has also a field "status" |
||
129 | if ('' == $fieldSatus) { |
||
130 | $info = \str_replace('%t', $tableName, \_AM_MODULEBUILDER_BUILDING_CHECK_BROKEN1); |
||
131 | $this->infos[] = ['icon' => 'error', 'info' => $info]; |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | |||
136 | return true; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @private function getCheckUserpage |
||
141 | * |
||
142 | * @return array|bool |
||
143 | */ |
||
144 | private function getCheckUserpage() |
||
145 | { |
||
146 | //check field params: minimum one param is selected |
||
147 | foreach (\array_keys($this->tables) as $t) { |
||
148 | $tableId = $this->tables[$t]->getVar('table_id'); |
||
149 | $tableName = $this->tables[$t]->getVar('table_name'); |
||
150 | $fields = $this->cf->getTableFields($this->modId, $tableId); |
||
151 | |||
152 | foreach (\array_keys($fields) as $f) { |
||
153 | $fieldName = $fields[$f]->getVar('field_name'); |
||
154 | // check fields for parameters |
||
155 | if ($f > 0) { |
||
156 | $fieldParams = (int)$fields[$f]->getVar('field_parent') + (int)$fields[$f]->getVar('field_admin') + (int)$fields[$f]->getVar('field_inlist') + (int)$fields[$f]->getVar('field_inform') |
||
157 | + (int)$fields[$f]->getVar('field_user') + (int)$fields[$f]->getVar('field_ihead') + (int)$fields[$f]->getVar('field_ibody') + (int)$fields[$f]->getVar('field_ifoot') |
||
158 | + (int)$fields[$f]->getVar('field_thead') + (int)$fields[$f]->getVar('field_tbody') + (int)$fields[$f]->getVar('field_tfoot') + (int)$fields[$f]->getVar('field_block') |
||
159 | + (int)$fields[$f]->getVar('field_main') + (int)$fields[$f]->getVar('field_search') + (int)$fields[$f]->getVar('field_required'); |
||
160 | if (0 == $fieldParams) { |
||
161 | $info = \str_replace(['%f', '%t'], [$fieldName, $tableName], \_AM_MODULEBUILDER_BUILDING_CHECK_FIELDS1); |
||
162 | $this->infos[] = ['icon' => 'error', 'info' => $info]; |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | } |
||
167 | |||
168 | //check field params: user file no usage in index or item |
||
169 | foreach (\array_keys($this->tables) as $t) { |
||
170 | $tableId = $this->tables[$t]->getVar('table_id'); |
||
171 | $tableName = $this->tables[$t]->getVar('table_name'); |
||
172 | $fields = $this->cf->getTableFields($this->modId, $tableId); |
||
173 | |||
174 | foreach (\array_keys($fields) as $f) { |
||
175 | $fieldName = $fields[$f]->getVar('field_name'); |
||
176 | if (1 == $fields[$f]->getVar('field_user')) { |
||
177 | // check fields for parameters |
||
178 | if ($f > 0) { |
||
179 | $fieldParams = (int)$fields[$f]->getVar('field_ihead') + (int)$fields[$f]->getVar('field_ibody') + (int)$fields[$f]->getVar('field_ifoot') |
||
180 | + (int)$fields[$f]->getVar('field_thead') + (int)$fields[$f]->getVar('field_tbody') + (int)$fields[$f]->getVar('field_tfoot'); |
||
181 | if (0 == $fieldParams) { |
||
182 | $info = \str_replace(['%f', '%t'], [$fieldName, $tableName], \_AM_MODULEBUILDER_BUILDING_CHECK_FIELDS2); |
||
183 | $this->infos[] = ['icon' => 'warning', 'info' => $info]; |
||
184 | } |
||
185 | } |
||
186 | } |
||
187 | } |
||
188 | } |
||
189 | //check field params: user file index multiple usage |
||
190 | //check field params: user file item multiple usage |
||
191 | foreach (\array_keys($this->tables) as $t) { |
||
192 | $tableId = $this->tables[$t]->getVar('table_id'); |
||
193 | $tableName = $this->tables[$t]->getVar('table_name'); |
||
194 | $fields = $this->cf->getTableFields($this->modId, $tableId); |
||
195 | |||
196 | foreach (\array_keys($fields) as $f) { |
||
197 | $fieldName = $fields[$f]->getVar('field_name'); |
||
198 | if (1 == $fields[$f]->getVar('field_user')) { |
||
199 | // check fields for parameters |
||
200 | if ($f > 0) { |
||
201 | $fieldParams = (int)$fields[$f]->getVar('field_ihead') + (int)$fields[$f]->getVar('field_ibody') + (int)$fields[$f]->getVar('field_ifoot'); |
||
202 | if ($fieldParams > 1) { |
||
203 | $info = \str_replace(['%f', '%t'], [$fieldName, $tableName], \_AM_MODULEBUILDER_BUILDING_CHECK_FIELDS3); |
||
204 | $this->infos[] = ['icon' => 'warning', 'info' => $info]; |
||
205 | } |
||
206 | $fieldParams = (int)$fields[$f]->getVar('field_thead') + (int)$fields[$f]->getVar('field_tbody') + (int)$fields[$f]->getVar('field_tfoot'); |
||
207 | if ($fieldParams > 1) { |
||
208 | $info = \str_replace(['%f', '%t'], [$fieldName, $tableName], \_AM_MODULEBUILDER_BUILDING_CHECK_FIELDS3); |
||
209 | $this->infos[] = ['icon' => 'warning', 'info' => $info]; |
||
210 | } |
||
211 | } |
||
212 | } |
||
213 | } |
||
214 | } |
||
215 | |||
216 | //check table params: user submit or rate or broken, but table not for user side |
||
217 | foreach (\array_keys($this->tables) as $t) { |
||
218 | $tableName = $this->tables[$t]->getVar('table_name'); |
||
219 | if ((0 == $this->tables[$t]->getVar('table_user')) && (1 == $this->tables[$t]->getVar('table_submit') || 1 == $this->tables[$t]->getVar('table_broken') || 1 == $this->tables[$t]->getVar('table_rate'))) { |
||
220 | $info = \str_replace(['%t'], [$tableName], \_AM_MODULEBUILDER_BUILDING_CHECK_USERPAGE1); |
||
221 | $this->infos[] = ['icon' => 'error', 'info' => $info]; |
||
222 | } |
||
223 | } |
||
224 | |||
225 | //check field/table params: params for index file, but table param table_index = 0 |
||
226 | //check field/table params: params for user file, but table param table_user = 0 |
||
227 | foreach (\array_keys($this->tables) as $t) { |
||
228 | $tableId = $this->tables[$t]->getVar('table_id'); |
||
229 | $tableName = $this->tables[$t]->getVar('table_name'); |
||
230 | $tableIndex = (int)$this->tables[$t]->getVar('table_index'); |
||
231 | $tableUser = (int)$this->tables[$t]->getVar('table_user'); |
||
232 | $fields = $this->cf->getTableFields($this->modId, $tableId); |
||
233 | |||
234 | $fieldParamsIndex = 0; |
||
235 | $fieldParamsUser = 0; |
||
236 | $fieldName = ''; |
||
237 | foreach (\array_keys($fields) as $f) { |
||
238 | $fieldName = $fields[$f]->getVar('field_name'); |
||
239 | if (1 == $fields[$f]->getVar('field_user')) { |
||
240 | // check fields for parameters |
||
241 | if ($f > 0) { |
||
242 | $fieldParams = (int)$fields[$f]->getVar('field_ihead') + (int)$fields[$f]->getVar('field_ibody') + (int)$fields[$f]->getVar('field_ifoot'); |
||
243 | $fieldParamsIndex += $fieldParams; |
||
244 | if ($fieldParams >= 1 && 0 == $tableIndex) { |
||
245 | $info = \str_replace(['%f', '%t'], [$fieldName, $tableName], \_AM_MODULEBUILDER_BUILDING_CHECK_FIELDS5); |
||
246 | $this->infos[] = ['icon' => 'warning', 'info' => $info]; |
||
247 | } |
||
248 | $fieldParams = (int)$fields[$f]->getVar('field_thead') + (int)$fields[$f]->getVar('field_tbody') + (int)$fields[$f]->getVar('field_tfoot'); |
||
249 | $fieldParamsUser += $fieldParams; |
||
250 | if ($fieldParams >= 1 && 0 == $tableUser) { |
||
251 | $info = \str_replace(['%f', '%t'], [$fieldName, $tableName], \_AM_MODULEBUILDER_BUILDING_CHECK_FIELDS7); |
||
252 | $this->infos[] = ['icon' => 'warning', 'info' => $info]; |
||
253 | } |
||
254 | } |
||
255 | } |
||
256 | } |
||
257 | if (0 == $fieldParamsIndex && 1 == $tableIndex) { |
||
258 | $info = \str_replace(['%f', '%t'], [$fieldName, $tableName], \_AM_MODULEBUILDER_BUILDING_CHECK_FIELDS4); |
||
259 | $this->infos[] = ['icon' => 'warning', 'info' => $info]; |
||
260 | } |
||
261 | if (0 == $fieldParamsUser && 1 == $tableUser) { |
||
262 | $info = \str_replace(['%f', '%t'], [$fieldName, $tableName], \_AM_MODULEBUILDER_BUILDING_CHECK_FIELDS6); |
||
263 | $this->infos[] = ['icon' => 'warning', 'info' => $info]; |
||
264 | } |
||
265 | } |
||
266 | |||
267 | return true; |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * @private function getCheckBlock |
||
272 | * |
||
273 | * @return array|bool |
||
274 | */ |
||
275 | private function getCheckBlock() |
||
276 | { |
||
277 | //use in block but no field selected |
||
278 | foreach (\array_keys($this->tables) as $t) { |
||
279 | $tableId = $this->tables[$t]->getVar('table_id'); |
||
280 | $tableName = $this->tables[$t]->getVar('table_name'); |
||
281 | $fields = $this->cf->getTableFields($this->modId, $tableId); |
||
282 | $count = 0; |
||
283 | if (1 == $this->tables[$t]->getVar('table_blocks')) { |
||
284 | foreach (\array_keys($fields) as $f) { |
||
285 | if (1 == $fields[$f]->getVar('field_block')) { |
||
286 | $count++; |
||
287 | } |
||
288 | } |
||
289 | if (0 == $count) { |
||
290 | $info = \str_replace(['%t'], [$tableName], \_AM_MODULEBUILDER_BUILDING_CHECK_BLOCK1); |
||
291 | $this->infos[] = ['icon' => 'warning', 'info' => $info]; |
||
292 | } |
||
293 | } |
||
294 | } |
||
295 | //use in block but no field date |
||
296 | foreach (\array_keys($this->tables) as $t) { |
||
297 | $tableId = $this->tables[$t]->getVar('table_id'); |
||
298 | $tableName = $this->tables[$t]->getVar('table_name'); |
||
299 | |||
300 | $count = 0; |
||
301 | if (1 == $this->tables[$t]->getVar('table_blocks')) { |
||
302 | $fields = $this->cf->getTableFields($this->modId, $tableId); |
||
303 | foreach (\array_keys($fields) as $f) { |
||
304 | if (Constants::FIELD_ELE_TEXTDATESELECT == $fields[$f]->getVar('field_element') || Constants::FIELD_ELE_DATETIME == $fields[$f]->getVar('field_element')) { |
||
305 | $count++; |
||
306 | } |
||
307 | } |
||
308 | if (0 == $count) { |
||
309 | $info = \str_replace(['%t'], [$tableName], \_AM_MODULEBUILDER_BUILDING_CHECK_BLOCK2); |
||
310 | $this->infos[] = ['icon' => 'warning', 'info' => $info]; |
||
311 | } |
||
312 | } |
||
313 | } |
||
314 | |||
315 | return true; |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * @private function getCheckComments |
||
320 | * |
||
321 | * @return array|bool |
||
322 | */ |
||
323 | private function getCheckComments() |
||
324 | { |
||
325 | //use comments in multiple tables |
||
326 | $count = 0; |
||
327 | $tableComments = []; |
||
328 | foreach (\array_keys($this->tables) as $t) { |
||
329 | if (1 == $this->tables[$t]->getVar('table_comments')) { |
||
330 | $count++; |
||
331 | $tableComments[] = $this->tables[$t]->getVar('table_name'); |
||
332 | } |
||
333 | } |
||
334 | if ($count > 1) { |
||
335 | $tablesComments = \implode(', ', $tableComments); |
||
336 | $info = \str_replace('%t', $tablesComments, \_AM_MODULEBUILDER_BUILDING_CHECK_COMMENTS1); |
||
337 | $this->infos[] = ['icon' => 'error', 'info' => $info]; |
||
338 | } |
||
339 | |||
340 | foreach (\array_keys($this->tables) as $t) { |
||
341 | if (1 == $this->tables[$t]->getVar('table_comments')) { |
||
342 | $tableId = $this->tables[$t]->getVar('table_id'); |
||
343 | $tableName = $this->tables[$t]->getVar('table_name'); |
||
344 | $fields = $this->cf->getTableFields($this->modId, $tableId); |
||
345 | $fieldComments = 0; |
||
346 | foreach (\array_keys($fields) as $f) { |
||
347 | if (Constants::FIELD_ELE_TEXTCOMMENTS == (int)$fields[$f]->getVar('field_element')) { |
||
348 | $fieldComments++; |
||
349 | } |
||
350 | } |
||
351 | // check whether each table with handling "comments" has also a field "comments" |
||
352 | if (0 == $fieldComments) { |
||
353 | $info = \str_replace('%t', $tableName, \_AM_MODULEBUILDER_BUILDING_CHECK_COMMENTS2); |
||
354 | $this->infos[] = ['icon' => 'warning', 'info' => $info]; |
||
355 | } |
||
356 | } |
||
357 | } |
||
358 | |||
359 | return true; |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * @private function getCheckRatings |
||
364 | * |
||
365 | * @return array|bool |
||
366 | */ |
||
367 | private function getCheckRating() |
||
368 | { |
||
369 | foreach (\array_keys($this->tables) as $t) { |
||
370 | if (1 == $this->tables[$t]->getVar('table_rate')) { |
||
371 | $tableId = $this->tables[$t]->getVar('table_id'); |
||
372 | $tableName = $this->tables[$t]->getVar('table_name'); |
||
373 | $fields = $this->cf->getTableFields($this->modId, $tableId); |
||
374 | $fieldRatings = 0; |
||
375 | $fieldVotes = 0; |
||
376 | foreach (\array_keys($fields) as $f) { |
||
377 | if (Constants::FIELD_ELE_TEXTRATINGS == (int)$fields[$f]->getVar('field_element')) { |
||
378 | $fieldRatings++; |
||
379 | } |
||
380 | if (Constants::FIELD_ELE_TEXTVOTES == (int)$fields[$f]->getVar('field_element')) { |
||
381 | $fieldVotes++; |
||
382 | } |
||
383 | } |
||
384 | // check whether each table with handling "rating" has also a field "rating" |
||
385 | if (0 == (int)$fieldRatings) { |
||
386 | $info = \str_replace('%t', $tableName, \_AM_MODULEBUILDER_BUILDING_CHECK_RATINGS1); |
||
387 | $this->infos[] = ['icon' => 'error', 'info' => $info]; |
||
388 | } |
||
389 | // check whether each table with handling "rating" has also a field "votes" |
||
390 | if (0 == (int)$fieldVotes) { |
||
391 | $info = \str_replace('%t', $tableName, \_AM_MODULEBUILDER_BUILDING_CHECK_RATINGS2); |
||
392 | $this->infos[] = ['icon' => 'error', 'info' => $info]; |
||
393 | } |
||
394 | } |
||
395 | } |
||
396 | |||
397 | return true; |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * @private function getCheckReads |
||
402 | * |
||
403 | * @return array|bool |
||
404 | */ |
||
405 | private function getCheckReads() |
||
406 | { |
||
407 | foreach (\array_keys($this->tables) as $t) { |
||
408 | if (1 == $this->tables[$t]->getVar('table_reads')) { |
||
409 | $tableId = $this->tables[$t]->getVar('table_id'); |
||
410 | $tableName = $this->tables[$t]->getVar('table_name'); |
||
411 | $fields = $this->cf->getTableFields($this->modId, $tableId); |
||
412 | $fieldReads = 0; |
||
413 | foreach (\array_keys($fields) as $f) { |
||
414 | if (Constants::FIELD_ELE_TEXTREADS == (int)$fields[$f]->getVar('field_element')) { |
||
415 | $fieldReads++; |
||
416 | } |
||
417 | } |
||
418 | // check whether each table with handling "reads" has also a field "reads" |
||
419 | if (0 == (int)$fieldReads) { |
||
420 | $info = \str_replace('%t', $tableName, \_AM_MODULEBUILDER_BUILDING_CHECK_READS1); |
||
421 | $this->infos[] = ['icon' => 'error', 'info' => $info]; |
||
422 | } |
||
423 | } |
||
424 | } |
||
425 | |||
426 | return true; |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * @private function getCheckSql |
||
431 | * |
||
432 | * @return array|bool |
||
433 | */ |
||
434 | private function getCheckSql() |
||
459 | } |
||
460 | } |
||
461 |