Passed
Push — master ( e1cde6...891b58 )
by Goffy
03:33
created

CheckData   F

Complexity

Total Complexity 63

Size/Duplication

Total Lines 332
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 159
dl 0
loc 332
rs 3.36
c 1
b 0
f 1
wmc 63

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getInstance() 0 8 2
A getCheckBroken() 0 24 6
A getCheckPreBuilding() 0 13 1
B getCheckRating() 0 44 11
F getCheckUserpage() 0 81 21
C getCheckBlock() 0 40 12
B getCheckComments() 0 39 9

How to fix   Complexity   

Complex Class

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
2
3
namespace XoopsModules\Modulebuilder\Files;
4
5
use XoopsModules\Modulebuilder;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
 */
16
/**
17
 * modulebuilder module.
18
 *
19
 * @copyright       XOOPS Project (https://xoops.org)
20
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
21
 *
22
 * @since           2.5.0
23
 *
24
 * @author          Txmod Xoops http://www.txmodxoops.org
25
 *
26
 */
27
28
//include dirname(__DIR__) . '/autoload.php';
29
30
/**
31
 * Class CheckData.
32
 */
33
class CheckData
34
{
35
    /**
36
     * @var mixed
37
     */
38
    private $cf = null;
39
40
    /**
41
     * @var mixed
42
     */
43
    private $modId = null;
44
45
    /**
46
     * @var mixed
47
     */
48
    private $tables = null;
49
50
    /**
51
     * @var mixed
52
     */
53
    private $infos = [];
54
55
56
57
    /**
58
     * @public function constructor
59
     * @param null
60
     */
61
    public function __construct()
62
    {
63
        $this->cf = Modulebuilder\Files\CreateFile::getInstance();
64
    }
65
66
    /**
67
     * @static function getInstance
68
     *
69
     * @param null
70
     *
71
     * @return Modulebuilder\Files\CheckData
72
     */
73
    public static function getInstance()
74
    {
75
        static $instance = false;
76
        if (!$instance) {
77
            $instance = new self();
78
        }
79
80
        return $instance;
81
    }
82
83
    /**
84
     * @public function getCheckResult
85
     *
86
     * @param $module
87
     * @return array
88
     */
89
    public function getCheckPreBuilding($module)
90
    {
91
        $this->modId  = $module->getVar('mod_id');
92
        $this->tables = $this->cf->getTableTables($this->modId);
93
        $this->infos = [];
94
95
        $this->getCheckBlock();
96
        $this->getCheckBroken();
97
        $this->getCheckComments();
98
        $this->getCheckUserpage();
99
        $this->getCheckRating();
100
101
        return $this->infos;
102
    }
103
104
    /**
105
     * @public function getCheckBroken
106
     *
107
     * @return array|bool
108
     */
109
    private function getCheckBroken()
110
    {
111
        foreach (array_keys($this->tables) as $t) {
112
            if (1 == $this->tables[$t]->getVar('table_broken')) {
113
                $tableId = $this->tables[$t]->getVar('table_id');
114
                $tableName = $this->tables[$t]->getVar('table_name');
115
                $fields = $this->cf->getTableFields($this->modId, $tableId);
116
                $fieldSatus = '';
117
118
                foreach (array_keys($fields) as $f) {
119
                    $fieldName = $fields[$f]->getVar('field_name');
120
                    if (16 == $fields[$f]->getVar('field_element')) {
121
                        $fieldSatus = $fieldName;
122
                    }
123
                }
124
                // check whether each table with handling "broken" has also a field "status"
125
                if ('' == $fieldSatus) {
126
                    $info = str_replace('%t', $tableName, _AM_MODULEBUILDER_BUILDING_CHECK_BROKEN1);
127
                    $this->infos[] = ['icon' => 'error', 'info' => $info];
128
                }
129
            }
130
        }
131
132
        return true;
133
    }
134
135
    /**
136
     * @private function getCheckUserpage
137
     *
138
     * @return array|bool
139
     */
140
    private function getCheckUserpage()
141
    {
142
        foreach (array_keys($this->tables) as $t) {
143
            $tableId = $this->tables[$t]->getVar('table_id');
144
            $tableName = $this->tables[$t]->getVar('table_name');
145
            $fields = $this->cf->getTableFields($this->modId, $tableId);
146
147
            foreach (array_keys($fields) as $f) {
148
                $fieldName = $fields[$f]->getVar('field_name');
149
                // check fields for parameters
150
                if ($f > 0) {
151
                    $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')
152
                        + (int)$fields[$f]->getVar('field_user') + (int)$fields[$f]->getVar('field_ihead') + (int)$fields[$f]->getVar('field_ibody') + (int)$fields[$f]->getVar('field_ifoot')
153
                        + (int)$fields[$f]->getVar('field_thead') + (int)$fields[$f]->getVar('field_tbody') + (int)$fields[$f]->getVar('field_tfoot') + (int)$fields[$f]->getVar('field_block')
154
                        + (int)$fields[$f]->getVar('field_main') + (int)$fields[$f]->getVar('field_search') + (int)$fields[$f]->getVar('field_required');
155
                    if (0 == $fieldParams) {
156
                        $info = str_replace(['%f', '%t'], [$fieldName, $tableName], _AM_MODULEBUILDER_BUILDING_CHECK_FIELDS1);
157
                        $this->infos[] = ['icon' => 'error', 'info' => $info];
158
                    }
159
                }
160
            }
161
        }
162
163
        //check user file no usage in index or item
164
        foreach (array_keys($this->tables) as $t) {
165
            $tableId = $this->tables[$t]->getVar('table_id');
166
            $tableName = $this->tables[$t]->getVar('table_name');
167
            $fields = $this->cf->getTableFields($this->modId, $tableId);
168
169
            foreach (array_keys($fields) as $f) {
170
                $fieldName = $fields[$f]->getVar('field_name');
171
                if (1 == $fields[$f]->getVar('field_user')) {
172
                    // check fields for parameters
173
                    if ($f > 0) {
174
                        $fieldParams = (int)$fields[$f]->getVar('field_ihead') + (int)$fields[$f]->getVar('field_ibody') + (int)$fields[$f]->getVar('field_ifoot')
175
                            + (int)$fields[$f]->getVar('field_thead') + (int)$fields[$f]->getVar('field_tbody') + (int)$fields[$f]->getVar('field_tfoot');
176
                        if (0 == $fieldParams) {
177
                            $info = str_replace(['%f', '%t'], [$fieldName, $tableName], _AM_MODULEBUILDER_BUILDING_CHECK_FIELDS2);
178
                            $this->infos[] = ['icon' => 'warning', 'info' => $info];
179
                        }
180
                    }
181
                }
182
            }
183
        }
184
        //check user file index multiple usage
185
        //check user file item multiple usage
186
        foreach (array_keys($this->tables) as $t) {
187
            $tableId = $this->tables[$t]->getVar('table_id');
188
            $tableName = $this->tables[$t]->getVar('table_name');
189
            $fields = $this->cf->getTableFields($this->modId, $tableId);
190
191
            foreach (array_keys($fields) as $f) {
192
                $fieldName = $fields[$f]->getVar('field_name');
193
                if (1 == $fields[$f]->getVar('field_user')) {
194
                    // check fields for parameters
195
                    if ($f > 0) {
196
                        $fieldParams = (int)$fields[$f]->getVar('field_ihead') + (int)$fields[$f]->getVar('field_ibody') + (int)$fields[$f]->getVar('field_ifoot');
197
                        if ($fieldParams > 1) {
198
                            $info = str_replace(['%f', '%t'], [$fieldName, $tableName], _AM_MODULEBUILDER_BUILDING_CHECK_FIELDS3);
199
                            $this->infos[] = ['icon' => 'warning', 'info' => $info];
200
                        }
201
                        $fieldParams = (int)$fields[$f]->getVar('field_thead') + (int)$fields[$f]->getVar('field_tbody') + (int)$fields[$f]->getVar('field_tfoot');
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
                    }
207
                }
208
            }
209
        }
210
211
        //check user submit or rate or broken, but table not for user side
212
        foreach (array_keys($this->tables) as $t) {
213
            $tableId = $this->tables[$t]->getVar('table_id');
0 ignored issues
show
Unused Code introduced by
The assignment to $tableId is dead and can be removed.
Loading history...
214
            $tableName = $this->tables[$t]->getVar('table_name');
215
            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'))) {
216
                $info = str_replace(['%t'], [$tableName], _AM_MODULEBUILDER_BUILDING_CHECK_USERPAGE1);
217
                $this->infos[] = ['icon' => 'error', 'info' => $info];
218
            }
219
        }
220
        return true;
221
    }
222
223
    /**
224
     * @private function getCheckBlock
225
     *
226
     * @return array|bool
227
     */
228
    private function getCheckBlock()
229
    {
230
        //use in block but no field selected
231
        foreach (array_keys($this->tables) as $t) {
232
            $tableId = $this->tables[$t]->getVar('table_id');
233
            $tableName = $this->tables[$t]->getVar('table_name');
234
            $fields = $this->cf->getTableFields($this->modId, $tableId);
235
            $count = 0;
236
            if (1 == $this->tables[$t]->getVar('table_blocks')) {
237
                foreach (array_keys($fields) as $f) {
238
                    if (1 == $fields[$f]->getVar('field_block')) {
239
                        $count++;
240
                    }
241
                }
242
                if (0 == $count) {
243
                    $info = str_replace(['%t'], [$tableName], _AM_MODULEBUILDER_BUILDING_CHECK_BLOCK1);
244
                    $this->infos[] = ['icon' => 'warning', 'info' => $info];
245
                }
246
            }
247
        }
248
        //use in block but no field date
249
        foreach (array_keys($this->tables) as $t) {
250
            $tableId = $this->tables[$t]->getVar('table_id');
251
            $tableName = $this->tables[$t]->getVar('table_name');
252
253
            $count = 0;
254
            if (1 == $this->tables[$t]->getVar('table_blocks')) {
255
                $fields = $this->cf->getTableFields($this->modId, $tableId);
256
                foreach (array_keys($fields) as $f) {
257
                    if (15 == $fields[$f]->getVar('field_element') || 21 == $fields[$f]->getVar('field_element')) {
258
                        $count++;
259
                    }
260
                }
261
                if (0 == $count) {
262
                    $info = str_replace(['%t'], [$tableName], _AM_MODULEBUILDER_BUILDING_CHECK_BLOCK2);
263
                    $this->infos[] = ['icon' => 'warning', 'info' => $info];
264
                }
265
            }
266
        }
267
        return true;
268
    }
269
270
    /**
271
     * @private function getCheckComments
272
     *
273
     * @return array|bool
274
     */
275
    private function getCheckComments()
276
    {
277
        //use comments in multiple tables
278
        $count         = 0;
279
        $tableComments = [];
280
        foreach (array_keys($this->tables) as $t) {
281
            if (1 == $this->tables[$t]->getVar('table_comments')) {
282
                $count++;
283
                $tableComments[] = $this->tables[$t]->getVar('table_name');
284
            }
285
        }
286
        if ($count > 1) {
287
            $tablesComments = implode(', ', $tableComments);
288
            $info = str_replace('%t', $tablesComments, _AM_MODULEBUILDER_BUILDING_CHECK_COMMENTS1);
289
            $this->infos[] = ['icon' => 'error', 'info' => $info];
290
        }
291
292
        foreach (array_keys($this->tables) as $t) {
293
            if (1 == $this->tables[$t]->getVar('table_comments')) {
294
                $tableId = $this->tables[$t]->getVar('table_id');
295
                $tableName = $this->tables[$t]->getVar('table_name');
296
                $fields = $this->cf->getTableFields($this->modId, $tableId);
297
                $fieldComments = '';
298
299
                foreach (array_keys($fields) as $f) {
300
                    $fieldName = $fields[$f]->getVar('field_name');
301
                    if ($fieldName == $this->tables[$t]->getVar('table_fieldname') . '_comments') {
302
                        $fieldComments = $fieldName;
303
                    }
304
                }
305
                // check whether each table with handling "comments" has also a field "comments"
306
                if ('' == $fieldComments) {
307
                    $info = str_replace(['%f', '%t'], [$this->tables[$t]->getVar('table_fieldname') . '_comments', $tableName], _AM_MODULEBUILDER_BUILDING_CHECK_COMMENTS2);
308
                    $this->infos[] = ['icon' => 'warning', 'info' => $info];
309
                }
310
            }
311
        }
312
313
        return true;
314
    }
315
316
    /**
317
     * @private function getCheckComments
318
     *
319
     * @return array|bool
320
     */
321
    private function getCheckRating()
322
    {
323
        foreach (array_keys($this->tables) as $t) {
324
            if (1 == $this->tables[$t]->getVar('table_rate')) {
325
                $tableId = $this->tables[$t]->getVar('table_id');
326
                $tableName = $this->tables[$t]->getVar('table_name');
327
                $fields = $this->cf->getTableFields($this->modId, $tableId);
328
                $fieldRatings = '';
329
330
                foreach (array_keys($fields) as $f) {
331
                    $fieldName = $fields[$f]->getVar('field_name');
332
                    if ($fieldName == $this->tables[$t]->getVar('table_fieldname') . '_ratings') {
333
                        $fieldRatings = $fieldName;
334
                    }
335
                }
336
                // check whether each table with handling "rating" has also a field "rating"
337
                if ('' == $fieldRatings) {
338
                    $info = str_replace(['%f', '%t'], [$this->tables[$t]->getVar('table_fieldname') . '_ratings', $tableName], _AM_MODULEBUILDER_BUILDING_CHECK_RATINGS1);
339
                    $this->infos[] = ['icon' => 'error', 'info' => $info];
340
                }
341
            }
342
        }
343
        foreach (array_keys($this->tables) as $t) {
344
            if (1 == $this->tables[$t]->getVar('table_rate')) {
345
                $tableId = $this->tables[$t]->getVar('table_id');
346
                $tableName = $this->tables[$t]->getVar('table_name');
347
                $fields = $this->cf->getTableFields($this->modId, $tableId);
348
                $fieldVotes = '';
349
350
                foreach (array_keys($fields) as $f) {
351
                    $fieldName = $fields[$f]->getVar('field_name');
352
                    if ($fieldName == $this->tables[$t]->getVar('table_fieldname') . '_votes') {
353
                        $fieldVotes = $fieldName;
354
                    }
355
                }
356
                // check whether each table with handling "rating" has also a field "votes"
357
                if ('' == $fieldVotes) {
358
                    $info = str_replace(['%f', '%t'], [$this->tables[$t]->getVar('table_fieldname') . '_votes', $tableName], _AM_MODULEBUILDER_BUILDING_CHECK_RATINGS1);
359
                    $this->infos[] = ['icon' => 'error', 'info' => $info];
360
                }
361
            }
362
        }
363
364
        return true;
365
    }
366
}
367