Passed
Push — master ( c14394...753352 )
by Goffy
04:41
created

CheckData::getCheckUserpage()   D

Complexity

Conditions 16
Paths 240

Size

Total Lines 72
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 44
nc 240
nop 0
dl 0
loc 72
rs 4.2333
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        return true;
212
    }
213
214
    /**
215
     * @private function getCheckBlock
216
     *
217
     * @return array|bool
218
     */
219
    private function getCheckBlock()
220
    {
221
        //use in block but not field selected
222
        foreach (array_keys($this->tables) as $t) {
223
            $tableId = $this->tables[$t]->getVar('table_id');
224
            $tableName = $this->tables[$t]->getVar('table_name');
225
            $fields = $this->cf->getTableFields($this->modId, $tableId);
226
            $count = 0;
227
            if (1 == $this->tables[$t]->getVar('table_blocks')) {
228
                foreach (array_keys($fields) as $f) {
229
                    if (1 == $fields[$f]->getVar('field_block')) {
230
                        $count++;
231
                    }
232
                }
233
                if (0 == $count) {
234
                    $info = str_replace(['%t'], [$tableName], _AM_MODULEBUILDER_BUILDING_CHECK_BLOCK1);
235
                    $this->infos[] = ['icon' => 'warning', 'info' => $info];
236
                }
237
            }
238
        }
239
        //use in block but not field date
240
        foreach (array_keys($this->tables) as $t) {
241
            $tableId = $this->tables[$t]->getVar('table_id');
242
            $tableName = $this->tables[$t]->getVar('table_name');
243
244
            $count = 0;
245
            if (1 == $this->tables[$t]->getVar('table_blocks')) {
246
                $fields = $this->cf->getTableFields($this->modId, $tableId);
247
                foreach (array_keys($fields) as $f) {
248
                    if (15 == $fields[$f]->getVar('field_element') || 21 == $fields[$f]->getVar('field_element')) {
249
                        $count++;
250
                    }
251
                }
252
                if (0 == $count) {
253
                    $info = str_replace(['%t'], [$tableName], _AM_MODULEBUILDER_BUILDING_CHECK_BLOCK2);
254
                    $this->infos[] = ['icon' => 'warning', 'info' => $info];
255
                }
256
            }
257
        }
258
        return true;
259
    }
260
261
    /**
262
     * @private function getCheckComments
263
     *
264
     * @return array|bool
265
     */
266
    private function getCheckComments()
267
    {
268
        //use comments in multiple tables
269
        $count         = 0;
270
        $tableComments = [];
271
        foreach (array_keys($this->tables) as $t) {
272
            if (1 == $this->tables[$t]->getVar('table_comments')) {
273
                $count++;
274
                $tableComments[] = $this->tables[$t]->getVar('table_name');
275
            }
276
        }
277
        if ($count > 1) {
278
            $tablesComments = implode(', ', $tableComments);
279
            $info = str_replace('%t', $tablesComments, _AM_MODULEBUILDER_BUILDING_CHECK_COMMENTS1);
280
            $this->infos[] = ['icon' => 'error', 'info' => $info];
281
        }
282
283
        foreach (array_keys($this->tables) as $t) {
284
            if (1 == $this->tables[$t]->getVar('table_comments')) {
285
                $tableId = $this->tables[$t]->getVar('table_id');
286
                $tableName = $this->tables[$t]->getVar('table_name');
287
                $fields = $this->cf->getTableFields($this->modId, $tableId);
288
                $fieldComments = '';
289
290
                foreach (array_keys($fields) as $f) {
291
                    $fieldName = $fields[$f]->getVar('field_name');
292
                    if ($fieldName == $this->tables[$t]->getVar('table_fieldname') . '_comments') {
293
                        $fieldComments = $fieldName;
294
                    }
295
                }
296
                // check whether each table with handling "comments" has also a field "comments"
297
                if ('' == $fieldComments) {
298
                    $info = str_replace(['%f', '%t'], [$this->tables[$t]->getVar('table_fieldname') . '_comments', $tableName], _AM_MODULEBUILDER_BUILDING_CHECK_COMMENTS2);
299
                    $this->infos[] = ['icon' => 'warning', 'info' => $info];
300
                }
301
            }
302
        }
303
304
        return true;
305
    }
306
307
    /**
308
     * @private function getCheckComments
309
     *
310
     * @return array|bool
311
     */
312
    private function getCheckRating()
313
    {
314
        foreach (array_keys($this->tables) as $t) {
315
            if (1 == $this->tables[$t]->getVar('table_rate')) {
316
                $tableId = $this->tables[$t]->getVar('table_id');
317
                $tableName = $this->tables[$t]->getVar('table_name');
318
                $fields = $this->cf->getTableFields($this->modId, $tableId);
319
                $fieldRatings = '';
320
321
                foreach (array_keys($fields) as $f) {
322
                    $fieldName = $fields[$f]->getVar('field_name');
323
                    if ($fieldName == $this->tables[$t]->getVar('table_fieldname') . '_ratings') {
324
                        $fieldRatings = $fieldName;
325
                    }
326
                }
327
                // check whether each table with handling "rating" has also a field "rating"
328
                if ('' == $fieldRatings) {
329
                    $info = str_replace(['%f', '%t'], [$this->tables[$t]->getVar('table_fieldname') . '_ratings', $tableName], _AM_MODULEBUILDER_BUILDING_CHECK_RATINGS1);
330
                    $this->infos[] = ['icon' => 'error', 'info' => $info];
331
                }
332
            }
333
        }
334
        foreach (array_keys($this->tables) as $t) {
335
            if (1 == $this->tables[$t]->getVar('table_rate')) {
336
                $tableId = $this->tables[$t]->getVar('table_id');
337
                $tableName = $this->tables[$t]->getVar('table_name');
338
                $fields = $this->cf->getTableFields($this->modId, $tableId);
339
                $fieldVotes = '';
340
341
                foreach (array_keys($fields) as $f) {
342
                    $fieldName = $fields[$f]->getVar('field_name');
343
                    if ($fieldName == $this->tables[$t]->getVar('table_fieldname') . '_votes') {
344
                        $fieldVotes = $fieldName;
345
                    }
346
                }
347
                // check whether each table with handling "rating" has also a field "votes"
348
                if ('' == $fieldVotes) {
349
                    $info = str_replace(['%f', '%t'], [$this->tables[$t]->getVar('table_fieldname') . '_votes', $tableName], _AM_MODULEBUILDER_BUILDING_CHECK_RATINGS1);
350
                    $this->infos[] = ['icon' => 'error', 'info' => $info];
351
                }
352
            }
353
        }
354
355
        return true;
356
    }
357
}
358