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

UserRate::getUserRateSave()   B

Complexity

Conditions 7
Paths 15

Size

Total Lines 136
Code Lines 116

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 116
nc 15
nop 3
dl 0
loc 136
rs 7.0666
c 0
b 0
f 0

How to fix   Long Method   

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\User;
4
5
use XoopsModules\Modulebuilder;
6
use XoopsModules\Modulebuilder\Files;
7
8
/*
9
 You may not change or alter any portion of this comment or credits
10
 of supporting developers from this source code or any supporting source code
11
 which is considered copyrighted (c) material of the original comment or credit authors.
12
13
 This program is distributed in the hope that it will be useful,
14
 but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
 */
17
/**
18
 * modulebuilder module.
19
 *
20
 * @copyright       XOOPS Project (https://xoops.org)
21
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
22
 *
23
 * @since           2.5.0
24
 *
25
 * @author          Txmod Xoops http://www.txmodxoops.org
26
 *
27
 */
28
29
/**
30
 * Class UserRate.
31
 */
32
class UserRate extends Files\CreateFile
33
{
34
    /**
35
     * @var mixed
36
     */
37
    private $xc = null;
38
	
39
	/**
40
     * @var mixed
41
     */
42
    private $pc = null;
43
	
44
	/**
45
     * @var mixed
46
     */
47
    private $uxc = null;
48
	
49
	
50
	/**
51
     * @public function constructor
52
     *
53
     * @param null
54
     */
55
    public function __construct()
56
    {
57
        parent::__construct();
58
        $this->xc  = Modulebuilder\Files\CreateXoopsCode::getInstance();
59
        $this->pc  = Modulebuilder\Files\CreatePhpCode::getInstance();
60
        $this->uxc = UserXoopsCode::getInstance();
61
    }
62
63
    /**
64
     * @static function getInstance
65
     *
66
     * @param null
67
     *
68
     * @return UserRate
69
     */
70
    public static function getInstance()
71
    {
72
        static $instance = false;
73
        if (!$instance) {
74
            $instance = new self();
75
        }
76
77
        return $instance;
78
    }
79
80
    /**
81
     * @public function write
82
     * @param string $module
83
     * @param $tables
84
     * @param string $filename
85
     */
86
    public function write($module, $tables, $filename)
87
    {
88
        $this->setModule($module);
89
        $this->setTables($tables);
90
        $this->setFileName($filename);
91
    }
92
93
    /**
94
     * @private function getUserRateHeader
95
     *
96
     * @param $moduleDirname
97
     * @return string
98
     */
99
    public function getUserRateHeader($moduleDirname)
100
    {
101
        $ret = $this->pc->getPhpCodeUseNamespace(['Xmf', 'Request'], '', '');
102
        $ret .= $this->pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname], '', '');
103
        $ret .= $this->pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname, 'Constants']);
104
        $ret .= $this->getInclude();
105
        $ret .= $this->xc->getXcXoopsRequest('op', 'op', 'list', 'Cmd');
106
        $ret .= $this->xc->getXcXoopsRequest('source', 'source', '0', 'Int');
107
108
        return $ret;
109
    }
110
111
    /**
112
     * @private function getUserRateSwitch
113
     * @param $tables
114
     * @param $language
115
     * @return string
116
     */
117
    private function getUserRateSwitch($tables, $language)
118
    {
119
        $t = "\t\t";
120
        $cases  = [
121
            'list' => [$this->getUserRateDefault( $t)],
122
            'save' => [$this->getUserRateSave($tables, $language,  $t)],
123
        ];
124
125
        return $this->xc->getXcSwitch('op', $cases, true);
126
    }
127
128
    /**
129
     * @public function getAdminPagesList
130
     * @param string $t
131
     * @return string
132
     */
133
    public function getUserRateDefault($t = '')
134
    {
135
        $ret = $this->pc->getPhpCodeCommentLine('default should not happen','', $t);
136
        $ret .= $this->xc->getXcRedirectHeader('index', '', '3', '_NOPERM', true, $t);
137
138
        return $ret;
139
    }
140
141
    /**
142
     * @public function getUserRateSave
143
     * @param $tables
144
     * @param $language
145
     * @param $t
146
     * @return string
147
     */
148
    public function getUserRateSave($tables, $language, $t)
149
    {
150
        $ret                = $this->pc->getPhpCodeCommentLine('Security Check', '', $t);
151
        $xoopsSecurityCheck = $this->xc->getXcXoopsSecurityCheck();
152
        $securityError      = $this->xc->getXcXoopsSecurityErrors();
153
        $implode            = $this->pc->getPhpCodeImplode(',', $securityError);
154
        $redirectError      = $this->xc->getXcRedirectHeader('index', '', '3', $implode, true, $t . "\t");
155
        $ret                .= $this->pc->getPhpCodeConditions($xoopsSecurityCheck, '', '', $redirectError, false, $t);
156
157
        $ret .= $this->xc->getXcXoopsRequest('rating', 'rating', '0', 'Int', false, $t);
158
        $ret .= $this->xc->getXcEqualsOperator('$itemid', '0','', $t);
159
        $ret .= $this->xc->getXcEqualsOperator('$redir ', "\$_SERVER['HTTP_REFERER']",'', $t);
160
        foreach ($tables as $table) {
161
            $tableName = $table->getVar('table_name');
162
            $stuTableName = mb_strtoupper($tableName);
163
            if (1 == $table->getVar('table_rate')) {
164
                $fields = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
165
                $fieldId = '';
166
                foreach (array_keys($fields) as $f) {
167
                    if (0 == $f) {
168
                        $fieldId = $fields[$f]->getVar('field_name');
169
                    }
170
                }
171
                $contIf = $this->xc->getXcXoopsRequest('itemid', $fieldId, '0', 'Int', false, $t . "\t");
172
                $contIf .= $this->xc->getXcEqualsOperator('$redir', "'{$tableName}.php?op=show&amp;{$fieldId}=' . \$itemid",'', $t . "\t");
173
                $const = $this->xc->getXcGetConstants('TABLE_' . $stuTableName);
174
                $ret .= $this->pc->getPhpCodeConditions('$source', ' === ', $const, $contIf, false, $t);
175
            }
176
        }
177
178
        $ret    .= $this->pc->getPhpCodeBlankLine();
179
        $ret    .= $this->pc->getPhpCodeCommentLine('Check permissions', '', $t);
180
        $ret    .= $this->xc->getXcEqualsOperator('$rate_allowed', 'false','', $t);
181
        $xUser  = $this->pc->getPhpCodeGlobals('xoopsUser');
182
        $ret    .= $this->pc->getPhpCodeTernaryOperator('groups', '(isset(' . $xUser . ') && is_object(' . $xUser . '))', $xUser . '->getGroups()', 'XOOPS_GROUP_ANONYMOUS', "\t\t");
183
        $contIf = $this->xc->getXcEqualsOperator('$rate_allowed', 'true','', $t . "\t\t");
184
        $contIf .= $this->getSimpleString('break;', $t . "\t\t");
185
        $cond   = 'XOOPS_GROUP_ADMIN == $group || in_array($group, $helper->getConfig(\'ratingbar_groups\'))';
186
        $contFe = $this->pc->getPhpCodeConditions($cond, '', '', $contIf, false, $t . "\t");
187
        $ret    .= $this->pc->getPhpCodeForeach('groups', false, false, 'group', $contFe, $t);
188
        $contIf = $this->xc->getXcRedirectHeader('index', '', '3', $language . 'RATING_NOPERM', true, $t . "\t");
189
        $ret    .= $this->pc->getPhpCodeConditions('!$rate_allowed', '', '', $contIf, false, $t);
190
191
        $ret           .= $this->pc->getPhpCodeBlankLine();
192
        $ret           .= $this->pc->getPhpCodeCommentLine('Check rating value', null, $t);
193
        $cases         = [];
194
        $contIf        = $this->xc->getXcRedirectHeader('index', '', '3', $language . 'RATING_VOTE_BAD', true, $t . "\t\t");
195
        $contIf        .= $this->getSimpleString('exit;', $t . "\t\t");
196
        $const         = $this->xc->getXcGetConstants('RATING_NONE');
197
        $cases[$const] =  [$contIf];
198
        $contIf        = $this->xc->getXcRedirectHeader('index', '', '3', $language . 'RATING_VOTE_BAD', true, $t . "\t\t\t");
199
        $contIf        .= $this->getSimpleString('exit;', $t . "\t\t\t");
200
        $const         = $this->xc->getXcGetConstants('RATING_LIKES');
201
        $cases[$const] = [$this->pc->getPhpCodeConditions('$rating > 1 || $rating < -1', '', '', $contIf, false, $t . "\t\t")];
202
        $const         = $this->xc->getXcGetConstants('RATING_5STARS');
203
        $cases[$const] = [$this->pc->getPhpCodeConditions('$rating > 5 || $rating < 1', '', '', $contIf, false, $t . "\t\t")];
204
        $const         = $this->xc->getXcGetConstants('RATING_10STARS');
205
        $cases[$const] = '';
206
        $const         = $this->xc->getXcGetConstants('RATING_10NUM');
207
        $cases[$const] = [$this->pc->getPhpCodeConditions('$rating > 10 || $rating < 1', '', '', $contIf, false, $t . "\t\t")];
208
        $config        = '(int)' . $this->xc->getXcGetConfig('ratingbars');
209
        $ret           .= $this->xc->getXcSwitch($config, $cases, true, false, $t, false, true);
210
211
        $ret .= $this->pc->getPhpCodeBlankLine();
212
        $ret .= $this->pc->getPhpCodeCommentLine('Get existing rating', null, $t);
213
        $ret .= $this->xc->getXcEqualsOperator('$itemrating', '$ratingsHandler->getItemRating($itemid, $source)','', $t);
214
215
        $ret      .= $this->pc->getPhpCodeBlankLine();
216
        $ret      .= $this->pc->getPhpCodeCommentLine('Set data rating', null, $t);
217
        $contIf   = $this->pc->getPhpCodeCommentLine('If yo want to avoid revoting then activate next line', null, $t . "\t");
218
        $contIf   .= $t . "\t//" . $this->xc->getXcRedirectHeader('index', '', '3', $language . 'RATING_VOTE_BAD');
219
        $contIf   .= $this->xc->getXcHandlerGet('ratings', "itemrating['id']",'Obj', 'ratingsHandler', false, $t . "\t");
220
        $contElse = $this->xc->getXcHandlerCreateObj('ratings', $t . "\t");
221
        $ret      .= $this->pc->getPhpCodeConditions("\$itemrating['voted']", '', '', $contIf, $contElse, $t);
222
        $ret      .= $this->xc->getXcSetVarObj('ratings', 'rate_source', '$source', $t);
223
        $ret      .= $this->xc->getXcSetVarObj('ratings', 'rate_itemid', '$itemid', $t);
224
        $ret      .= $this->xc->getXcSetVarObj('ratings', 'rate_value', '$rating', $t);
225
        $ret      .= $this->xc->getXcSetVarObj('ratings', 'rate_uid', "\$itemrating['uid']", $t);
226
        $ret      .= $this->xc->getXcSetVarObj('ratings', 'rate_ip', "\$itemrating['ip']", $t);
227
        $ret      .= $this->xc->getXcSetVarObj('ratings', 'rate_date', 'time()', $t);
228
        $ret      .= $this->pc->getPhpCodeCommentLine('Insert Data', null, $t);
229
        $insert   = $this->xc->getXcHandlerInsert('ratings', 'ratings', 'Obj');
230
        $contIf   = $this->pc->getPhpCodeUnset('ratingsObj', $t . "\t");
231
232
        $contIf       .= $this->pc->getPhpCodeCommentLine('Calc average rating value', null, $t . "\t");
233
        $contIf       .= $this->xc->getXcEqualsOperator('$nb_ratings    ', '0','', $t . "\t");
234
        $contIf       .= $this->xc->getXcEqualsOperator('$avg_rate_value', '0','', $t . "\t");
235
        $contIf       .= $this->xc->getXcEqualsOperator('$current_rating', '0','', $t . "\t");
236
        $tableName    = 'ratings';
237
        $ucfTableName = ucfirst($tableName);
238
        $critName     = 'cr' . $ucfTableName;
239
        $contIf       .= $this->xc->getXcCriteriaCompo($critName, $t . "\t");
240
        $crit         = $this->xc->getXcCriteria('', "'rate_source'", '$source','',true);
241
        $contIf       .= $this->xc->getXcCriteriaAdd($critName, $crit, $t . "\t");
242
        $crit         = $this->xc->getXcCriteria('', "'rate_itemid'", '$itemid','',true);
243
        $contIf       .= $this->xc->getXcCriteriaAdd($critName, $crit, $t . "\t");
244
        $contIf       .= $this->xc->getXcHandlerCountClear($tableName . 'Count', $tableName, '$' . $critName, $t . "\t");
245
        $contIf       .= $this->xc->getXcHandlerAllClear($tableName . 'All', $tableName, '$' . $critName, $t . "\t");
246
        $contFe       = $this->xc->getXcEqualsOperator('$current_rating', "\$ratingsAll[\$i]->getVar('rate_value')",'+', $t . "\t\t");
247
        $contIf       .= $this->pc->getPhpCodeForeach("{$tableName}All", true, false, 'i', $contFe, $t . "\t");
248
        $contIf       .= $this->pc->getPhpCodeUnset($tableName . 'All', $t . "\t");
249
        $contIfInt    = $this->xc->getXcEqualsOperator('$avg_rate_value', 'number_format($current_rating / $ratingsCount, 2)','', $t . "\t\t");
250
        $contIf       .= $this->pc->getPhpCodeConditions('$ratingsCount', ' > ', '0', $contIfInt, false, $t . "\t");
251
252
        $contIf .= $this->pc->getPhpCodeCommentLine('Update related table', null, $t . "\t");
253
        foreach ($tables as $table) {
254
            $tableName      = $table->getVar('table_name');
255
            $tableFieldName = $table->getVar('table_fieldname');
256
            $stuTableName   = mb_strtoupper($tableName);
257
            if (1 == $table->getVar('table_rate')) {
258
                $contIfInt = $this->xc->getXcEqualsOperator('$tableName', "'{$tableName}'",'', $t . "\t\t");
259
                $contIfInt .= $this->xc->getXcEqualsOperator('$fieldRatings', "'{$tableFieldName}_ratings'",'', $t . "\t\t");
260
                $contIfInt .= $this->xc->getXcEqualsOperator('$fieldVotes  ', "'{$tableFieldName}_votes'",'', $t . "\t\t");
261
                $contIfInt .= $this->xc->getXcHandlerGetObj($tableName, 'itemid', $t . "\t\t");
262
                $contIfInt .= $this->xc->getXcSetVarObj($tableName, "{$tableFieldName}_ratings", '$avg_rate_value', $t . "\t\t");
263
                $contIfInt .= $this->xc->getXcSetVarObj($tableName, "{$tableFieldName}_votes", '$ratingsCount', $t . "\t\t");
264
                $insertInt = $this->xc->getXcHandlerInsert($tableName, $tableName, 'Obj');
265
266
                $insertOK  = $this->xc->getXcRedirectHeader('$redir', '', '2', "{$language}RATING_VOTE_THANKS", false, $t . "\t\t\t");
267
                $insertErr = $this->xc->getXcRedirectHeader($tableName, '', '3', "{$language}RATING_ERROR1", true, $t . "\t\t\t");
268
                $contIfInt .= $this->pc->getPhpCodeConditions($insertInt, '', '', $insertOK, $insertErr, $t. "\t\t");
269
                $contIfInt .= $this->pc->getPhpCodeUnset($tableName . 'Obj', $t . "\t\t");
270
                $const     = $this->xc->getXcGetConstants('TABLE_' . $stuTableName);
271
                $contIf    .= $this->pc->getPhpCodeConditions('$source', ' === ', $const, $contIfInt, false, $t . "\t");
272
            }
273
        }
274
275
        $contIf .= $this->pc->getPhpCodeBlankLine();
276
        $contIf .= $this->xc->getXcRedirectHeader('index', '', '2', "{$language}RATING_VOTE_THANKS", true, $t . "\t");
277
        $ret    .= $this->pc->getPhpCodeConditions($insert, '', '', $contIf, false, $t);
278
279
        $ret .= $this->pc->getPhpCodeCommentLine('Get Error', null, $t);
280
        $ret .= $this->getSimpleString("echo 'Error: ' . \$ratingsObj->getHtmlErrors();", $t);
281
282
283
        return $ret;
284
    }
285
286
    /**
287
     * @public function getUserRateFooter
288
     * @return string
289
     */
290
    public function getUserRateFooter()
291
    {
292
        return $this->getInclude('footer');
293
    }
294
295
    /**
296
     * @public function render
297
     * @param null
298
     * @return bool|string
299
     */
300
    public function render()
301
    {
302
        $module             = $this->getModule();
303
        $tables             = $this->getTables();
304
        $filename           = $this->getFileName();
305
        $moduleDirname      = $module->getVar('mod_dirname');
306
        $language           = $this->getLanguage($moduleDirname, 'MA');
307
        $content            = $this->getHeaderFilesComments($module);
308
        $content            .= $this->getUserRateHeader($moduleDirname);
309
        $content            .= $this->getUserRateSwitch($tables, $language);
310
        $content            .= $this->getUserRateFooter($language);
0 ignored issues
show
Unused Code introduced by
The call to XoopsModules\Modulebuild...te::getUserRateFooter() has too many arguments starting with $language. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

310
        $content            .= $this->/** @scrutinizer ignore-call */ getUserRateFooter($language);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
311
312
        $this->create($moduleDirname, '/', $filename, $content, _AM_MODULEBUILDER_FILE_CREATED, _AM_MODULEBUILDER_FILE_NOTCREATED);
313
314
        return $this->renderFile();
315
    }
316
}
317