| Total Complexity | 48 |
| Total Lines | 344 |
| Duplicated Lines | 5.81 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DatedUpdateHolderController 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 DatedUpdateHolderController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class DatedUpdateHolderController extends PageController |
||
| 34 | { |
||
| 35 | private static $allowed_actions = [ |
||
| 36 | 'rss', |
||
| 37 | 'atom', |
||
| 38 | 'DateRangeForm', |
||
| 39 | ]; |
||
| 40 | |||
| 41 | private static $casting = [ |
||
| 42 | 'MetaTitle' => 'Text', |
||
| 43 | 'FilterDescription' => 'Text', |
||
| 44 | ]; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Get the meta title for the current action |
||
| 48 | * |
||
| 49 | * @return string |
||
| 50 | */ |
||
| 51 | public function getMetaTitle() |
||
| 52 | { |
||
| 53 | $title = $this->data()->getTitle(); |
||
| 54 | $filter = $this->FilterDescription(); |
||
| 55 | if ($filter) { |
||
| 56 | $title = "{$title} - {$filter}"; |
||
| 57 | } |
||
| 58 | |||
| 59 | $this->extend('updateMetaTitle', $title); |
||
| 60 | return $title; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Returns a description of the current filter |
||
| 65 | * |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | public function FilterDescription() |
||
| 69 | { |
||
| 70 | $params = $this->parseParams(); |
||
| 71 | |||
| 72 | $filters = array(); |
||
| 73 | if ($params['tag']) { |
||
| 74 | $term = TaxonomyTerm::get_by_id(TaxonomyTerm::class, $params['tag']); |
||
| 75 | if ($term) { |
||
| 76 | $filters[] = _t('DatedUpdateHolder.FILTER_WITHIN', 'within') . ' "' . $term->Name . '"'; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | if ($params['from'] || $params['to']) { |
||
| 81 | if ($params['from']) { |
||
| 82 | $from = strtotime($params['from']); |
||
| 83 | if ($params['to']) { |
||
| 84 | $to = strtotime($params['to']); |
||
| 85 | $filters[] = _t('DatedUpdateHolder.FILTER_BETWEEN', 'between') . ' ' |
||
| 86 | . date('j/m/Y', $from) . ' and ' . date('j/m/Y', $to); |
||
| 87 | } else { |
||
| 88 | $filters[] = _t('DatedUpdateHolder.FILTER_ON', 'on') . ' ' . date('j/m/Y', $from); |
||
| 89 | } |
||
| 90 | } else { |
||
| 91 | $to = strtotime($params['to']); |
||
| 92 | $filters[] = _t('DatedUpdateHolder.FILTER_ON', 'on') . ' ' . date('j/m/Y', $to); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | if ($params['year'] && $params['month']) { |
||
| 97 | $timestamp = mktime(1, 1, 1, $params['month'], 1, $params['year']); |
||
| 98 | $filters[] = _t('DatedUpdateHolder.FILTER_IN', 'in') . ' ' . date('F', $timestamp) . ' ' . $params['year']; |
||
| 99 | } |
||
| 100 | |||
| 101 | if ($filters) { |
||
| 102 | return $this->getUpdateName() . ' ' . implode(' ', $filters); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | public function getUpdateName() |
||
| 107 | { |
||
| 108 | return Config::inst()->get($this->data()->ClassName, 'update_name'); |
||
| 109 | } |
||
| 110 | |||
| 111 | protected function init() |
||
| 112 | { |
||
| 113 | parent::init(); |
||
| 114 | RSSFeed::linkToFeed($this->Link() . 'rss', $this->getSubscriptionTitle()); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Parse URL parameters. |
||
| 119 | * |
||
| 120 | * @param bool $produceErrorMessages Set to false to omit session messages. |
||
| 121 | */ |
||
| 122 | public function parseParams($produceErrorMessages = true) |
||
| 123 | { |
||
| 124 | $tag = $this->request->getVar('tag'); |
||
| 125 | $from = $this->request->getVar('from'); |
||
| 126 | $to = $this->request->getVar('to'); |
||
| 127 | $year = $this->request->getVar('year'); |
||
| 128 | $month = $this->request->getVar('month'); |
||
| 129 | |||
| 130 | if ($tag == '') { |
||
| 131 | $tag = null; |
||
| 132 | } |
||
| 133 | if ($from == '') { |
||
| 134 | $from = null; |
||
| 135 | } |
||
| 136 | if ($to == '') { |
||
| 137 | $to = null; |
||
| 138 | } |
||
| 139 | if ($year == '') { |
||
| 140 | $year = null; |
||
| 141 | } |
||
| 142 | if ($month == '') { |
||
| 143 | $month = null; |
||
| 144 | } |
||
| 145 | |||
| 146 | if (isset($tag)) { |
||
| 147 | $tag = (int)$tag; |
||
| 148 | } |
||
| 149 | View Code Duplication | if (isset($from)) { |
|
| 150 | $from = urldecode($from); |
||
| 151 | $parser = DBDatetime::create(); |
||
| 152 | $parser->setValue($from); |
||
| 153 | $from = $parser->Format('y-MM-dd'); |
||
| 154 | } |
||
| 155 | View Code Duplication | if (isset($to)) { |
|
| 156 | $to = urldecode($to); |
||
| 157 | $parser = DBDatetime::create(); |
||
| 158 | $parser->setValue($to); |
||
| 159 | $to = $parser->Format('y-MM-dd'); |
||
| 160 | } |
||
| 161 | if (isset($year)) { |
||
| 162 | $year = (int)$year; |
||
| 163 | } |
||
| 164 | if (isset($month)) { |
||
| 165 | $month = (int)$month; |
||
| 166 | } |
||
| 167 | |||
| 168 | // If only "To" has been provided filter by single date. Normalise by swapping with "From". |
||
| 169 | if (isset($to) && !isset($from)) { |
||
| 170 | list($to, $from) = array($from, $to); |
||
| 171 | } |
||
| 172 | |||
| 173 | // Flip the dates if the order is wrong. |
||
| 174 | if (isset($to) && isset($from) && strtotime($from)>strtotime($to)) { |
||
| 175 | list($to, $from) = array($from, $to); |
||
| 176 | |||
| 177 | if ($produceErrorMessages) { |
||
| 178 | // @todo replace |
||
| 179 | // Session::setFormMessage( |
||
| 180 | // 'Form_DateRangeForm', |
||
| 181 | // _t('DateUpdateHolder.FilterAppliedMessage', 'Filter has been applied with the dates reversed.'), |
||
| 182 | // 'warning' |
||
| 183 | // ); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | // Notify the user that filtering by single date is taking place. |
||
| 188 | if (isset($from) && !isset($to)) { |
||
| 189 | if ($produceErrorMessages) { |
||
| 190 | // @todo replace |
||
| 191 | // Session::setFormMessage( |
||
| 192 | // 'Form_DateRangeForm', |
||
| 193 | // _t('DateUpdateHolder.DateRangeFilterMessage', 'Filtered by a single date.'), |
||
| 194 | // 'warning' |
||
| 195 | // ); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | return [ |
||
| 200 | 'tag' => $tag, |
||
| 201 | 'from' => $from, |
||
| 202 | 'to' => $to, |
||
| 203 | 'year' => $year, |
||
| 204 | 'month' => $month, |
||
| 205 | ]; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Build the link - keep the date range, reset the rest. |
||
| 210 | */ |
||
| 211 | public function AllTagsLink() |
||
| 212 | { |
||
| 213 | $link = HTTP::setGetVar('tag', null, null, '&'); |
||
| 214 | $link = HTTP::setGetVar('month', null, $link, '&'); |
||
| 215 | $link = HTTP::setGetVar('year', null, $link, '&'); |
||
| 216 | $link = HTTP::setGetVar('start', null, $link, '&'); |
||
| 217 | |||
| 218 | return $link; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * List tags and attach links. |
||
| 223 | */ |
||
| 224 | public function UpdateTagsWithLinks() |
||
| 225 | { |
||
| 226 | $tags = $this->UpdateTags(); |
||
| 227 | |||
| 228 | $processed = ArrayList::create(); |
||
| 229 | |||
| 230 | foreach ($tags as $tag) { |
||
| 231 | // Build the link - keep the tag, and date range, but reset month, year and pagination. |
||
| 232 | $link = HTTP::setGetVar('tag', $tag->ID, null, '&'); |
||
| 233 | $link = HTTP::setGetVar('month', null, $link, '&'); |
||
| 234 | $link = HTTP::setGetVar('year', null, $link, '&'); |
||
| 235 | $link = HTTP::setGetVar('start', null, $link, '&'); |
||
| 236 | |||
| 237 | $tag->Link = $link; |
||
| 238 | $processed->push($tag); |
||
| 239 | } |
||
| 240 | |||
| 241 | return $processed; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get the TaxonomyTerm related to the current tag GET parameter. |
||
| 246 | */ |
||
| 247 | public function CurrentTag() |
||
| 248 | { |
||
| 249 | $tagID = $this->request->getVar('tag'); |
||
| 250 | |||
| 251 | if (isset($tagID)) { |
||
| 252 | return TaxonomyTerm::get_by_id(TaxonomyTerm::class, (int)$tagID); |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Extract the available months based on the current query. |
||
| 258 | * Only tag is respected. Pagination and months are ignored. |
||
| 259 | */ |
||
| 260 | public function AvailableMonths() |
||
| 269 | ); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Get the updates based on the current query. |
||
| 274 | */ |
||
| 275 | public function FilteredUpdates($pageSize = 20) |
||
| 276 | { |
||
| 277 | $params = $this->parseParams(); |
||
| 278 | |||
| 279 | $items = $this->Updates( |
||
| 280 | $params['tag'], |
||
| 281 | $params['from'], |
||
| 282 | $params['to'], |
||
| 283 | $params['year'], |
||
| 284 | $params['month'] |
||
| 285 | ); |
||
| 286 | |||
| 287 | // Apply pagination |
||
| 288 | $list = PaginatedList::create($items, $this->getRequest()); |
||
| 289 | $list->setPageLength($pageSize); |
||
| 290 | return $list; |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @return Form |
||
| 295 | */ |
||
| 296 | public function DateRangeForm() |
||
| 330 | } |
||
| 331 | |||
| 332 | public function doDateFilter() |
||
| 344 | } |
||
| 345 | |||
| 346 | public function doDateReset() |
||
| 357 | } |
||
| 358 | |||
| 359 | public function rss() |
||
| 360 | { |
||
| 361 | $rss = RSSFeed::create( |
||
| 362 | $this->Updates()->sort('Created DESC')->limit(20), |
||
| 363 | $this->Link('rss'), |
||
| 364 | $this->getSubscriptionTitle() |
||
| 365 | ); |
||
| 366 | return $rss->outputToBrowser(); |
||
| 367 | } |
||
| 368 | |||
| 369 | View Code Duplication | public function atom() |
|
| 377 | } |
||
| 378 | } |
||
| 379 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths