| Total Complexity | 52 |
| Total Lines | 303 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like TcaSiteLanguage 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 TcaSiteLanguage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class TcaSiteLanguage extends AbstractDatabaseRecordProvider implements FormDataProviderInterface |
||
| 37 | { |
||
| 38 | private const FOREIGN_TABLE = 'site_language'; |
||
| 39 | private const FOREIGN_FIELD = 'languageId'; |
||
| 40 | |||
| 41 | public function addData(array $result): array |
||
| 42 | { |
||
| 43 | foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfig) { |
||
| 44 | if (($fieldConfig['config']['type'] ?? '') !== 'siteLanguage') { |
||
| 45 | continue; |
||
| 46 | } |
||
| 47 | |||
| 48 | if (!($GLOBALS['TCA'][self::FOREIGN_TABLE] ?? false)) { |
||
| 49 | throw new \RuntimeException('Table ' . self::FOREIGN_TABLE . ' does not exists', 1624029932); |
||
| 50 | } |
||
| 51 | |||
| 52 | $childConfiguration = $GLOBALS['TCA'][self::FOREIGN_TABLE]['columns'][self::FOREIGN_FIELD]['config'] ?? []; |
||
| 53 | |||
| 54 | if (($childConfiguration['type'] ?? '') !== 'select') { |
||
| 55 | throw new \UnexpectedValueException( |
||
| 56 | 'Table ' . $result['tableName'] . ' field ' . $fieldName . ' points to field ' |
||
| 57 | . self::FOREIGN_FIELD . ' of table ' . self::FOREIGN_TABLE . ', but this field ' |
||
| 58 | . 'is either not defined or is not of type select', |
||
| 59 | 1624029933 |
||
| 60 | ); |
||
| 61 | } |
||
| 62 | |||
| 63 | if (!($childConfiguration['itemsProcFunc'] ?? false)) { |
||
| 64 | throw new \UnexpectedValueException( |
||
| 65 | 'Table ' . $result['tableName'] . ' field ' . $fieldName . ' points to field ' |
||
| 66 | . self::FOREIGN_FIELD . ' of table ' . self::FOREIGN_TABLE . '. This field must define ' |
||
| 67 | . 'an \'itemsProcFunc\'.', |
||
| 68 | 1624029934 |
||
| 69 | ); |
||
| 70 | } |
||
| 71 | |||
| 72 | $result = $this->addInlineRelatedConfig($result, $fieldName); |
||
| 73 | $result = $this->initializeMinMaxItems($result, $fieldName); |
||
| 74 | $result = $this->initializeAppearance($result, $fieldName); |
||
| 75 | $result = $this->addInlineFirstPid($result); |
||
| 76 | $result = $this->resolveSiteLanguageChildren($result, $fieldName); |
||
| 77 | $result = $this->addUniquePossibleRecords($result, $fieldName); |
||
| 78 | } |
||
| 79 | |||
| 80 | return $result; |
||
| 81 | } |
||
| 82 | |||
| 83 | protected function addInlineRelatedConfig(array $result, string $fieldName): array |
||
| 84 | { |
||
| 85 | $config = $result['processedTca']['columns'][$fieldName]['config']; |
||
| 86 | $config['foreign_table'] = self::FOREIGN_TABLE; |
||
| 87 | $config['foreign_selector'] = self::FOREIGN_FIELD; |
||
| 88 | $result['processedTca']['columns'][$fieldName]['config'] = $config; |
||
| 89 | |||
| 90 | return $result; |
||
| 91 | } |
||
| 92 | |||
| 93 | protected function initializeMinMaxItems(array $result, string $fieldName): array |
||
| 94 | { |
||
| 95 | $config = $result['processedTca']['columns'][$fieldName]['config']; |
||
| 96 | $config['minitems'] = isset($config['minitems']) ? MathUtility::forceIntegerInRange($config['minitems'], 1) : 1; |
||
| 97 | $config['maxitems'] = isset($config['maxitems']) ? MathUtility::forceIntegerInRange($config['maxitems'], 2) : 99999; |
||
| 98 | $result['processedTca']['columns'][$fieldName]['config'] = $config; |
||
| 99 | |||
| 100 | return $result; |
||
| 101 | } |
||
| 102 | |||
| 103 | protected function initializeAppearance(array $result, string $fieldName): array |
||
| 104 | { |
||
| 105 | $config = $result['processedTca']['columns'][$fieldName]['config']; |
||
| 106 | if (!is_array($config['appearance'] ?? false)) { |
||
| 107 | $config['appearance'] = []; |
||
| 108 | } |
||
| 109 | $config['appearance']['showPossibleLocalizationRecords'] = false; |
||
| 110 | $config['appearance']['showRemovedLocalizationRecords'] = false; |
||
| 111 | $config['appearance']['collapseAll'] = true; |
||
| 112 | $config['appearance']['expandSignle'] = false; |
||
| 113 | $config['appearance']['enabledControls'] = [ |
||
| 114 | 'info' => false, |
||
| 115 | 'new' => false, |
||
| 116 | 'dragdrop' => false, |
||
| 117 | 'sort' => false, |
||
| 118 | 'hide' => false, |
||
| 119 | 'delete' => true, |
||
| 120 | 'localize' => false, |
||
| 121 | ]; |
||
| 122 | |||
| 123 | $config['size'] = (int)($config['size'] ?? 4); |
||
| 124 | |||
| 125 | $result['processedTca']['columns'][$fieldName]['config'] = $config; |
||
| 126 | |||
| 127 | return $result; |
||
| 128 | } |
||
| 129 | |||
| 130 | protected function addInlineFirstPid(array $result): array |
||
| 131 | { |
||
| 132 | if (($result['inlineFirstPid'] ?? null) !== null || ($result['tableName'] ?? '') !== self::FOREIGN_TABLE) { |
||
| 133 | return $result; |
||
| 134 | } |
||
| 135 | |||
| 136 | $pid = $result['databaseRow']['pid'] ?? 0; |
||
| 137 | |||
| 138 | if (!MathUtility::canBeInterpretedAsInteger($pid) || strpos($pid, 'NEW') !== 0) { |
||
| 139 | throw new \RuntimeException( |
||
| 140 | 'inlineFirstPid should either be an integer or a "NEW..." string', |
||
| 141 | 1624310264 |
||
| 142 | ); |
||
| 143 | } |
||
| 144 | |||
| 145 | $result['inlineFirstPid'] = $pid; |
||
| 146 | |||
| 147 | return $result; |
||
| 148 | } |
||
| 149 | |||
| 150 | protected function resolveSiteLanguageChildren(array $result, string $fieldName): array |
||
| 204 | } |
||
| 205 | |||
| 206 | protected function compileDefaultSiteLanguageChild(array $result, string $parentFieldName): array |
||
| 233 | ]); |
||
| 234 | } |
||
| 235 | |||
| 236 | protected function compileChild(array $result, string $parentFieldName, int $childUid): array |
||
| 237 | { |
||
| 238 | $inlineStackProcessor = GeneralUtility::makeInstance(InlineStackProcessor::class); |
||
| 239 | $inlineStackProcessor->initializeByGivenStructure($result['inlineStructure']); |
||
| 240 | $inlineTopMostParent = $inlineStackProcessor->getStructureLevel(0); |
||
| 241 | |||
| 242 | return GeneralUtility::makeInstance( |
||
| 243 | FormDataCompiler::class, |
||
| 244 | GeneralUtility::makeInstance(SiteConfigurationDataGroup::class) |
||
| 245 | )->compile([ |
||
| 246 | 'command' => 'edit', |
||
| 247 | 'tableName' => self::FOREIGN_TABLE, |
||
| 248 | 'vanillaUid' => $childUid, |
||
| 249 | 'returnUrl' => $result['returnUrl'], |
||
| 250 | 'isInlineChild' => true, |
||
| 251 | 'inlineStructure' => $result['inlineStructure'], |
||
| 252 | 'inlineExpandCollapseStateArray' => $result['inlineExpandCollapseStateArray'], |
||
| 253 | 'inlineFirstPid' => $result['inlineFirstPid'], |
||
| 254 | 'inlineParentConfig' => $result['processedTca']['columns'][$parentFieldName]['config'], |
||
| 255 | 'inlineParentUid' => $result['databaseRow']['uid'], |
||
| 256 | 'inlineParentTableName' => $result['tableName'], |
||
| 257 | 'inlineParentFieldName' => $parentFieldName, |
||
| 258 | 'inlineTopMostParentUid' => $result['inlineTopMostParentUid'] ?: ($inlineTopMostParent['uid'] ?? null), |
||
| 259 | 'inlineTopMostParentTableName' => $result['inlineTopMostParentTableName'] ?: ($inlineTopMostParent['table'] ?? ''), |
||
| 260 | 'inlineTopMostParentFieldName' => $result['inlineTopMostParentFieldName'] ?: ($inlineTopMostParent['field'] ?? ''), |
||
| 261 | ]); |
||
| 262 | } |
||
| 263 | |||
| 264 | protected function addUniquePossibleRecords(array $result, string $fieldName): array |
||
| 265 | { |
||
| 266 | $formDataGroup = GeneralUtility::makeInstance(OnTheFly::class); |
||
| 267 | $formDataGroup->setProviderList([TcaSelectItems::class]); |
||
| 268 | |||
| 269 | // Add unique possible records, so they can be used in the selector field |
||
| 270 | $result['processedTca']['columns'][$fieldName]['config']['uniquePossibleRecords'] = GeneralUtility::makeInstance( |
||
| 271 | FormDataCompiler::class, |
||
| 272 | $formDataGroup |
||
| 273 | )->compile([ |
||
| 274 | 'command' => 'new', |
||
| 275 | 'tableName' => self::FOREIGN_TABLE, |
||
| 276 | 'pageTsConfig' => $result['pageTsConfig'], |
||
| 277 | 'userTsConfig' => $result['userTsConfig'], |
||
| 278 | 'databaseRow' => $result['databaseRow'], |
||
| 279 | 'processedTca' => [ |
||
| 280 | 'ctrl' => [], |
||
| 281 | 'columns' => [ |
||
| 282 | self::FOREIGN_FIELD => [ |
||
| 283 | 'config' => $GLOBALS['TCA'][self::FOREIGN_TABLE]['columns'][self::FOREIGN_FIELD]['config'], |
||
| 284 | ], |
||
| 285 | ], |
||
| 286 | ], |
||
| 287 | 'inlineExpandCollapseStateArray' => $result['inlineExpandCollapseStateArray'], |
||
| 288 | ])['processedTca']['columns'][self::FOREIGN_FIELD]['config']['items'] ?? []; |
||
| 289 | |||
| 290 | return $result; |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Create the database row for the default site language based |
||
| 295 | * on an already existing default language from another site. |
||
| 296 | * |
||
| 297 | * @return array |
||
| 298 | */ |
||
| 299 | protected function getDefaultDatabaseRow(): array |
||
| 339 | } |
||
| 340 | } |
||
| 341 |