| Conditions | 17 |
| Paths | 22 |
| Total Lines | 85 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 1 | Features | 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 |
||
| 170 | protected function fetchRelData($item, string $fieldName): string |
||
| 171 | { |
||
| 172 | $fieldNameArray = explode('.', $fieldName); |
||
| 173 | $methodName = array_shift($fieldNameArray); |
||
| 174 | $foreignField = $fieldNameArray[0]; |
||
| 175 | $relType = $this->getRelationshipType($methodName); |
||
| 176 | $className = $this->getRelClassName($methodName); |
||
| 177 | $classNameForArray = $this->classToSafeClass($className); |
||
| 178 | // die($methodName . '.' . $foreignField . '.' . $relType . '.' . $className); |
||
| 179 | $limit = Config::inst()->get(static::class, 'limit_to_lookups'); |
||
| 180 | if (!isset($this->lookupTableCache[$classNameForArray])) { |
||
| 181 | $this->lookupTableCache[$classNameForArray] = $className::get()->limit($limit)->map('ID', $foreignField)->toArray(); |
||
| 182 | } |
||
| 183 | if ($relType === 'has_one') { |
||
| 184 | // Check if data is already cached |
||
| 185 | $fieldName = $methodName . 'ID'; |
||
| 186 | $id = (int) $item->$fieldName; |
||
| 187 | if ($id === 0) { |
||
| 188 | return ''; |
||
| 189 | } |
||
| 190 | return (string) ($this->lookupTableCache[$classNameForArray][$id] ?? 'error' . $className::get()->byID($id)?->$foreignField); |
||
| 191 | } else { |
||
| 192 | $relName = $this->classToSafeClass($item->ClassName) . '_' . $fieldName; |
||
| 193 | $result = []; |
||
| 194 | // slow.... |
||
| 195 | if ($relType === 'has_many') { |
||
| 196 | foreach ($item->$methodName()->column($foreignField) as $val) { |
||
| 197 | $result[] = $val; |
||
| 198 | } |
||
| 199 | if (!isset($this->joinTableCache[$relName])) { |
||
| 200 | // relation object details |
||
| 201 | $rel = $item->$methodName(); |
||
| 202 | $this->joinTableCache[$relName] = [ |
||
| 203 | 'foreign' => $rel->getForeignKey(),// e.g. MyExportRecordID |
||
| 204 | ]; |
||
| 205 | // NB!!!!!!!!!!!!!! |
||
| 206 | // local and foreign are swapped here on purpose |
||
| 207 | $fieldRelatingToModelExported = $this->joinTableCache[$relName]['foreign']; |
||
| 208 | |||
| 209 | $list = $className::get()->limit($limit)->map('ID', $fieldRelatingToModelExported)->toArray(); |
||
| 210 | foreach ($list as $idOfRelatedItem => $idOfModelExported) { |
||
| 211 | if (! isset($this->lookupTableCache[$relName][$idOfModelExported])) { |
||
| 212 | $this->lookupTableCache[$relName][$idOfModelExported] = []; |
||
| 213 | } |
||
| 214 | $this->lookupTableCache[$relName][$idOfModelExported][] = $idOfRelatedItem; |
||
| 215 | } |
||
| 216 | } |
||
| 217 | if (! empty($this->lookupTableCache[$relName][$item->ID])) { |
||
| 218 | foreach ($this->lookupTableCache[$relName][$item->ID] as $fieldRelatingToLookupRelation) { |
||
| 219 | $result[] = $this->lookupTableCache[$classNameForArray][$fieldRelatingToLookupRelation] ?? ''; |
||
| 220 | } |
||
| 221 | } |
||
| 222 | } elseif ($relType === 'many_many') { |
||
| 223 | if (!isset($this->joinTableCache[$relName])) { |
||
| 224 | // relation object details |
||
| 225 | $rel = $item->$methodName(); |
||
| 226 | $this->joinTableCache[$relName] = [ |
||
| 227 | 'table' => $rel->getJoinTable(), //e.g. MyExportRecord_MyManyManyRelationshipNam |
||
| 228 | 'local' => $rel->getLocalKey(), // e.g. MyRelationID |
||
| 229 | 'foreign' => $rel->getForeignKey(), // e.g. MyExportRecordID |
||
| 230 | ]; |
||
| 231 | $joinTable = $this->joinTableCache[$relName]['table']; |
||
| 232 | // NB!!!!!!!!!!!!!! |
||
| 233 | // local and foreign are swapped here on purpose |
||
| 234 | $fieldRelatingToModelExported = $this->joinTableCache[$relName]['foreign']; // e.g. MyExportRecordID |
||
| 235 | $fieldRelatingToLookupRelation = $this->joinTableCache[$relName]['local'];// e.g. MyRelationID |
||
| 236 | |||
| 237 | $limit = Config::inst()->get(static::class, 'limit_to_join_tables'); |
||
| 238 | $list = DB::query('SELECT "' . $fieldRelatingToModelExported . '", "' . $fieldRelatingToLookupRelation . '" FROM "' . $joinTable . '" LIMIT ' . $limit); |
||
| 239 | foreach ($list as $row) { |
||
| 240 | if (! isset($this->lookupTableCache[$joinTable][$row[$fieldRelatingToModelExported]])) { |
||
| 241 | $this->lookupTableCache[$joinTable][$row[$fieldRelatingToModelExported]] = []; |
||
| 242 | } |
||
| 243 | $this->lookupTableCache[$joinTable][$row[$fieldRelatingToModelExported]][] = $row[$fieldRelatingToLookupRelation]; |
||
| 244 | } |
||
| 245 | } else { |
||
| 246 | $joinTable = $this->joinTableCache[$relName]['table']; |
||
| 247 | } |
||
| 248 | if (! empty($this->lookupTableCache[$joinTable][$item->ID])) { |
||
| 249 | foreach ($this->lookupTableCache[$joinTable][$item->ID] as $fieldRelatingToLookupRelation) { |
||
| 250 | $result[] = $this->lookupTableCache[$classNameForArray][$fieldRelatingToLookupRelation] ?? ''; |
||
| 251 | } |
||
| 252 | } |
||
| 253 | } |
||
| 254 | return implode($this->exportSeparator, $result); |
||
| 255 | } |
||
| 316 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths