| Total Complexity | 53 |
| Total Lines | 273 |
| Duplicated Lines | 0 % |
| Changes | 12 | ||
| Bugs | 1 | Features | 0 |
Complex classes like TemplateOverviewPageController 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 TemplateOverviewPageController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class TemplateOverviewPageController extends PageController |
||
| 32 | { |
||
| 33 | protected $myMoreList; |
||
| 34 | protected $totalPageCount = 0; |
||
| 35 | |||
| 36 | private static $url_segment = 'admin/templates'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * folder for example images from root dir |
||
| 40 | * it is recommended to keep this outside of public dir! |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private static $folder_for_example_images = ''; |
||
| 45 | |||
| 46 | private static $allowed_actions = [ |
||
| 47 | 'showmore' => true, |
||
| 48 | 'quicklist' => true, |
||
| 49 | 'listofobjectsused' => true, |
||
| 50 | 'exampleimage' => true, |
||
| 51 | ]; |
||
| 52 | |||
| 53 | private static $base_class = SiteTree::class; |
||
| 54 | |||
| 55 | private static $base_class_provider = SiteTreeDetails::class; |
||
| 56 | |||
| 57 | public function index(HTTPRequest $request = null) |
||
| 58 | { |
||
| 59 | // $this->renderWith(['Sunnysideup\\TemplateOverview\\TemplateOverviewPageController']); |
||
| 60 | return []; |
||
| 61 | } |
||
| 62 | |||
| 63 | public function Content() |
||
| 64 | { |
||
| 65 | return $this->renderWith('Sunnysideup\\TemplateOverview\\Includes\\TemplateOverviewList'); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function showmore($request) |
||
| 69 | { |
||
| 70 | $id = $request->param('ID'); |
||
| 71 | $className = $this->getBaseClass(); |
||
| 72 | /** @var null|DataObject $obj */ |
||
| 73 | $obj = $className::get_by_id((int) $id); |
||
| 74 | if (null !== $obj) { |
||
| 75 | $className = $obj->ClassName; |
||
| 76 | $list = $className::get() |
||
| 77 | ->filter(['ClassName' => $obj->ClassName]) |
||
| 78 | ->limit(300); |
||
| 79 | $this->myMoreList = ArrayList::create(); |
||
| 80 | foreach ($list as $count => $item) { |
||
| 81 | $this->myMoreList->push(clone $this->createPageObject($item, $count)); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | return $this->renderWith('Sunnysideup\\TemplateOverview\\TemplateOverviewPageShowMoreList'); |
||
| 86 | } |
||
| 87 | |||
| 88 | public function getMyMoreList() |
||
| 91 | } |
||
| 92 | |||
| 93 | public function Link($action = null) |
||
| 101 | } |
||
| 102 | |||
| 103 | public function TestTaskLink() |
||
| 106 | } |
||
| 107 | |||
| 108 | public function QuickListLink() |
||
| 109 | { |
||
| 110 | return $this->Link('quicklist'); |
||
| 111 | } |
||
| 112 | |||
| 113 | public function ImagesListLink() |
||
| 114 | { |
||
| 115 | return $this->Link('listofobjectsused/' . Image::class); |
||
| 116 | } |
||
| 117 | |||
| 118 | public function quicklist() |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | public function listofobjectsused($request) |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * returns a list of all (SiteTree) Classes. |
||
| 161 | * |
||
| 162 | * @return ArrayList |
||
| 163 | */ |
||
| 164 | public function ListOfAllClasses() |
||
| 165 | { |
||
| 166 | $providerClass = Injector::inst()->get($this->Config()->get('base_class_provider')); |
||
| 167 | |||
| 168 | $originalList = $providerClass->ListOfAllClasses(); |
||
| 169 | $listOfAllClasses = $providerClass->CountsPerClass(); |
||
| 170 | $newList = ArrayList::create(); |
||
| 171 | foreach ($originalList as $obj) { |
||
| 172 | $count = (int) ($listOfAllClasses[$obj->ClassName] ?? 0); |
||
| 173 | $newList->push($this->createPageObject($obj, $count)); |
||
| 174 | $this->totalPageCount += $count; |
||
| 175 | } |
||
| 176 | |||
| 177 | return $newList; |
||
| 178 | } |
||
| 179 | |||
| 180 | public function TotalTemplateCount(): int |
||
| 181 | { |
||
| 182 | $className = $this->getBaseClass(); |
||
| 183 | |||
| 184 | return count(ClassInfo::subclassesFor($className)) - 1; |
||
| 185 | } |
||
| 186 | |||
| 187 | public function TotalPageCount(): int |
||
| 188 | { |
||
| 189 | $this->ListOfAllClasses(); |
||
| 190 | return $this->totalPageCount; |
||
| 191 | } |
||
| 192 | |||
| 193 | protected function getBaseClass(): string |
||
| 194 | { |
||
| 195 | return $this->Config()->get('base_class'); |
||
| 196 | } |
||
| 197 | |||
| 198 | protected function init() |
||
| 199 | { |
||
| 200 | parent::init(); |
||
| 201 | //important |
||
| 202 | Versioned::set_stage(Versioned::DRAFT); |
||
| 203 | if (Director::is_cli() || Director::isDev() || Permission::check('ADMIN')) { |
||
| 204 | Config::modify()->set(SSViewer::class, 'theme_enabled', false); |
||
| 205 | Requirements::css('sunnysideup/templateoverview: client/css/TemplateOverviewPage.css'); |
||
| 206 | Requirements::css('silverstripe/admin: client/dist/styles/bundle.css'); |
||
| 207 | Requirements::javascript('https://code.jquery.com/jquery-3.6.3.min.js'); |
||
| 208 | Requirements::javascript('sunnysideup/templateoverview: client/javascript/TemplateOverviewPage.js'); |
||
| 209 | if (class_exists(PrettyPhoto::class)) { |
||
| 210 | PrettyPhoto::include_code(); |
||
| 211 | } |
||
| 212 | |||
| 213 | //user_error("It is recommended that you install the Sunny Side Up Pretty Photo Module", E_USER_NOTICE); |
||
| 214 | } else { |
||
| 215 | return Security::permissionFailure($this, 'Please login to access this list'); |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @param SiteTree $obj |
||
| 221 | * @param int $count |
||
| 222 | * |
||
| 223 | * @return ArrayData |
||
| 224 | */ |
||
| 225 | protected function createPageObject($obj, $count) |
||
| 226 | { |
||
| 227 | $parent = $obj->Parent(); |
||
| 228 | $canCreateString = ($obj->canCreate() ? 'Yes' : 'No'); |
||
| 229 | $isAdmin = Permission::check('ADMIN'); |
||
| 230 | $listArray = []; |
||
| 231 | $listArray['Name'] = 1 === $count ? $obj->i18n_singular_name() : $obj->i18n_plural_name(); |
||
| 232 | $listArray['Description'] = DBField::create_field('HTMLText', $obj->hasMethod('i18n_classDescription') ? $obj->i18n_classDescription() : Config::inst()->get($obj->ClassName, 'description')); |
||
| 233 | $listArray['ClassName'] = $obj->ClassName; |
||
| 234 | $listArray['Count'] = $count; |
||
| 235 | $listArray['ID'] = $obj->ID; |
||
| 236 | $listArray['URLSegment'] = $obj->URLSegment ?? 'n/a'; |
||
| 237 | $listArray['ControllerLink'] = $this->Link(); |
||
| 238 | $listArray['Title'] = $obj->getTitle(); |
||
| 239 | $listArray['LiveLink'] = $obj->hasMethod('Link') ? str_replace('?stage=Stage', '', (string) $obj->Link()) : 'please-add-Link-method'; |
||
| 240 | $listArray['PreviewLink'] = $obj->hasMethod('PreviewLink') ? $obj->PreviewLink() : 'please-add-PreviewLink-method'; |
||
| 241 | $listArray['CMSEditLink'] = $obj->hasMethod('CMSEditLink') ? $obj->CMSEditLink() : 'please-add-CMSEditLink-method'; |
||
| 242 | $listArray['MoreCanBeCreated'] = $isAdmin ? $canCreateString : 'Please login as ADMIN to see this value'; |
||
| 243 | $listArray['AllowedChildren'] = ''; |
||
| 244 | $listArray['Breadcrumbs'] = $parent ? $parent->Breadcrumbs() : ''; |
||
| 245 | $listArray['Icon'] = $this->getIcon($obj); |
||
| 246 | if ($obj instanceof SiteTree) { |
||
| 247 | $children = $this->listOfTitles($obj->allowedChildren()); |
||
| 248 | $listArray['AllowedChildren'] = implode(', ', $children); |
||
| 249 | } |
||
| 250 | |||
| 251 | return new ArrayData($listArray); |
||
| 252 | } |
||
| 253 | |||
| 254 | protected function getIcon($obj): string |
||
| 255 | { |
||
| 256 | $icon = ''; |
||
| 257 | $icon = $this->getIconInner($obj, 'getPageIconURL'); |
||
| 258 | if (!$icon) { |
||
| 259 | $icon = $this->getIconInner($obj, 'getIconClass'); |
||
| 260 | if (!$icon) { |
||
| 261 | $icon = $this->getIconInner($obj, 'getIcon'); |
||
| 262 | if (!$icon) { |
||
| 263 | return (string) LeftAndMain::menu_icon_for_class($obj->ClassName); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | if (!$icon) { |
||
| 269 | $icon = 'font-page'; |
||
| 270 | } |
||
| 271 | |||
| 272 | if (false === strpos('.', $icon)) { |
||
| 273 | // $icon = str_replace('font-icon-', 'fa-', $icon); |
||
| 274 | } |
||
| 275 | |||
| 276 | return $icon; |
||
| 277 | } |
||
| 278 | |||
| 279 | protected function getIconInner($obj, $method): ?string |
||
| 289 | } |
||
| 290 | |||
| 291 | protected function listOfTitles($array) |
||
| 292 | { |
||
| 293 | if (is_array($array) && count($array)) { |
||
| 294 | $newArray = []; |
||
| 295 | foreach ($array as $item) { |
||
| 304 | } |
||
| 305 | } |
||
| 306 |
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