| Total Complexity | 47 |
| Total Lines | 289 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 0 |
Complex classes like ExportAllCustomButton 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 ExportAllCustomButton, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class ExportAllCustomButton extends GridFieldExportButton |
||
| 26 | { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Example: |
||
| 30 | * |
||
| 31 | * ```php |
||
| 32 | * Member::class => [ |
||
| 33 | * 'Name' => [ |
||
| 34 | * 'FirstName', |
||
| 35 | * 'Surname', |
||
| 36 | * 'MyHasOneSalutation.Title', |
||
| 37 | * ], |
||
| 38 | * 'Email' => 'Email', |
||
| 39 | * 'MyHasOneRelation' => 'MyHasOneRelation.Title', |
||
| 40 | * 'MyManyManyRelation' => 'MyManyManyRelation.Title', |
||
| 41 | * 'MyManyManyRelation Nice Title' => 'MyManyManyRelation2.Title', |
||
| 42 | * ], |
||
| 43 | * MyOtherClass => '*', |
||
| 44 | * ``` |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | private static array $custom_exports = []; |
||
| 48 | private static int $limit_to_lookups = 500; |
||
| 49 | private static int $limit_to_join_tables = 100000; |
||
| 50 | |||
| 51 | private static int $max_chars_per_cell = 200; |
||
| 52 | private static $db_defaults = [ |
||
| 53 | 'ID' => 'Int', |
||
| 54 | 'Created' => 'DBDatetime', |
||
| 55 | 'LastEdited' => 'DBDatetime', |
||
| 56 | ]; |
||
| 57 | |||
| 58 | protected bool $hasCustomExport = false; |
||
| 59 | protected array $dbCache = []; |
||
| 60 | protected array $relCache = []; |
||
| 61 | |||
| 62 | protected array $lookupTableCache = []; |
||
| 63 | |||
| 64 | protected array $joinTableCache = []; |
||
| 65 | protected string $exportSeparator = ' ||| '; |
||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * Generate export fields for CSV. |
||
| 70 | * |
||
| 71 | * @param GridField $gridField |
||
| 72 | * |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | public function generateExportFileData($gridField): string |
||
| 127 | } |
||
| 128 | |||
| 129 | |||
| 130 | protected function getDataRowForExport($item) |
||
| 131 | { |
||
| 132 | $array = []; |
||
| 133 | $maxCharsPerCell = Config::inst()->get(static::class, 'max_chars_per_cell'); |
||
| 134 | foreach ($this->exportColumns as $fieldOrFieldArray) { |
||
| 135 | $v = $this->getDataRowForExportInner($item, $fieldOrFieldArray); |
||
| 136 | $v = substr($v, 0, $maxCharsPerCell); |
||
| 137 | $array[] = $v; |
||
| 138 | } |
||
| 139 | return $array; |
||
| 140 | } |
||
| 141 | |||
| 142 | protected function getDataRowForExportInner($item, $fieldOrFieldArray): string |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | |||
| 170 | protected function fetchRelData($item, string $fieldName): string |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | protected function fieldTypes($fieldName) |
||
| 259 | { |
||
| 260 | |||
| 261 | if (count($this->dbCache) === 0) { |
||
| 262 | $this->dbCache = |
||
| 263 | Config::inst()->get(static::class, 'db_defaults') + |
||
| 264 | Config::inst()->get(Member::class, 'db'); |
||
| 265 | } |
||
| 266 | return $this->dbCache[$fieldName]; |
||
| 267 | } |
||
| 268 | |||
| 269 | protected function getRelationshipType($methodName) |
||
| 270 | { |
||
| 271 | return $this->relCache[$methodName]['type']; |
||
| 272 | } |
||
| 273 | |||
| 274 | protected function getRelClassName($methodName) |
||
| 275 | { |
||
| 276 | return $this->relCache[$methodName]['class']; |
||
| 277 | } |
||
| 278 | |||
| 279 | protected function buildRelCache() |
||
| 280 | { |
||
| 281 | if (count($this->relCache) === 0) { |
||
| 282 | |||
| 283 | foreach (['has_one', 'has_many', 'many_many'] as $relType) { |
||
| 284 | foreach (Config::inst()->get(Member::class, $relType) as $methodName => $className) { |
||
| 285 | $this->relCache[$methodName] = [ |
||
| 286 | 'type' => $relType, |
||
| 287 | 'class' => $className |
||
| 288 | ]; |
||
| 289 | } |
||
| 290 | } |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Return the columns to export |
||
| 296 | * |
||
| 297 | * @param GridField $gridField |
||
| 298 | * |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | protected function getExportColumnsForGridField(GridField $gridField) |
||
| 302 | { |
||
| 303 | $modelClass = $gridField->getModelClass(); |
||
| 304 | $custom = Config::inst()->get(static::class, 'custom_exports'); |
||
| 305 | if (isset($custom[$modelClass]) && $custom[$modelClass] === '*') { |
||
| 306 | $this->exportColumns = AllFields::create($modelClass)->getExportFields(); |
||
| 307 | } |
||
| 308 | return parent::getExportColumnsForGridField($gridField); |
||
| 309 | } |
||
| 310 | |||
| 311 | protected function classToSafeClass(string $class): string |
||
| 314 | } |
||
| 315 | } |
||
| 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