We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 58 |
| Total Lines | 374 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Complex classes like CalendarController 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 CalendarController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class CalendarController extends AbstractController |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * This holds all issues for the list view. |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | * @access protected |
||
| 27 | */ |
||
| 28 | protected $allIssues = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The main method of the plugin |
||
| 32 | * |
||
| 33 | * @return void |
||
| 34 | */ |
||
| 35 | public function mainAction() |
||
| 36 | { |
||
| 37 | $requestData = GeneralUtility::_GPmerged('tx_dlf'); |
||
| 38 | unset($requestData['__referrer'], $requestData['__trustedProperties']); |
||
| 39 | |||
| 40 | // Set initial document (anchor or year file) if configured. |
||
| 41 | if (empty($requestData['id']) && !empty($this->settings['initialDocument'])) { |
||
| 42 | $requestData['id'] = $this->settings['initialDocument']; |
||
| 43 | } |
||
| 44 | |||
| 45 | // Load current document. |
||
| 46 | $this->loadDocument($requestData); |
||
| 47 | if ($this->doc === null) { |
||
| 48 | // Quit without doing anything if required variables are not set. |
||
| 49 | return; |
||
| 50 | } |
||
| 51 | |||
| 52 | $metadata = $this->doc->getTitledata(); |
||
| 53 | if (!empty($metadata['type'][0])) { |
||
| 54 | $type = $metadata['type'][0]; |
||
| 55 | } else { |
||
| 56 | return; |
||
| 57 | } |
||
| 58 | |||
| 59 | switch ($type) { |
||
| 60 | case 'newspaper': |
||
| 61 | case 'ephemera': |
||
| 62 | $this->forward('years', null, null, $requestData); |
||
| 63 | break; |
||
| 64 | case 'year': |
||
| 65 | $this->forward('calendar', null, null, $requestData); |
||
| 66 | break; |
||
| 67 | case 'issue': |
||
| 68 | default: |
||
| 69 | break; |
||
| 70 | } |
||
| 71 | |||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The Calendar Method |
||
| 76 | * |
||
| 77 | * @access public |
||
| 78 | * |
||
| 79 | * @param string $content: The PlugIn content |
||
| 80 | * @param array $conf: The PlugIn configuration |
||
| 81 | * |
||
| 82 | * @return void |
||
| 83 | */ |
||
| 84 | public function calendarAction() |
||
| 85 | { |
||
| 86 | $requestData = GeneralUtility::_GPmerged('tx_dlf'); |
||
| 87 | unset($requestData['__referrer'], $requestData['__trustedProperties']); |
||
| 88 | |||
| 89 | // access arguments passed by the mainAction() |
||
| 90 | $mainrquestData = $this->request->getArguments(); |
||
| 91 | |||
| 92 | // merge both arguments together --> passing id by GET parameter tx_dlf[id] should win |
||
| 93 | $requestData = array_merge($requestData, $mainrquestData); |
||
| 94 | |||
| 95 | // Load current document. |
||
| 96 | $this->loadDocument($requestData); |
||
| 97 | if ($this->doc === null) { |
||
| 98 | // Quit without doing anything if required variables are not set. |
||
| 99 | return; |
||
| 100 | } |
||
| 101 | |||
| 102 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 103 | ->getQueryBuilderForTable('tx_dlf_documents'); |
||
| 104 | |||
| 105 | // Get all children of year anchor. |
||
| 106 | $result = $queryBuilder |
||
| 107 | ->select( |
||
| 108 | 'tx_dlf_documents.uid AS uid', |
||
| 109 | 'tx_dlf_documents.title AS title', |
||
| 110 | 'tx_dlf_documents.year AS year', |
||
| 111 | 'tx_dlf_documents.mets_label AS label', |
||
| 112 | 'tx_dlf_documents.mets_orderlabel AS orderlabel' |
||
| 113 | ) |
||
| 114 | ->from('tx_dlf_documents') |
||
| 115 | ->where( |
||
| 116 | $queryBuilder->expr()->eq('tx_dlf_documents.structure', Helper::getUidFromIndexName('issue', 'tx_dlf_structures', $this->doc->cPid)), |
||
| 117 | $queryBuilder->expr()->eq('tx_dlf_documents.partof', intval($this->doc->uid)), |
||
| 118 | Helper::whereExpression('tx_dlf_documents') |
||
| 119 | ) |
||
| 120 | ->orderBy('tx_dlf_documents.mets_orderlabel') |
||
| 121 | ->execute(); |
||
| 122 | |||
| 123 | $issues = []; |
||
| 124 | |||
| 125 | // Process results. |
||
| 126 | while ($resArray = $result->fetch()) { |
||
| 127 | // Set title for display in calendar view. |
||
| 128 | if (!empty($resArray['title'])) { |
||
| 129 | $title = $resArray['title']; |
||
| 130 | } else { |
||
| 131 | $title = !empty($resArray['label']) ? $resArray['label'] : $resArray['orderlabel']; |
||
| 132 | if (strtotime($title) !== false) { |
||
| 133 | $title = strftime('%x', strtotime($title)); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | $issues[] = [ |
||
| 137 | 'uid' => $resArray['uid'], |
||
| 138 | 'title' => $title, |
||
| 139 | 'year' => $resArray['year'] |
||
| 140 | ]; |
||
| 141 | } |
||
| 142 | // We need an array of issues with year => month => day number as key. |
||
| 143 | $calendarIssuesByYear = []; |
||
| 144 | foreach ($issues as $issue) { |
||
| 145 | $dateTimestamp = strtotime($issue['year']); |
||
| 146 | if ($dateTimestamp !== false) { |
||
| 147 | $_year = date('Y', $dateTimestamp); |
||
| 148 | $_month = date('n', $dateTimestamp); |
||
| 149 | $_day = date('j', $dateTimestamp); |
||
| 150 | $calendarIssuesByYear[$_year][$_month][$_day][] = $issue; |
||
| 151 | } else { |
||
| 152 | $this->logger->warning('Document with UID ' . $issue['uid'] . 'has no valid date of publication'); |
||
|
1 ignored issue
–
show
|
|||
| 153 | } |
||
| 154 | } |
||
| 155 | // Sort by years. |
||
| 156 | ksort($calendarIssuesByYear); |
||
| 157 | // Build calendar for year (default) or season. |
||
| 158 | $iteration = 1; |
||
| 159 | foreach ($calendarIssuesByYear as $year => $calendarIssuesByMonth) { |
||
| 160 | // Sort by months. |
||
| 161 | ksort($calendarIssuesByMonth); |
||
| 162 | // Default: First month is January, last month is December. |
||
| 163 | $firstMonth = 1; |
||
| 164 | $lastMonth = 12; |
||
| 165 | // Show calendar from first issue up to end of season if applicable. |
||
| 166 | if ( |
||
| 167 | empty($this->settings['showEmptyMonths']) |
||
| 168 | && count($calendarIssuesByYear) > 1 |
||
| 169 | ) { |
||
| 170 | if ($iteration == 1) { |
||
| 171 | $firstMonth = (int) key($calendarIssuesByMonth); |
||
| 172 | } elseif ($iteration == count($calendarIssuesByYear)) { |
||
| 173 | end($calendarIssuesByMonth); |
||
| 174 | $lastMonth = (int) key($calendarIssuesByMonth); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | $this->getCalendarYear($calendarIssuesByMonth, $year, $firstMonth, $lastMonth); |
||
| 178 | $iteration++; |
||
| 179 | } |
||
| 180 | // Prepare list as alternative view. |
||
| 181 | $issueData = []; |
||
| 182 | foreach ($this->allIssues as $dayTimestamp => $issues) { |
||
| 183 | $issueData[$dayTimestamp]['dateString'] = strftime('%A, %x', $dayTimestamp); |
||
| 184 | $issueData[$dayTimestamp]['items'] = []; |
||
| 185 | foreach ($issues as $issue) { |
||
| 186 | $issueData[$dayTimestamp]['items'][] = $issue; |
||
| 187 | } |
||
| 188 | } |
||
| 189 | $this->view->assign('issueData', $issueData); |
||
| 190 | |||
| 191 | // Link to current year. |
||
| 192 | $linkTitleData = $this->doc->getTitledata(); |
||
| 193 | $yearLinkTitle = !empty($linkTitleData['mets_orderlabel'][0]) ? $linkTitleData['mets_orderlabel'][0] : $linkTitleData['mets_label'][0]; |
||
| 194 | |||
| 195 | $this->view->assign('documentId', $this->doc->uid); |
||
| 196 | $this->view->assign('yearLinkTitle', $yearLinkTitle); |
||
| 197 | $this->view->assign('parentDocumentId', $this->doc->parentId); |
||
| 198 | $this->view->assign('allYearDocTitle', $this->doc->getTitle($this->doc->parentId)); |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * The Years Method |
||
| 203 | * |
||
| 204 | * @access public |
||
| 205 | * |
||
| 206 | * @return void |
||
| 207 | */ |
||
| 208 | public function yearsAction() |
||
| 209 | { |
||
| 210 | $requestData = GeneralUtility::_GPmerged('tx_dlf'); |
||
| 211 | unset($requestData['__referrer'], $requestData['__trustedProperties']); |
||
| 212 | |||
| 213 | // access arguments passed by the mainAction() |
||
| 214 | $mainrquestData = $this->request->getArguments(); |
||
| 215 | |||
| 216 | // merge both arguments together --> passing id by GET parameter tx_dlf[id] should win |
||
| 217 | $requestData = array_merge($requestData, $mainrquestData); |
||
| 218 | |||
| 219 | // Load current document. |
||
| 220 | $this->loadDocument($requestData); |
||
| 221 | if ($this->doc === null) { |
||
| 222 | // Quit without doing anything if required variables are not set. |
||
| 223 | return; |
||
| 224 | } |
||
| 225 | |||
| 226 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 227 | ->getQueryBuilderForTable('tx_dlf_documents'); |
||
| 228 | |||
| 229 | // Get all children of anchor. This should be the year anchor documents |
||
| 230 | $result = $queryBuilder |
||
| 231 | ->select( |
||
| 232 | 'tx_dlf_documents.uid AS uid', |
||
| 233 | 'tx_dlf_documents.title AS title', |
||
| 234 | 'tx_dlf_documents.mets_label AS label', |
||
| 235 | 'tx_dlf_documents.mets_orderlabel AS orderlabel' |
||
| 236 | ) |
||
| 237 | ->from('tx_dlf_documents') |
||
| 238 | ->where( |
||
| 239 | $queryBuilder->expr()->eq('tx_dlf_documents.structure', Helper::getUidFromIndexName('year', 'tx_dlf_structures', $this->doc->cPid)), |
||
| 240 | $queryBuilder->expr()->eq('tx_dlf_documents.partof', intval($this->doc->uid)), |
||
| 241 | Helper::whereExpression('tx_dlf_documents') |
||
| 242 | ) |
||
| 243 | ->orderBy('tx_dlf_documents.mets_orderlabel') |
||
| 244 | ->execute(); |
||
| 245 | |||
| 246 | $years = []; |
||
| 247 | // Process results. |
||
| 248 | while ($resArray = $result->fetch()) { |
||
| 249 | $years[] = [ |
||
| 250 | 'title' => !empty($resArray['label']) ? $resArray['label'] : (!empty($resArray['orderlabel']) ? $resArray['orderlabel'] : $resArray['title']), |
||
| 251 | 'uid' => $resArray['uid'] |
||
| 252 | ]; |
||
| 253 | } |
||
| 254 | $yearArray = []; |
||
| 255 | if (count($years) > 0) { |
||
| 256 | foreach ($years as $year) { |
||
| 257 | $yearArray[] = [ |
||
| 258 | 'documentId' => $year['uid'], |
||
| 259 | 'title' => $year['title'] |
||
| 260 | ]; |
||
| 261 | } |
||
| 262 | $this->view->assign('yearName', $yearArray); |
||
| 263 | } |
||
| 264 | |||
| 265 | $this->view->assign('documentId', $this->doc->uid); |
||
| 266 | $this->view->assign('allYearDocTitle', $this->doc->getTitle($this->doc->uid)); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Build calendar for a certain year |
||
| 271 | * |
||
| 272 | * @access protected |
||
| 273 | * |
||
| 274 | * @param array $calendarIssuesByMonth All issues sorted by month => day |
||
| 275 | * @param int $year Gregorian year |
||
| 276 | * @param int $firstMonth 1 for January, 2 for February, ... 12 for December |
||
| 277 | * @param int $lastMonth 1 for January, 2 for February, ... 12 for December |
||
| 278 | * |
||
| 279 | * @return string Content for template subpart |
||
| 280 | */ |
||
| 281 | protected function getCalendarYear($calendarIssuesByMonth, $year, $firstMonth = 1, $lastMonth = 12) |
||
| 394 | } |
||
| 395 | } |
||
| 396 |
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.