| Conditions | 13 |
| Paths | 2 |
| Total Lines | 102 |
| Code Lines | 86 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 83 | public function getSearchFunction($moduleDirname) |
||
| 84 | { |
||
| 85 | $pc = Tdmcreate\Files\CreatePhpCode::getInstance(); |
||
| 86 | $xc = Tdmcreate\Files\CreateXoopsCode::getInstance(); |
||
| 87 | $ucfModuleDirname = ucfirst($moduleDirname); |
||
| 88 | $tables = $this->getTables(); |
||
| 89 | $t = "\t"; |
||
| 90 | $ret = $pc->getPhpCodeCommentMultiLine(['search callback functions' => '', '' => '', '@param $queryarray' => '', '@param $andor' => '', '@param $limit' => '', '@param $offset' => '', '@param $userid' => '', '@return' => 'mixed $itemIds']); |
||
| 91 | $func = $xc->getXcEqualsOperator('$ret', "[]", '', $t); |
||
| 92 | $func .= $xc->getXcGetInstance('helper', "\XoopsModules\\{$ucfModuleDirname}\Helper", $t); |
||
| 93 | |||
| 94 | if (is_array($tables)) { |
||
| 95 | foreach (array_keys($tables) as $i) { |
||
| 96 | if(1 === (int) $tables[$i]->getVar('table_search')) { |
||
| 97 | $tableId = $tables[$i]->getVar('table_id'); |
||
| 98 | $tableMid = $tables[$i]->getVar('table_mid'); |
||
| 99 | $tableName = $tables[$i]->getVar('table_name'); |
||
| 100 | $tableFieldname = $tables[$i]->getVar('table_fieldname'); |
||
| 101 | $func .= $pc->getPhpCodeCommentLine('search in table', $tableName, $t); |
||
| 102 | $func .= $pc->getPhpCodeCommentLine('search keywords', '', $t); |
||
| 103 | $func .= $xc->getXcEqualsOperator('$elementCount', '0', '', $t); |
||
| 104 | $func .= $xc->getXcHandlerLine($tableName, $t); |
||
| 105 | $contIf = $xc->getXcEqualsOperator('$elementCount', 'count($queryarray)', '', $t . "\t"); |
||
| 106 | $func .= $pc->getPhpCodeConditions('is_array($queryarray)', '', '', $contIf, false, $t); |
||
| 107 | $contIf = $xc->getXcCriteriaCompo('crKeywords', $t . "\t"); |
||
| 108 | $for = $xc->getXcCriteriaCompo('crKeyword', $t . "\t\t"); |
||
| 109 | |||
| 110 | $fields = $this->getTableFields($tableMid, $tableId); |
||
| 111 | $fieldId = ''; |
||
| 112 | $fieldMain = ''; |
||
| 113 | $fieldDate = ''; |
||
| 114 | $countField = 0; |
||
| 115 | foreach (array_keys($fields) as $f) { |
||
| 116 | $fieldName = $fields[$f]->getVar('field_name'); |
||
| 117 | if (0 == $f) { |
||
| 118 | $fieldId = $fieldName; |
||
| 119 | } |
||
| 120 | if (1 === (int)$fields[$f]->getVar('field_main')) { |
||
| 121 | $fieldMain = $fieldName; |
||
| 122 | } |
||
| 123 | if (15 === (int)$fields[$f]->getVar('field_element') || 21 === (int)$fields[$f]->getVar('field_element')) { |
||
| 124 | $fieldDate = $fieldName; |
||
| 125 | } |
||
| 126 | if (1 === (int)$fields[$f]->getVar('field_search')) { |
||
| 127 | $crit = $xc->getXcCriteria('', "'{$fieldName}'", "'%' . \$queryarray[\$i] . '%'", "'LIKE'", true, $t . "\t"); |
||
| 128 | $for .= $xc->getXcCriteriaAdd('crKeyword', $crit, $t . "\t\t", "\n", "'OR'"); |
||
| 129 | $countField++; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | if ($countField > 0) { |
||
| 133 | $for .= $xc->getXcCriteriaAdd('crKeywords', '$crKeyword', $t . "\t\t", "\n", '$andor'); |
||
| 134 | } |
||
| 135 | $for .= $pc->getPhpCodeUnset('crKeyword', $t . "\t\t"); |
||
| 136 | $contIf .= $pc->getPhpCodeFor( 'i', $for, 'elementCount', '0', ' < ', $t . "\t"); |
||
| 137 | $func .= $pc->getPhpCodeConditions('$elementCount', ' > ', '0', $contIf, false, $t); |
||
| 138 | $func .= $pc->getPhpCodeCommentLine('search user(s)', '', $t); |
||
| 139 | $contIf = $xc->getXcEqualsOperator('$userid', "array_map('intval', \$userid)", '', $t . "\t"); |
||
| 140 | $contIf .= $xc->getXcCriteriaCompo('crUser', $t . "\t"); |
||
| 141 | $crit = $xc->getXcCriteria('', "'{$tableFieldname}_submitter'", "'(' . implode(',', \$userid) . ')'", "'IN'", true, $t . "\t"); |
||
| 142 | $contIf .= $xc->getXcCriteriaAdd('crUser', $crit, $t . "\t", "\n", "'OR'"); |
||
| 143 | $contElse = $xc->getXcCriteriaCompo('crUser', $t . "\t"); |
||
| 144 | $crit = $xc->getXcCriteria('', "'{$tableFieldname}_submitter'", '$userid', '', true, $t . "\t"); |
||
| 145 | $contElse .= $xc->getXcCriteriaAdd('crUser', $crit, $t . "\t", "\n", "'OR'"); |
||
| 146 | $func .= $pc->getPhpCodeConditions('$userid && is_array($userid)', '', '', $contIf, $contElse, $t, 'is_numeric($userid) && $userid > 0'); |
||
| 147 | $func .= $xc->getXcCriteriaCompo('crSearch', $t); |
||
| 148 | $contIf = $xc->getXcCriteriaAdd('crSearch', '$crKeywords', $t . "\t", "\n", "'AND'"); |
||
| 149 | $cond = $pc->getPhpCodeIsset('crKeywords'); |
||
| 150 | $func .= $pc->getPhpCodeConditions($cond, '', '', $contIf, false, $t); |
||
| 151 | $contIf = $xc->getXcCriteriaAdd('crSearch', '$crUser', $t . "\t", "\n", "'AND'"); |
||
| 152 | $cond = $pc->getPhpCodeIsset('crUser'); |
||
| 153 | $func .= $pc->getPhpCodeConditions($cond, '', '', $contIf, false, $t); |
||
| 154 | $func .= $xc->getXcCriteriaSetStart( 'crSearch', '$offset', $t); |
||
| 155 | $func .= $xc->getXcCriteriaSetLimit( 'crSearch', '$limit', $t); |
||
| 156 | if ('' !== $fieldDate) { |
||
| 157 | $func .= $xc->getXcCriteriaSetSort( 'crSearch', "'{$fieldDate}'", $t); |
||
| 158 | } else { |
||
| 159 | $func .= $xc->getXcCriteriaSetSort( 'crSearch', "'{$fieldId}_date'", $t); |
||
| 160 | } |
||
| 161 | $func .= $xc->getXcCriteriaSetOrder( 'crSearch', "'DESC'", $t); |
||
| 162 | $func .= $xc->getXcHandlerAllClear($tableName . 'All', $tableName, '$crSearch', $t); |
||
| 163 | $contentForeach = $t . "\t\$ret[] = [\n"; |
||
| 164 | $contentForeach .= $t . "\t\t'image' => 'assets/icons/16/{$tableName}.png',\n"; |
||
| 165 | $contentForeach .= $t . "\t\t'link' => '{$tableName}.php?op=show&{$fieldId}=' . \${$tableName}All[\$i]->getVar('{$fieldId}'),\n"; |
||
| 166 | $contentForeach .= $t . "\t\t'title' => \${$tableName}All[\$i]->getVar('{$fieldMain}'),\n"; |
||
| 167 | if ('' !== $fieldDate) { |
||
| 168 | $contentForeach .= $t . "\t\t'time' => \${$tableName}All[\$i]->getVar('{$fieldDate}')\n"; |
||
| 169 | } |
||
| 170 | $contentForeach .= $t . "\t];\n"; |
||
| 171 | $func .= $pc->getPhpCodeForeach("{$tableName}All", true, false, 'i', $contentForeach, "\t"); |
||
| 172 | $func .= $pc->getPhpCodeUnset('crKeywords', $t); |
||
| 173 | $func .= $pc->getPhpCodeUnset('crKeyword', $t); |
||
| 174 | $func .= $pc->getPhpCodeUnset('crUser', $t); |
||
| 175 | $func .= $pc->getPhpCodeUnset('crSearch', $t); |
||
| 176 | } |
||
| 177 | $func .= $pc->getPhpCodeBlankLine(); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | $func .= $this->getSimpleString('return $ret;', $t); |
||
| 181 | $func .= $pc->getPhpCodeBlankLine(); |
||
| 182 | $ret .= $pc->getPhpCodeFunction("{$moduleDirname}_search", '$queryarray, $andor, $limit, $offset, $userid', $func); |
||
| 183 | |||
| 184 | return $ret; |
||
| 185 | } |
||
| 207 |