| Total Complexity | 42 |
| Total Lines | 358 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like LinkAnalyzerResult 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 LinkAnalyzerResult, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class LinkAnalyzerResult |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var LinkAnalyzer |
||
| 38 | */ |
||
| 39 | protected $linkAnalyzer; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var BrokenLinkRepository |
||
| 43 | */ |
||
| 44 | protected $brokenLinkRepository; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var ConnectionPool |
||
| 48 | */ |
||
| 49 | protected $connectionPool; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $brokenLinks = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $newBrokenLinkCounts = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $oldBrokenLinkCounts = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var bool |
||
| 68 | */ |
||
| 69 | protected $differentToLastResult = false; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Save language codes to reduce database requests |
||
| 73 | * |
||
| 74 | * @var array<int, string> |
||
| 75 | */ |
||
| 76 | protected $languageCodes = ['default']; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Save localized pages to reduce database requests |
||
| 80 | * |
||
| 81 | * @var array<string, int> |
||
| 82 | */ |
||
| 83 | protected $localizedPages = []; |
||
| 84 | |||
| 85 | public function __construct( |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Call LinkAnalyzer with provided task configuration and process result values |
||
| 97 | * |
||
| 98 | * @param int $page |
||
| 99 | * @param int $depth |
||
| 100 | * @param array $pageRow |
||
| 101 | * @param array $modTSconfig |
||
| 102 | * @param array $searchFields |
||
| 103 | * @param array $linkTypes |
||
| 104 | * @param string $languages |
||
| 105 | * |
||
| 106 | * @return $this |
||
| 107 | */ |
||
| 108 | public function getResultForTask( |
||
| 109 | int $page, |
||
| 110 | int $depth, |
||
| 111 | array $pageRow, |
||
| 112 | array $modTSconfig, |
||
| 113 | array $searchFields = [], |
||
| 114 | array $linkTypes = [], |
||
| 115 | string $languages = '' |
||
| 116 | ): self { |
||
| 117 | $rootLineHidden = $this->linkAnalyzer->getRootLineIsHidden($pageRow); |
||
| 118 | $checkHidden = $modTSconfig['checkhidden'] === 1; |
||
| 119 | |||
| 120 | if ($rootLineHidden && !$checkHidden) { |
||
| 121 | return $this; |
||
| 122 | } |
||
| 123 | |||
| 124 | $treeList = $this->linkAnalyzer->extGetTreeList($page, $depth, 0, '1=1', $modTSconfig['checkhidden']); |
||
| 125 | |||
| 126 | if ($pageRow['hidden'] === 0 || $checkHidden) { |
||
| 127 | $treeList .= $page; |
||
| 128 | } |
||
| 129 | |||
| 130 | if ($treeList === '') { |
||
| 131 | return $this; |
||
| 132 | } |
||
| 133 | |||
| 134 | $pageList = $this->addPageTranslationsToPageList( |
||
| 135 | $treeList, |
||
| 136 | $page, |
||
| 137 | (bool)$modTSconfig['checkhidden'], |
||
| 138 | $languages |
||
| 139 | ); |
||
| 140 | |||
| 141 | $this->linkAnalyzer->init($searchFields, $pageList, $modTSconfig); |
||
| 142 | $this->oldBrokenLinkCounts = $this->linkAnalyzer->getLinkCounts(); |
||
| 143 | |||
| 144 | $this->linkAnalyzer->getLinkStatistics($linkTypes, $modTSconfig['checkhidden']); |
||
| 145 | $this->newBrokenLinkCounts = $this->linkAnalyzer->getLinkCounts(); |
||
| 146 | |||
| 147 | $this->brokenLinks = $this->brokenLinkRepository->getAllBrokenLinksForPages( |
||
| 148 | GeneralUtility::intExplode(',', $pageList, true), |
||
| 149 | array_keys($linkTypes), |
||
| 150 | $searchFields, |
||
| 151 | GeneralUtility::intExplode(',', $languages, true) |
||
| 152 | ); |
||
| 153 | |||
| 154 | $this |
||
| 155 | ->processLinkCounts($linkTypes) |
||
| 156 | ->processBrokenLinks(); |
||
| 157 | |||
| 158 | return $this; |
||
| 159 | } |
||
| 160 | |||
| 161 | public function setBrokenLinks(array $brokenLinks): void |
||
| 162 | { |
||
| 163 | $this->brokenLinks = $brokenLinks; |
||
| 164 | } |
||
| 165 | |||
| 166 | public function getBrokenLinks(): array |
||
| 169 | } |
||
| 170 | |||
| 171 | public function setNewBrokenLinkCounts(array $newBrokenLinkCounts): void |
||
| 172 | { |
||
| 173 | $this->newBrokenLinkCounts = $newBrokenLinkCounts; |
||
| 174 | } |
||
| 175 | |||
| 176 | public function getNewBrokenLinkCounts(): array |
||
| 177 | { |
||
| 178 | return $this->newBrokenLinkCounts; |
||
| 179 | } |
||
| 180 | |||
| 181 | public function setOldBrokenLinkCounts(array $oldBrokenLinkCounts): void |
||
| 182 | { |
||
| 183 | $this->oldBrokenLinkCounts = $oldBrokenLinkCounts; |
||
| 184 | } |
||
| 185 | |||
| 186 | public function getOldBrokenLinkCounts(): array |
||
| 187 | { |
||
| 188 | return $this->oldBrokenLinkCounts; |
||
| 189 | } |
||
| 190 | |||
| 191 | public function getTotalBrokenLinksCount(): int |
||
| 192 | { |
||
| 193 | return $this->newBrokenLinkCounts['total'] ?? 0; |
||
| 194 | } |
||
| 195 | |||
| 196 | public function isDifferentToLastResult(): bool |
||
| 197 | { |
||
| 198 | return $this->differentToLastResult; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Process the link counts (old and new) and ensures that all link types are available in the array |
||
| 203 | * |
||
| 204 | * @param array<int, string> $linkTypes list of link types |
||
| 205 | * @return LinkAnalyzerResult |
||
| 206 | */ |
||
| 207 | protected function processLinkCounts(array $linkTypes): self |
||
| 208 | { |
||
| 209 | foreach (array_keys($linkTypes) as $linkType) { |
||
| 210 | if (!isset($this->newBrokenLinkCounts[$linkType])) { |
||
| 211 | $this->newBrokenLinkCounts[$linkType] = 0; |
||
| 212 | } |
||
| 213 | if (!isset($this->oldBrokenLinkCounts[$linkType])) { |
||
| 214 | $this->oldBrokenLinkCounts[$linkType] = 0; |
||
| 215 | } |
||
| 216 | if ($this->newBrokenLinkCounts[$linkType] !== $this->oldBrokenLinkCounts[$linkType]) { |
||
| 217 | $this->differentToLastResult = true; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | return $this; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Process broken link values and assign them to new variables which are used in the templates |
||
| 226 | * shipped by the core but can also be used in custom templates. The raw data is untouched and |
||
| 227 | * can also still be used in custom templates. |
||
| 228 | * |
||
| 229 | * @return LinkAnalyzerResult |
||
| 230 | */ |
||
| 231 | protected function processBrokenLinks(): self |
||
| 232 | { |
||
| 233 | foreach ($this->brokenLinks as $key => &$brokenLink) { |
||
| 234 | $fullRecord = BackendUtility::getRecord($brokenLink['table_name'], $brokenLink['record_uid']); |
||
| 235 | |||
| 236 | if ($fullRecord !== null) { |
||
| 237 | $brokenLink['full_record'] = $fullRecord; |
||
| 238 | $brokenLink['record_title'] = BackendUtility::getRecordTitle($brokenLink['table_name'], $fullRecord); |
||
| 239 | } |
||
| 240 | |||
| 241 | $brokenLink['real_pid'] = ((int)($brokenLink['language'] ?? 0) > 0 && (string)($brokenLink['table_name'] ?? '') !== 'pages') |
||
| 242 | ? $this->getLocalizedPageId((int)$brokenLink['record_pid'], (int)$brokenLink['language']) |
||
| 243 | : $brokenLink['record_pid']; |
||
| 244 | $pageRecord = BackendUtility::getRecord('pages', $brokenLink['real_pid']); |
||
| 245 | |||
| 246 | if ($pageRecord !== null) { |
||
| 247 | $brokenLink['page_record'] = $pageRecord; |
||
| 248 | } |
||
| 249 | |||
| 250 | $brokenLink['record_type'] = $this->getLanguageService()->sL($GLOBALS['TCA'][$brokenLink['table_name']]['ctrl']['title'] ?? ''); |
||
| 251 | $brokenLink['target'] = (((string)($brokenLink['link_type'] ?? '') === 'db') ? 'id:' : '') . $brokenLink['url']; |
||
| 252 | $brokenLink['language_code'] = $this->getLanguageCode((int)$brokenLink['language']); |
||
| 253 | } |
||
| 254 | |||
| 255 | return $this; |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Add localized page ids to the list of pages to get broken links from |
||
| 260 | * |
||
| 261 | * @param string $pageList |
||
| 262 | * @param int $page |
||
| 263 | * @param bool $checkHidden |
||
| 264 | * @param string $languages |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | protected function addPageTranslationsToPageList( |
||
| 268 | string $pageList, |
||
| 269 | int $page, |
||
| 270 | bool $checkHidden, |
||
| 271 | string $languages = '' |
||
| 272 | ): string { |
||
| 273 | $queryBuilder = $this->connectionPool->getQueryBuilderForTable('pages'); |
||
| 274 | $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class)); |
||
| 275 | |||
| 276 | $constraints[] = $queryBuilder->expr()->eq( |
||
|
|
|||
| 277 | 'l10n_parent', |
||
| 278 | $queryBuilder->createNamedParameter($page, Connection::PARAM_INT) |
||
| 279 | ); |
||
| 280 | |||
| 281 | if ($languages !== '') { |
||
| 282 | $constraints[] = $queryBuilder->expr()->in( |
||
| 283 | 'sys_language_uid', |
||
| 284 | $queryBuilder->createNamedParameter( |
||
| 285 | GeneralUtility::intExplode(',', $languages, true), |
||
| 286 | Connection::PARAM_INT_ARRAY |
||
| 287 | ) |
||
| 288 | ); |
||
| 289 | } |
||
| 290 | |||
| 291 | $result = $queryBuilder |
||
| 292 | ->select('uid', 'hidden') |
||
| 293 | ->from('pages') |
||
| 294 | ->where(...$constraints) |
||
| 295 | ->execute(); |
||
| 296 | |||
| 297 | while ($row = $result->fetch()) { |
||
| 298 | if ($row['hidden'] === 0 || $checkHidden) { |
||
| 299 | $pageList .= ',' . $row['uid']; |
||
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | return $pageList; |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Get language iso code and store it in the local property languageCodes |
||
| 308 | * |
||
| 309 | * @param int $languageId |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | protected function getLanguageCode(int $languageId): string |
||
| 313 | { |
||
| 314 | if ((bool)($this->languageCodes[$languageId] ?? false)) { |
||
| 315 | return $this->languageCodes[$languageId]; |
||
| 316 | } |
||
| 317 | |||
| 318 | $queryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_language'); |
||
| 319 | $queryBuilder |
||
| 320 | ->getRestrictions() |
||
| 321 | ->removeAll() |
||
| 322 | ->add(GeneralUtility::makeInstance(DeletedRestriction::class)); |
||
| 323 | |||
| 324 | $langauge = $queryBuilder |
||
| 325 | ->select('uid', 'language_isocode') |
||
| 326 | ->from('sys_language') |
||
| 327 | ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($languageId, \PDO::PARAM_INT))) |
||
| 328 | ->setMaxResults(1) |
||
| 329 | ->execute() |
||
| 330 | ->fetch() ?: []; |
||
| 331 | |||
| 332 | if (is_array($langauge) && $langauge !== []) { |
||
| 333 | $this->languageCodes[(int)$langauge['uid']] = $langauge['language_isocode']; |
||
| 334 | return $langauge['language_isocode']; |
||
| 335 | } |
||
| 336 | |||
| 337 | return ''; |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Get localized page id and store it in the local property localizedPages |
||
| 342 | * |
||
| 343 | * @param int $parentId |
||
| 344 | * @param int $languageId |
||
| 345 | * @return int |
||
| 346 | */ |
||
| 347 | protected function getLocalizedPageId(int $parentId, int $languageId): int |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @return LanguageService |
||
| 388 | */ |
||
| 389 | protected function getLanguageService(): LanguageService |
||
| 390 | { |
||
| 391 | return $GLOBALS['LANG']; |
||
| 392 | } |
||
| 393 | } |
||
| 394 |