| Conditions | 25 |
| Paths | 831 |
| Total Lines | 129 |
| Code Lines | 74 |
| 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 |
||
| 112 | public function expandParameters(array $paramArray, int $pid): array |
||
| 113 | { |
||
| 114 | // Traverse parameter names: |
||
| 115 | foreach ($paramArray as $p => $v) { |
||
| 116 | $v = trim($v); |
||
| 117 | |||
| 118 | // If value is encapsulated in square brackets it means there are some ranges of values to find, otherwise the value is literal |
||
| 119 | if (strpos($v, '[') === 0 && substr($v, -1) === ']') { |
||
| 120 | // So, find the value inside brackets and reset the paramArray value as an array. |
||
| 121 | $v = substr($v, 1, -1); |
||
| 122 | $paramArray[$p] = []; |
||
| 123 | |||
| 124 | // Explode parts and traverse them: |
||
| 125 | $parts = explode('|', $v); |
||
| 126 | foreach ($parts as $pV) { |
||
| 127 | |||
| 128 | // Look for integer range: (fx. 1-34 or -40--30 // reads minus 40 to minus 30) |
||
| 129 | if (preg_match('/^(-?[0-9]+)\s*-\s*(-?[0-9]+)$/', trim($pV), $reg)) { |
||
| 130 | $reg = $this->swapIfFirstIsLargerThanSecond($reg); |
||
| 131 | |||
| 132 | // Traverse range, add values: |
||
| 133 | // Limit to size of range! |
||
| 134 | $runAwayBrake = 1000; |
||
| 135 | for ($a = $reg[1]; $a <= $reg[2]; $a++) { |
||
| 136 | $paramArray[$p][] = $a; |
||
| 137 | $runAwayBrake--; |
||
| 138 | if ($runAwayBrake <= 0) { |
||
| 139 | break; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | } elseif (strpos(trim($pV), '_TABLE:') === 0) { |
||
| 143 | |||
| 144 | // Parse parameters: |
||
| 145 | $subparts = GeneralUtility::trimExplode(';', $pV); |
||
| 146 | $subpartParams = []; |
||
| 147 | foreach ($subparts as $spV) { |
||
| 148 | [$pKey, $pVal] = GeneralUtility::trimExplode(':', $spV); |
||
| 149 | $subpartParams[$pKey] = $pVal; |
||
| 150 | } |
||
| 151 | |||
| 152 | // Table exists: |
||
| 153 | if (isset($GLOBALS['TCA'][$subpartParams['_TABLE']])) { |
||
| 154 | $lookUpPid = isset($subpartParams['_PID']) ? intval($subpartParams['_PID']) : intval($pid); |
||
| 155 | $recursiveDepth = isset($subpartParams['_RECURSIVE']) ? intval($subpartParams['_RECURSIVE']) : 0; |
||
| 156 | $pidField = isset($subpartParams['_PIDFIELD']) ? trim($subpartParams['_PIDFIELD']) : 'pid'; |
||
| 157 | $where = $subpartParams['_WHERE'] ?? ''; |
||
| 158 | $addTable = $subpartParams['_ADDTABLE'] ?? ''; |
||
| 159 | |||
| 160 | $fieldName = $subpartParams['_FIELD'] ? $subpartParams['_FIELD'] : 'uid'; |
||
| 161 | if ($fieldName === 'uid' || $GLOBALS['TCA'][$subpartParams['_TABLE']]['columns'][$fieldName]) { |
||
| 162 | $queryBuilder = $this->getQueryBuilder($subpartParams['_TABLE']); |
||
| 163 | |||
| 164 | if ($recursiveDepth > 0) { |
||
| 165 | /** @var QueryGenerator $queryGenerator */ |
||
| 166 | $queryGenerator = GeneralUtility::makeInstance(QueryGenerator::class); |
||
| 167 | $pidList = $queryGenerator->getTreeList($lookUpPid, $recursiveDepth, 0, 1); |
||
| 168 | $pidArray = GeneralUtility::intExplode(',', $pidList); |
||
| 169 | } else { |
||
| 170 | $pidArray = [(string) $lookUpPid]; |
||
| 171 | } |
||
| 172 | |||
| 173 | $queryBuilder->getRestrictions() |
||
| 174 | ->removeAll() |
||
| 175 | ->add(GeneralUtility::makeInstance(DeletedRestriction::class)); |
||
| 176 | |||
| 177 | $queryBuilder |
||
| 178 | ->select($fieldName) |
||
| 179 | ->from($subpartParams['_TABLE']) |
||
| 180 | ->where( |
||
| 181 | $queryBuilder->expr()->in($pidField, $queryBuilder->createNamedParameter($pidArray, Connection::PARAM_INT_ARRAY)), |
||
| 182 | $where |
||
| 183 | ); |
||
| 184 | |||
| 185 | if (! empty($addTable)) { |
||
| 186 | // TODO: Check if this works as intended! |
||
| 187 | $queryBuilder->add('from', $addTable); |
||
| 188 | } |
||
| 189 | $transOrigPointerField = $GLOBALS['TCA'][$subpartParams['_TABLE']]['ctrl']['transOrigPointerField']; |
||
| 190 | |||
| 191 | if ($subpartParams['_ENABLELANG'] && $transOrigPointerField) { |
||
| 192 | $queryBuilder->andWhere( |
||
| 193 | $queryBuilder->expr()->lte( |
||
| 194 | $transOrigPointerField, |
||
| 195 | 0 |
||
| 196 | ) |
||
| 197 | ); |
||
| 198 | } |
||
| 199 | |||
| 200 | $statement = $queryBuilder->execute(); |
||
| 201 | |||
| 202 | $rows = []; |
||
| 203 | while ($row = $statement->fetch()) { |
||
| 204 | $rows[$row[$fieldName]] = $row; |
||
| 205 | } |
||
| 206 | |||
| 207 | if (is_array($rows)) { |
||
| 208 | $paramArray[$p] = array_merge($paramArray[$p], array_keys($rows)); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | } |
||
| 212 | } else { |
||
| 213 | // Just add value: |
||
| 214 | $paramArray[$p][] = $pV; |
||
| 215 | } |
||
| 216 | // Hook for processing own expandParameters place holder |
||
| 217 | if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['crawler/class.tx_crawler_lib.php']['expandParameters'])) { |
||
| 218 | $_params = [ |
||
| 219 | 'pObj' => &$this, |
||
| 220 | 'paramArray' => &$paramArray, |
||
| 221 | 'currentKey' => $p, |
||
| 222 | 'currentValue' => $pV, |
||
| 223 | 'pid' => $pid, |
||
| 224 | ]; |
||
| 225 | foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['crawler/class.tx_crawler_lib.php']['expandParameters'] as $_funcRef) { |
||
| 226 | GeneralUtility::callUserFunction($_funcRef, $_params, $this); |
||
| 227 | } |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | // Make unique set of values and sort array by key: |
||
| 232 | $paramArray[$p] = array_unique($paramArray[$p]); |
||
| 233 | ksort($paramArray); |
||
| 234 | } else { |
||
| 235 | // Set the literal value as only value in array: |
||
| 236 | $paramArray[$p] = [$v]; |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | return $paramArray; |
||
| 241 | } |
||
| 255 |