Total Complexity | 41 |
Total Lines | 586 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like UserPages 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 UserPages, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class UserPages extends Files\CreateFile |
||
36 | { |
||
37 | /** |
||
38 | * @var mixed |
||
39 | */ |
||
40 | private $uxc = null; |
||
41 | |||
42 | /** |
||
43 | * @var mixed |
||
44 | */ |
||
45 | private $xc = null; |
||
46 | |||
47 | /** |
||
48 | * @var mixed |
||
49 | */ |
||
50 | private $pc = null; |
||
51 | |||
52 | /** |
||
53 | * @public function constructor |
||
54 | * @param null |
||
55 | */ |
||
56 | public function __construct() |
||
57 | { |
||
58 | parent::__construct(); |
||
59 | $this->xc = Modulebuilder\Files\CreateXoopsCode::getInstance(); |
||
60 | $this->pc = Modulebuilder\Files\CreatePhpCode::getInstance(); |
||
61 | $this->uxc = Modulebuilder\Files\User\UserXoopsCode::getInstance(); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @static function getInstance |
||
66 | * @param null |
||
67 | * @return UserPages |
||
68 | */ |
||
69 | public static function getInstance() |
||
70 | { |
||
71 | static $instance = false; |
||
72 | if (!$instance) { |
||
73 | $instance = new self(); |
||
74 | } |
||
75 | |||
76 | return $instance; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @public function write |
||
81 | * @param $module |
||
82 | * @param $table |
||
83 | * @param $filename |
||
84 | */ |
||
85 | public function write($module, $table, $filename) |
||
86 | { |
||
87 | $this->setModule($module); |
||
88 | $this->setTable($table); |
||
89 | $this->setFileName($filename); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @private function getUserPagesHeader |
||
94 | * @param $moduleDirname |
||
95 | * @param $tableName |
||
96 | * @param $fieldId |
||
97 | * @param $tablePermissions |
||
98 | * @return string |
||
99 | */ |
||
100 | private function getUserPagesHeader($moduleDirname, $tableName, $fieldId, $tablePermissions) |
||
101 | { |
||
102 | $stuModuleDirname = \mb_strtoupper($moduleDirname); |
||
103 | $ccFieldId = $this->getCamelCase($fieldId, false, true); |
||
104 | |||
105 | $ret = $this->pc->getPhpCodeUseNamespace(['Xmf', 'Request'], '', ''); |
||
106 | $ret .= $this->pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname], '', ''); |
||
107 | $ret .= $this->pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname, 'Constants'], '', ''); |
||
108 | $ret .= $this->pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname, 'Common']); |
||
109 | $ret .= $this->getInclude(); |
||
110 | $ret .= $this->uxc->getUserTplMain($moduleDirname, $tableName); |
||
111 | $ret .= $this->pc->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'header', true); |
||
112 | $ret .= $this->pc->getPhpCodeBlankLine(); |
||
113 | $ret .= $this->xc->getXcXoopsRequest('op ', 'op', 'list', 'Cmd'); |
||
114 | $ret .= $this->xc->getXcXoopsRequest('start', 'start', '0', 'Int'); |
||
115 | $userpager = $this->xc->getXcGetConfig('userpager'); |
||
116 | $ret .= $this->xc->getXcXoopsRequest('limit', 'limit', $userpager, 'Int'); |
||
117 | $ret .= $this->xc->getXcXoopsRequest($ccFieldId, $fieldId, '0', 'Int'); |
||
118 | $ret .= $this->pc->getPhpCodeBlankLine(); |
||
119 | $ret .= $this->pc->getPhpCodeCommentLine('Define Stylesheet'); |
||
120 | $ret .= $this->xc->getXcXoThemeAddStylesheet(); |
||
121 | $ret .= $this->pc->getPhpCodeBlankLine(); |
||
122 | $ret .= $this->xc->getXcXoopsTplAssign('xoops_icons32_url', 'XOOPS_ICONS32_URL'); |
||
123 | $ret .= $this->xc->getXcXoopsTplAssign("{$moduleDirname}_url", "{$stuModuleDirname}_URL"); |
||
124 | $ret .= $this->pc->getPhpCodeBlankLine(); |
||
125 | $ret .= $this->pc->getPhpCodeArray('keywords', null, false, ''); |
||
126 | $ret .= $this->pc->getPhpCodeBlankLine(); |
||
127 | if (1 == $tablePermissions) { |
||
128 | $ret .= $this->xc->getXcEqualsOperator('$permEdit', '$permissionsHandler->getPermGlobalSubmit()'); |
||
129 | $ret .= $this->xc->getXcXoopsTplAssign("permEdit", '$permEdit'); |
||
130 | } |
||
131 | $ret .= $this->xc->getXcXoopsTplAssign("showItem", "\${$ccFieldId} > 0"); |
||
132 | $ret .= $this->pc->getPhpCodeBlankLine(); |
||
133 | |||
134 | return $ret; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @private function getUserPagesList |
||
139 | * @param $moduleDirname |
||
140 | * @param $tableName |
||
141 | * @param $fieldId |
||
142 | * @param $fieldMain |
||
143 | * @param $tableRate |
||
144 | * @param string $t |
||
145 | * @return string |
||
146 | */ |
||
147 | private function getUserPagesList($moduleDirname, $tableName, $fieldId, $fieldMain, $tableRate, $fieldReads, $t = '') |
||
148 | { |
||
149 | $ucfTableName = \ucfirst($tableName); |
||
150 | $stuTableName = \mb_strtoupper($tableName); |
||
151 | $ccFieldId = $this->getCamelCase($fieldId, false, true); |
||
152 | $ucfFieldId = \ucfirst($ccFieldId); |
||
|
|||
153 | $ccFieldMain = $this->getCamelCase($fieldMain, false, true); |
||
154 | $ccFieldReads = $this->getCamelCase($fieldReads, false, true); |
||
155 | $stuModuleDirname = \mb_strtoupper($moduleDirname); |
||
156 | |||
157 | $ret = ''; |
||
158 | if ($tableRate) { |
||
159 | $varRate = '$ratingbars'; |
||
160 | $ret .= $this->xc->getXcEqualsOperator($varRate, '(int)' . $this->xc->getXcGetConfig('ratingbars'),'', $t); |
||
161 | $contIf = $this->xc->getXcXoThemeAddStylesheet("{$stuModuleDirname}_URL . '/assets/css/rating.css'", $t . "\t", false); |
||
162 | $contIf .= $this->xc->getXcXoopsTplAssign('rating', $varRate, true, $t . "\t"); |
||
163 | $contIf .= $this->xc->getXcXoopsTplAssign('rating_5stars', "(Constants::RATING_5STARS === {$varRate})", true, $t . "\t"); |
||
164 | $contIf .= $this->xc->getXcXoopsTplAssign('rating_10stars', "(Constants::RATING_10STARS === {$varRate})", true, $t . "\t"); |
||
165 | $contIf .= $this->xc->getXcXoopsTplAssign('rating_10num', "(Constants::RATING_10NUM === {$varRate})", true, $t . "\t"); |
||
166 | $contIf .= $this->xc->getXcXoopsTplAssign('rating_likes', "(Constants::RATING_LIKES === {$varRate})", true, $t . "\t"); |
||
167 | $contIf .= $this->xc->getXcXoopsTplAssign('itemid', "'{$fieldId}'", true, $t . "\t"); |
||
168 | $contIf .= $this->xc->getXcXoopsTplAssign($moduleDirname . '_icon_url_16', "{$stuModuleDirname}_URL . '/' . \$modPathIcon16", true, $t . "\t"); |
||
169 | $ret .= $this->pc->getPhpCodeConditions($varRate, ' > ', '0', $contIf, false, $t); |
||
170 | } |
||
171 | $critName = 'cr' . $ucfTableName; |
||
172 | $ret .= $this->xc->getXcCriteriaCompo($critName, $t); |
||
173 | $crit = $this->xc->getXcCriteria('', "'{$fieldId}'", "\${$ccFieldId}",'',true); |
||
174 | $contIf = $this->xc->getXcCriteriaAdd($critName, $crit, $t . "\t"); |
||
175 | $ret .= $this->pc->getPhpCodeConditions("\${$ccFieldId}", ' > ', '0', $contIf, false, $t); |
||
176 | $ret .= $this->xc->getXcHandlerCountClear($tableName . 'Count', $tableName, '$' . $critName, $t); |
||
177 | $ret .= $this->xc->getXcXoopsTplAssign($tableName . 'Count', "\${$tableName}Count", '', $t); |
||
178 | $ret .= $this->xc->getXcCriteriaSetStart($critName, '$start', $t); |
||
179 | $ret .= $this->xc->getXcCriteriaSetLimit($critName, '$limit', $t); |
||
180 | $ret .= $this->xc->getXcHandlerAllClear($tableName . 'All', $tableName, '$' . $critName, $t); |
||
181 | $condIf = $this->pc->getPhpCodeArray($tableName, null, false, $t . "\t"); |
||
182 | $condIf .= $this->xc->getXcEqualsOperator("\${$ccFieldMain}", "''",'', $t . "\t"); |
||
183 | $condIf .= $this->pc->getPhpCodeCommentLine('Get All', $ucfTableName, $t . "\t"); |
||
184 | $foreach = $this->xc->getXcGetValues($tableName, $tableName . '[$i]', 'i', false, $t . "\t\t"); |
||
185 | $foreach .= $this->xc->getXcGetVar($ccFieldMain, "{$tableName}All[\$i]", $fieldMain, false, $t . "\t\t"); |
||
186 | $foreach .= $this->xc->getXcEqualsOperator('$keywords[$i]', "\${$ccFieldMain}",'', $t . "\t\t"); |
||
187 | if ($tableRate) { |
||
188 | $itemId = $this->xc->getXcGetVar($ccFieldId, "{$tableName}All[\$i]", $fieldId, true); |
||
189 | $const = $this->xc->getXcGetConstants('TABLE_' . $stuTableName); |
||
190 | $foreach .= $this->xc->getXcEqualsOperator("\${$tableName}[\$i]['rating']", "\$ratingsHandler->getItemRating({$itemId}, {$const})",'', $t . "\t\t"); |
||
191 | } |
||
192 | $condIf .= $this->pc->getPhpCodeForeach("{$tableName}All", true, false, 'i', $foreach, $t . "\t"); |
||
193 | $condIf .= $this->xc->getXcXoopsTplAssign($tableName, "\${$tableName}", true, $t . "\t"); |
||
194 | $condIf .= $this->pc->getPhpCodeUnset($tableName, $t . "\t"); |
||
195 | $condIf .= $this->xc->getXcPageNav($tableName, $t . "\t"); |
||
196 | $config = $this->xc->getXcGetConfig('table_type'); |
||
197 | $condIf .= $this->xc->getXcXoopsTplAssign('table_type', $config, true, $t . "\t"); |
||
198 | $config = $this->xc->getXcGetConfig('panel_type'); |
||
199 | $condIf .= $this->xc->getXcXoopsTplAssign('panel_type', $config, true, $t . "\t"); |
||
200 | $divideby = $this->xc->getXcGetConfig('divideby'); |
||
201 | $condIf .= $this->xc->getXcXoopsTplAssign('divideby', $divideby, true, $t . "\t"); |
||
202 | $numbCol = $this->xc->getXcGetConfig('numb_col'); |
||
203 | $condIf .= $this->xc->getXcXoopsTplAssign('numb_col', $numbCol, true, $t . "\t"); |
||
204 | $stripTags = $this->pc->getPhpCodeStripTags('', "\${$ccFieldMain} . ' - ' . " . "\$GLOBALS['xoopsModule']->getVar('name')", true); |
||
205 | $condIf2 = $this->xc->getXcXoopsTplAssign('xoops_pagetitle', $stripTags, true, $t . "\t\t"); |
||
206 | $condIf .= $this->pc->getPhpCodeConditions("'show' == \$op && '' != \${$ccFieldMain}", '', "", $condIf2, false, $t . "\t"); |
||
207 | |||
208 | if ('' !== $fieldReads) { |
||
209 | $condIf3 = $this->xc->getXcHandlerGetObj($tableName, $ccFieldId, $t . "\t\t"); |
||
210 | |||
211 | |||
212 | $getVar = $this->xc->getXcGetVar('', "{$tableName}Obj", $fieldReads, true); |
||
213 | $condIf3 .= $this->xc->getXcEqualsOperator("\${$ccFieldReads}", "(int)" . $getVar . ' + 1', false, $t . "\t\t"); |
||
214 | $condIf3 .= $this->xc->getXcSetVarObj($tableName, $fieldReads, "\${$ccFieldReads}", $t . "\t\t"); |
||
215 | $condIf3 .= $this->pc->getPhpCodeCommentLine('Insert Data', null, $t . "\t\t"); |
||
216 | $insert = $this->xc->getXcHandlerInsert($tableName, $tableName, 'Obj', 'Handler'); |
||
217 | $condIf3 .= $this->getSimpleString($insert .';',$t . "\t\t"); |
||
218 | //$contentInsert = $this->xc->getXcRedirectHeader("'{$tableName}.php?op=list&{$fieldId}=' . \${$ccFieldId}", '', '5', "\${$tableName}Obj->getHtmlErrors()", false, $t . "\t\t\t"); |
||
219 | //$condIf3 .= $this->pc->getPhpCodeConditions('!' . $insert, '', '', $contentInsert, false, $t . "\t\t"); |
||
220 | $condIf .= $this->pc->getPhpCodeConditions("'show' == \$op", '', "", $condIf3, false, $t . "\t"); |
||
221 | |||
222 | } |
||
223 | |||
224 | |||
225 | $ret .= $this->pc->getPhpCodeConditions("\${$tableName}Count", ' > ', '0', $condIf, false, $t); |
||
226 | |||
227 | return $ret; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @public function getUserSubmitSave |
||
232 | * @param string $moduleDirname |
||
233 | * @param $fields |
||
234 | * @param string $tableName |
||
235 | * @param $tableSoleName |
||
236 | * @param $tablePermissions |
||
237 | * @param $tableNotifications |
||
238 | * @param $language |
||
239 | * @param string $t |
||
240 | * @return string |
||
241 | */ |
||
242 | public function getUserPagesSave($moduleDirname, $fields, $tableName, $tableSoleName, $tablePermissions, $tableNotifications, $language, $t = '') |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * @private function getPermissionsSave |
||
357 | * @param $moduleDirname |
||
358 | * @param $ucfFieldId |
||
359 | * @param string $perm |
||
360 | * |
||
361 | * @return string |
||
362 | */ |
||
363 | private function getPermissionsSave($moduleDirname, $ucfFieldId, $perm = 'view') |
||
364 | { |
||
365 | $ret = $this->pc->getPhpCodeCommentLine('Permission to', $perm, "\t\t\t"); |
||
366 | $ret .= $this->xc->getXcDeleteRight('grouppermHandler', "{$moduleDirname}_{$perm}", '$mid', "\$new{$ucfFieldId}", false, "\t\t\t"); |
||
367 | $content = $this->xc->getXcAddRight('grouppermHandler', "{$moduleDirname}_{$perm}", "\$new{$ucfFieldId}", '$onegroupId', '$mid', false, "\t\t\t\t\t"); |
||
368 | $foreach = $this->pc->getPhpCodeForeach("_POST['groups_{$perm}']", false, false, 'onegroupId', $content, "\t\t\t\t"); |
||
369 | $ret .= $this->pc->getPhpCodeConditions("isset(\$_POST['groups_{$perm}'])", null, null, $foreach, false, "\t\t\t"); |
||
370 | |||
371 | return $ret; |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * @public function getUserPagesNew |
||
376 | * @param $tableName |
||
377 | * @param string $t |
||
378 | * @return string |
||
379 | */ |
||
380 | public function getUserPagesNew($tableName, $t = '') |
||
381 | { |
||
382 | $ret = $this->pc->getPhpCodeCommentLine('Check permissions', '', $t); |
||
383 | $contIf = $this->xc->getXcRedirectHeader($tableName, '?op=list', 3, '_NOPERM', true, $t . "\t"); |
||
384 | $ret .= $this->pc->getPhpCodeConditions('!$permissionsHandler->getPermGlobalSubmit()', '', '', $contIf, false, $t); |
||
385 | $ret .= $this->xc->getXcCommonPagesNew($tableName, $t); |
||
386 | |||
387 | return $ret; |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * @public function getUserPagesEdit |
||
392 | * @param $tableName |
||
393 | * @param $fieldId |
||
394 | * @param $language |
||
395 | * @param string $t |
||
396 | * @return string |
||
397 | */ |
||
398 | public function getUserPagesEdit($tableName, $fieldId, $language, $t = '') |
||
399 | { |
||
400 | $ccFieldId = $this->getCamelCase($fieldId, false, true); |
||
401 | $ret = $this->pc->getPhpCodeCommentLine('Check permissions', '', $t); |
||
402 | $contIf = $this->xc->getXcRedirectHeader($tableName, '?op=list', 3, '_NOPERM', true, $t . "\t"); |
||
403 | $ret .= $this->pc->getPhpCodeConditions('!$permissionsHandler->getPermGlobalSubmit()', '', '', $contIf, false, $t); |
||
404 | $ret .= $this->pc->getPhpCodeCommentLine('Check params', '', $t); |
||
405 | $contIf = $this->xc->getXcRedirectHeader($tableName, '?op=list', 3, "{$language}INVALID_PARAM", true, $t . "\t"); |
||
406 | $ret .= $this->pc->getPhpCodeConditions("\${$ccFieldId}", ' == ', '0', $contIf, false, $t); |
||
407 | $ret .= $this->xc->getXcCommonPagesEdit($tableName, $ccFieldId, $t); |
||
408 | |||
409 | return $ret; |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * @private function getUserPagesDelete |
||
414 | * @param $tableName |
||
415 | * @param $tableSoleName |
||
416 | * @param $language |
||
417 | * @param $fieldId |
||
418 | * @param $fieldMain |
||
419 | * @param $tableNotifications |
||
420 | * @param string $t |
||
421 | * @return string |
||
422 | */ |
||
423 | private function getUserPagesDelete($tableName, $tableSoleName, $language, $fieldId, $fieldMain, $tableNotifications, $t = '') |
||
435 | } |
||
436 | |||
437 | /** |
||
438 | * @private function getUserPagesBroken |
||
439 | * @param $language |
||
440 | * @param $moduleDirname |
||
441 | * @param $tableName |
||
442 | * @param $tableSoleName |
||
443 | * @param $fieldId |
||
444 | * @param $fieldStatus |
||
445 | * @param $fieldMain |
||
446 | * @param $tableNotifications |
||
447 | * @param string $t |
||
448 | * @return string |
||
449 | */ |
||
450 | private function getUserPagesBroken($language, $moduleDirname, $tableName, $tableSoleName, $fieldId, $fieldStatus, $fieldMain, $tableNotifications, $t = '') |
||
491 | } |
||
492 | |||
493 | /** |
||
494 | * @private function getUserPagesFooter |
||
495 | * @param $moduleDirname |
||
496 | * @param $tableName |
||
497 | * @param $tableComments |
||
498 | * @param $language |
||
499 | * |
||
500 | * @return string |
||
501 | */ |
||
502 | private function getUserPagesFooter($moduleDirname, $tableName, $tableComments, $language) |
||
528 | } |
||
529 | |||
530 | /** |
||
531 | * @private function getUserPagesSwitch |
||
532 | * @param $moduleDirname |
||
533 | * @param $tableId |
||
534 | * @param $tableMid |
||
535 | * @param $tableName |
||
536 | * @param $tableSoleName |
||
537 | * @param $tableSubmit |
||
538 | * @param $tablePermissions |
||
539 | * @param $tableBroken |
||
540 | * @param $fieldId |
||
541 | * @param $fieldMain |
||
542 | * @param $fieldStatus |
||
543 | * @param $tableNotifications |
||
544 | * @param $tableRate |
||
545 | * @param $fieldReads |
||
546 | * @param $language |
||
547 | * @param $t |
||
548 | * @return string |
||
549 | */ |
||
550 | private function getUserPagesSwitch($moduleDirname, $tableId, $tableMid, $tableName, $tableSoleName, $tableSubmit, $tablePermissions, $tableBroken, $fieldId, $fieldMain, $fieldStatus, $tableNotifications, $tableRate, $fieldReads, $language, $t) |
||
566 | } |
||
567 | |||
568 | /** |
||
569 | * @public function render |
||
570 | * @param null |
||
571 | * @return bool|string |
||
572 | */ |
||
573 | public function render() |
||
623 |