We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 70 |
| Total Lines | 356 |
| Duplicated Lines | 0 % |
| Changes | 11 | ||
| Bugs | 1 | Features | 0 |
Complex classes like TableOfContentsController 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 TableOfContentsController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class TableOfContentsController extends AbstractController |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * This holds the active entries according to the currently selected page |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | * @access protected |
||
| 34 | */ |
||
| 35 | protected $activeEntries = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The main method of the plugin |
||
| 39 | * |
||
| 40 | * @return void |
||
| 41 | */ |
||
| 42 | public function mainAction() |
||
| 43 | { |
||
| 44 | // Load current document. |
||
| 45 | $this->loadDocument($this->requestData); |
||
| 46 | if ( |
||
| 47 | $this->document === null |
||
| 48 | || $this->document->getDoc() === null |
||
| 49 | ) { |
||
| 50 | // Quit without doing anything if required variables are not set. |
||
| 51 | return; |
||
| 52 | } else { |
||
| 53 | if (!empty($this->requestData['logicalPage'])) { |
||
| 54 | $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']); |
||
| 55 | // The logical page parameter should not appear again |
||
| 56 | unset($this->requestData['logicalPage']); |
||
| 57 | } |
||
| 58 | if ($this->document->getDoc()->tableOfContents[0]['type'] == 'collection') { |
||
| 59 | $this->view->assign('currentList', $this->requestData['id']); |
||
| 60 | if (isset($this->requestData['transform'])) { |
||
| 61 | $this->view->assign('transform', $this->requestData['transform']); |
||
| 62 | } else { |
||
| 63 | $this->view->assign('transform', 'something'); |
||
| 64 | } |
||
| 65 | $this->view->assign('type', 'collection'); |
||
| 66 | $this->view->assign('types', $this->getTypes($this->document->getDoc()->tableOfContents)); |
||
| 67 | $this->view->assign('toc', $this->makeMenuFor3DObjects()); |
||
| 68 | } else { |
||
| 69 | $this->view->assign('type', 'other'); |
||
| 70 | $this->view->assign('toc', $this->makeMenuArray()); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * This builds a menu array for HMENU |
||
| 77 | * |
||
| 78 | * @access protected |
||
| 79 | * @return array HMENU array |
||
| 80 | */ |
||
| 81 | protected function makeMenuArray() |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * This builds a menu for list of 3D objects |
||
| 150 | * |
||
| 151 | * @access protected |
||
| 152 | * |
||
| 153 | * @param string $content: The PlugIn content |
||
| 154 | * @param array $conf: The PlugIn configuration |
||
| 155 | * |
||
| 156 | * @return array HMENU array |
||
| 157 | */ |
||
| 158 | protected function makeMenuFor3DObjects() |
||
| 159 | { |
||
| 160 | $menuArray = []; |
||
| 161 | |||
| 162 | // Go through table of contents and create all menu entries. |
||
| 163 | foreach ($this->document->getDoc()->tableOfContents as $entry) { |
||
| 164 | $menuArray[] = $this->getMenuEntryWithImage($entry, true); |
||
| 165 | } |
||
| 166 | return $menuArray; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * This builds an array for one menu entry |
||
| 171 | * |
||
| 172 | * @access protected |
||
| 173 | * |
||
| 174 | * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure |
||
| 175 | * @param bool $recursive : Whether to include the child entries |
||
| 176 | * |
||
| 177 | * @return array HMENU array for menu entry |
||
| 178 | */ |
||
| 179 | protected function getMenuEntry(array $entry, $recursive = false) |
||
| 180 | { |
||
| 181 | $entry = $this->resolveMenuEntry($entry); |
||
| 182 | |||
| 183 | $entryArray = []; |
||
| 184 | // Set "title", "volume", "type" and "pagination" from $entry array. |
||
| 185 | $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel']; |
||
| 186 | $entryArray['volume'] = $entry['volume']; |
||
| 187 | $entryArray['orderlabel'] = $entry['orderlabel']; |
||
| 188 | $entryArray['type'] = Helper::translate($entry['type'], 'tx_dlf_structures', $this->settings['storagePid']); |
||
| 189 | $entryArray['pagination'] = htmlspecialchars($entry['pagination']); |
||
| 190 | $entryArray['_OVERRIDE_HREF'] = ''; |
||
| 191 | $entryArray['doNotLinkIt'] = 1; |
||
| 192 | $entryArray['ITEM_STATE'] = 'NO'; |
||
| 193 | // Build menu links based on the $entry['points'] array. |
||
| 194 | if ( |
||
| 195 | !empty($entry['points']) |
||
| 196 | && MathUtility::canBeInterpretedAsInteger($entry['points']) |
||
| 197 | ) { |
||
| 198 | $entryArray['page'] = $entry['points']; |
||
| 199 | |||
| 200 | $entryArray['doNotLinkIt'] = 0; |
||
| 201 | if ($this->settings['basketButton']) { |
||
| 202 | $entryArray['basketButton'] = [ |
||
| 203 | 'logId' => $entry['id'], |
||
| 204 | 'startpage' => $entry['points'] |
||
| 205 | ]; |
||
| 206 | } |
||
| 207 | } elseif ( |
||
| 208 | !empty($entry['points']) |
||
| 209 | && is_string($entry['points']) |
||
| 210 | ) { |
||
| 211 | $entryArray['id'] = $entry['points']; |
||
| 212 | $entryArray['page'] = 1; |
||
| 213 | $entryArray['doNotLinkIt'] = 0; |
||
| 214 | if ($this->settings['basketButton']) { |
||
| 215 | $entryArray['basketButton'] = [ |
||
| 216 | 'logId' => $entry['id'], |
||
| 217 | 'startpage' => $entry['points'] |
||
| 218 | ]; |
||
| 219 | } |
||
| 220 | } elseif (!empty($entry['targetUid'])) { |
||
| 221 | $entryArray['id'] = $entry['targetUid']; |
||
| 222 | $entryArray['page'] = 1; |
||
| 223 | $entryArray['doNotLinkIt'] = 0; |
||
| 224 | if ($this->settings['basketButton']) { |
||
| 225 | $entryArray['basketButton'] = [ |
||
| 226 | 'logId' => $entry['id'], |
||
| 227 | 'startpage' => $entry['targetUid'] |
||
| 228 | ]; |
||
| 229 | } |
||
| 230 | } |
||
| 231 | // Set "ITEM_STATE" to "CUR" if this entry points to current page. |
||
| 232 | if (in_array($entry['id'], $this->activeEntries)) { |
||
| 233 | $entryArray['ITEM_STATE'] = 'CUR'; |
||
| 234 | } |
||
| 235 | // Build sub-menu if available and called recursively. |
||
| 236 | if ( |
||
| 237 | $recursive === true |
||
| 238 | && !empty($entry['children']) |
||
| 239 | ) { |
||
| 240 | // Build sub-menu only if one of the following conditions apply: |
||
| 241 | // 1. Current menu node is in rootline |
||
| 242 | // 2. Current menu node points to another file |
||
| 243 | // 3. Current menu node has no corresponding images |
||
| 244 | if ( |
||
| 245 | $entryArray['ITEM_STATE'] == 'CUR' |
||
| 246 | || is_string($entry['points']) |
||
| 247 | || empty($this->document->getDoc()->smLinks['l2p'][$entry['id']]) |
||
| 248 | ) { |
||
| 249 | $entryArray['_SUB_MENU'] = []; |
||
| 250 | foreach ($entry['children'] as $child) { |
||
| 251 | // Set "ITEM_STATE" to "ACT" if this entry points to current page and has sub-entries pointing to the same page. |
||
| 252 | if (in_array($child['id'], $this->activeEntries)) { |
||
| 253 | $entryArray['ITEM_STATE'] = 'ACT'; |
||
| 254 | } |
||
| 255 | $entryArray['_SUB_MENU'][] = $this->getMenuEntry($child, true); |
||
| 256 | } |
||
| 257 | } |
||
| 258 | // Append "IFSUB" to "ITEM_STATE" if this entry has sub-entries. |
||
| 259 | $entryArray['ITEM_STATE'] = ($entryArray['ITEM_STATE'] == 'NO' ? 'IFSUB' : $entryArray['ITEM_STATE'] . 'IFSUB'); |
||
| 260 | } |
||
| 261 | return $entryArray; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * If $entry references an external METS file (as mptr), |
||
| 266 | * try to resolve its database UID and return an updated $entry. |
||
| 267 | * |
||
| 268 | * This is so that when linking from a child document back to its parent, |
||
| 269 | * that link is via UID, so that subsequently the parent's TOC is built from database. |
||
| 270 | * |
||
| 271 | * @param array $entry |
||
| 272 | * @return array |
||
| 273 | */ |
||
| 274 | protected function resolveMenuEntry($entry) |
||
| 275 | { |
||
| 276 | // If the menu entry points to the parent document, |
||
| 277 | // resolve to the parent UID set on indexation. |
||
| 278 | $doc = $this->document->getDoc(); |
||
| 279 | if ( |
||
| 280 | $doc instanceof MetsDocument |
||
| 281 | && $entry['points'] === $doc->parentHref |
||
| 282 | && !empty($this->document->getPartof()) |
||
| 283 | ) { |
||
| 284 | unset($entry['points']); |
||
| 285 | $entry['targetUid'] = $this->document->getPartof(); |
||
| 286 | } |
||
| 287 | |||
| 288 | return $entry; |
||
| 289 | } |
||
| 290 | |||
| 291 | protected function getMenuEntryWithImage(array $entry, $recursive = false) |
||
| 358 | } |
||
| 359 | |||
| 360 | private function getTypes($entry) { |
||
| 361 | $types = []; |
||
| 362 | $index = 0; |
||
| 363 | |||
| 364 | if (!empty($entry[0]['children'])) { |
||
| 365 | foreach ($entry[0]['children'] as $child) { |
||
| 366 | $type = $this->getType($child); |
||
| 367 | if (!(in_array($type, $types)) && $type != NULL) { |
||
| 368 | $types[$index] = $type; |
||
| 369 | $index++; |
||
| 370 | } |
||
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | return $types; |
||
| 375 | } |
||
| 376 | |||
| 377 | private function getType($entry) { |
||
| 383 | } |
||
| 384 | } |
||
| 385 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.