We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 41 |
| Total Lines | 247 |
| 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() |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * This builds a menu array for HMENU |
||
| 58 | * |
||
| 59 | * @access protected |
||
| 60 | * @return array HMENU array |
||
| 61 | */ |
||
| 62 | protected function makeMenuArray() |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * This builds an array for one menu entry |
||
| 122 | * |
||
| 123 | * @access protected |
||
| 124 | * |
||
| 125 | * @param array $entry : The entry's array from \Kitodo\Dlf\Common\Doc->getLogicalStructure |
||
| 126 | * @param bool $recursive : Whether to include the child entries |
||
| 127 | * |
||
| 128 | * @return array HMENU array for menu entry |
||
| 129 | */ |
||
| 130 | protected function getMenuEntry(array $entry, $recursive = false) |
||
| 131 | { |
||
| 132 | $entry = $this->resolveMenuEntry($entry); |
||
| 133 | |||
| 134 | $entryArray = []; |
||
| 135 | // Set "title", "volume", "type" and "pagination" from $entry array. |
||
| 136 | $entryArray['title'] = !empty($entry['label']) ? $entry['label'] : $entry['orderlabel']; |
||
| 137 | $entryArray['volume'] = $entry['volume']; |
||
| 138 | $entryArray['orderlabel'] = $entry['orderlabel']; |
||
| 139 | $entryArray['type'] = $this->getTranslatedType($entry['type']); |
||
| 140 | $entryArray['pagination'] = htmlspecialchars($entry['pagination']); |
||
| 141 | $entryArray['_OVERRIDE_HREF'] = ''; |
||
| 142 | $entryArray['doNotLinkIt'] = 1; |
||
| 143 | $entryArray['ITEM_STATE'] = 'NO'; |
||
| 144 | // Build menu links based on the $entry['points'] array. |
||
| 145 | if ( |
||
| 146 | !empty($entry['points']) |
||
| 147 | && MathUtility::canBeInterpretedAsInteger($entry['points']) |
||
| 148 | ) { |
||
| 149 | $entryArray['page'] = $entry['points']; |
||
| 150 | |||
| 151 | $entryArray['doNotLinkIt'] = 0; |
||
| 152 | if ($this->settings['basketButton']) { |
||
| 153 | $entryArray['basketButton'] = [ |
||
| 154 | 'logId' => $entry['id'], |
||
| 155 | 'startpage' => $entry['points'] |
||
| 156 | ]; |
||
| 157 | } |
||
| 158 | } elseif ( |
||
| 159 | !empty($entry['points']) |
||
| 160 | && is_string($entry['points']) |
||
| 161 | ) { |
||
| 162 | $entryArray['id'] = $entry['points']; |
||
| 163 | $entryArray['page'] = 1; |
||
| 164 | $entryArray['doNotLinkIt'] = 0; |
||
| 165 | if ($this->settings['basketButton']) { |
||
| 166 | $entryArray['basketButton'] = [ |
||
| 167 | 'logId' => $entry['id'], |
||
| 168 | 'startpage' => $entry['points'] |
||
| 169 | ]; |
||
| 170 | } |
||
| 171 | } elseif (!empty($entry['targetUid'])) { |
||
| 172 | $entryArray['id'] = $entry['targetUid']; |
||
| 173 | $entryArray['page'] = 1; |
||
| 174 | $entryArray['doNotLinkIt'] = 0; |
||
| 175 | if ($this->settings['basketButton']) { |
||
| 176 | $entryArray['basketButton'] = [ |
||
| 177 | 'logId' => $entry['id'], |
||
| 178 | 'startpage' => $entry['targetUid'] |
||
| 179 | ]; |
||
| 180 | } |
||
| 181 | } |
||
| 182 | // Set "ITEM_STATE" to "CUR" if this entry points to current page. |
||
| 183 | if (in_array($entry['id'], $this->activeEntries)) { |
||
| 184 | $entryArray['ITEM_STATE'] = 'CUR'; |
||
| 185 | } |
||
| 186 | // Build sub-menu if available and called recursively. |
||
| 187 | if ( |
||
| 188 | $recursive === true |
||
| 189 | && !empty($entry['children']) |
||
| 190 | ) { |
||
| 191 | // Build sub-menu only if one of the following conditions apply: |
||
| 192 | // 1. Current menu node is in rootline |
||
| 193 | // 2. Current menu node points to another file |
||
| 194 | // 3. Current menu node has no corresponding images |
||
| 195 | if ( |
||
| 196 | $entryArray['ITEM_STATE'] == 'CUR' |
||
| 197 | || is_string($entry['points']) |
||
| 198 | || empty($this->document->getDoc()->smLinks['l2p'][$entry['id']]) |
||
| 199 | ) { |
||
| 200 | $entryArray['_SUB_MENU'] = []; |
||
| 201 | foreach ($entry['children'] as $child) { |
||
| 202 | // Set "ITEM_STATE" to "ACT" if this entry points to current page and has sub-entries pointing to the same page. |
||
| 203 | if (in_array($child['id'], $this->activeEntries)) { |
||
| 204 | $entryArray['ITEM_STATE'] = 'ACT'; |
||
| 205 | } |
||
| 206 | $entryArray['_SUB_MENU'][] = $this->getMenuEntry($child, true); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | // Append "IFSUB" to "ITEM_STATE" if this entry has sub-entries. |
||
| 210 | $entryArray['ITEM_STATE'] = ($entryArray['ITEM_STATE'] == 'NO' ? 'IFSUB' : $entryArray['ITEM_STATE'] . 'IFSUB'); |
||
| 211 | } |
||
| 212 | return $entryArray; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * If $entry references an external METS file (as mptr), |
||
| 217 | * try to resolve its database UID and return an updated $entry. |
||
| 218 | * |
||
| 219 | * This is so that when linking from a child document back to its parent, |
||
| 220 | * that link is via UID, so that subsequently the parent's TOC is built from database. |
||
| 221 | * |
||
| 222 | * @param array $entry |
||
| 223 | * @return array |
||
| 224 | */ |
||
| 225 | protected function resolveMenuEntry($entry) |
||
| 226 | { |
||
| 227 | // If the menu entry points to the parent document, |
||
| 228 | // resolve to the parent UID set on indexation. |
||
| 229 | $doc = $this->document->getDoc(); |
||
| 230 | if ( |
||
| 231 | $doc instanceof MetsDocument |
||
| 232 | && $entry['points'] === $doc->parentHref |
||
| 233 | && !empty($this->document->getPartof()) |
||
| 234 | ) { |
||
| 235 | unset($entry['points']); |
||
| 236 | $entry['targetUid'] = $this->document->getPartof(); |
||
| 237 | } |
||
| 238 | |||
| 239 | return $entry; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Get translated type of entry. |
||
| 244 | * |
||
| 245 | * @param array $type |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | private function getTranslatedType($type) { |
||
| 249 | return Helper::translate($type, 'tx_dlf_structures', $this->settings['storagePid']); |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Sort menu by orderlabel - currently implemented for newspaper. |
||
| 254 | * //TODO: add for years |
||
| 255 | * |
||
| 256 | * @param array &$menu |
||
| 257 | * @return void |
||
| 258 | */ |
||
| 259 | private function sortMenu(&$menu) { |
||
| 260 | if ($menu[0]['type'] == $this->getTranslatedType("newspaper")) { |
||
| 261 | $this->sortMenuForNewspapers($menu); |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Sort menu years of the newspaper by orderlabel. |
||
| 267 | * |
||
| 268 | * @param array &$menu |
||
| 269 | * @return void |
||
| 270 | */ |
||
| 271 | private function sortMenuForNewspapers(&$menu) { |
||
| 274 | }); |
||
| 275 | } |
||
| 276 | } |
||
| 277 |
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.