| Total Complexity | 40 |
| Total Lines | 341 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Slide 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 Slide, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Slide extends DataObject implements PermissionProvider, SlideInterface |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * The Create permission |
||
| 28 | */ |
||
| 29 | const SLIDE_CREATE = 'SLIDE_CREATE'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The Publish permission |
||
| 33 | */ |
||
| 34 | const SLIDE_PUBLISH = 'SLIDE_PUBLISH'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The Edit permission |
||
| 38 | */ |
||
| 39 | const SLIDE_EDIT = 'SLIDE_EDIT'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The Delete permission |
||
| 43 | */ |
||
| 44 | const SLIDE_DELETE = 'SLIDE_DELETE'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private static $singular_name = 'Slide'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private static $plural_name = 'Slides'; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private static $table_name = 'Slide'; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | private static $db = [ |
||
| 65 | 'Title' => 'Varchar(255)', |
||
| 66 | 'ShowTitle' => 'Boolean', |
||
| 67 | 'Content' => 'HTMLText', |
||
| 68 | 'SortOrder' => 'Int', |
||
| 69 | ]; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | private static $has_one = [ |
||
| 75 | 'Page' => \Page::class, |
||
| 76 | ]; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | private static $extensions = [ |
||
| 82 | Versioned::class, |
||
| 83 | InheritedPermissionsExtension::class, |
||
| 84 | ]; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | private static $versioning = [ |
||
| 90 | "Stage", "Live", |
||
| 91 | ]; |
||
| 92 | |||
| 93 | |||
| 94 | /** |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | private static $default_sort = 'SortOrder'; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Adds Publish button to SlideImage record |
||
| 101 | * |
||
| 102 | * @var bool |
||
| 103 | */ |
||
| 104 | private static $versioned_gridfield_extensions = true; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var array |
||
| 108 | */ |
||
| 109 | private static $searchable_fields = [ |
||
| 110 | 'Title', |
||
| 111 | 'Content', |
||
| 112 | ]; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var array |
||
| 116 | */ |
||
| 117 | private static $summary_fields = [ |
||
| 118 | 'SlideType', |
||
| 119 | 'Title', |
||
| 120 | ]; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @return FieldList |
||
| 124 | */ |
||
| 125 | public function getCMSFields() |
||
| 126 | { |
||
| 127 | $this->beforeUpdateCMSFields(function (FieldList $fields) { |
||
| 128 | $viewInherit = $fields->dataFieldByName('CanViewType'); |
||
| 129 | $editInherit = $fields->dataFieldByName('CanEditType'); |
||
| 130 | |||
| 131 | $fields->removeByName([ |
||
| 132 | 'Title', |
||
| 133 | 'ShowTitle', |
||
| 134 | 'SortOrder', |
||
| 135 | 'CanViewType', |
||
| 136 | 'CanEditType', |
||
| 137 | 'PageID', |
||
| 138 | ]); |
||
| 139 | |||
| 140 | $fields->addFieldToTab( |
||
| 141 | 'Root.Main', |
||
| 142 | TextCheckboxGroupField::create(), |
||
| 143 | 'Content' |
||
| 144 | ); |
||
| 145 | |||
| 146 | $fields->addFieldsToTab( |
||
| 147 | 'Root.Permissions', |
||
| 148 | [ |
||
| 149 | $viewInherit, |
||
| 150 | $editInherit, |
||
| 151 | ] |
||
| 152 | ); |
||
| 153 | }); |
||
| 154 | |||
| 155 | return parent::getCMSFields(); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return array |
||
| 160 | */ |
||
| 161 | public function providePermissions() |
||
| 162 | { |
||
| 163 | return [ |
||
| 164 | static::SLIDE_CREATE => [ |
||
| 165 | 'name' => _t(__CLASS__ . '.PERMISSION_CREATE_DESCRIPTION', 'Create Slides'), |
||
| 166 | 'help' => _t(__CLASS__ . '.PERMISSION_CREATE_HELP', 'Allow creating slides'), |
||
| 167 | 'category' => _t(__CLASS__ . '.PERMISSIONS_CATEGORY', 'FlexSlider Permissions'), |
||
| 168 | 'sort' => 100, |
||
| 169 | ], |
||
| 170 | static::SLIDE_PUBLISH => [ |
||
| 171 | 'name' => _t(__CLASS__ . '.PERMISSION_PUBLISH_DESCRIPTION', 'Publish Slides'), |
||
| 172 | 'help' => _t(__CLASS__ . '.PERMISSION_PUBLISH_HELP', 'Allow publishing slides'), |
||
| 173 | 'category' => _t(__CLASS__ . '.PERMISSIONS_CATEGORY', 'FlexSlider Permissions'), |
||
| 174 | 'sort' => 100, |
||
| 175 | ], |
||
| 176 | static::SLIDE_EDIT => [ |
||
| 177 | 'name' => _t(__CLASS__ . '.PERMISSION_EDIT_DESCRIPTION', 'Edit Slides'), |
||
| 178 | 'help' => _t(__CLASS__ . '.PERMISSION_EDIT_HELP', 'Allow editing slides'), |
||
| 179 | 'category' => _t(__CLASS__ . '.PERMISSIONS_CATEGORY', 'FlexSlider Permissions'), |
||
| 180 | 'sort' => 100, |
||
| 181 | ], |
||
| 182 | static::SLIDE_DELETE => [ |
||
| 183 | 'name' => _t(__CLASS__ . '.PERMISSION_DELETE_DESCRIPTION', 'Delete Slides'), |
||
| 184 | 'help' => _t(__CLASS__ . '.PERMISSION_DELETE_HELP', 'Allow deleting slides'), |
||
| 185 | 'category' => _t(__CLASS__ . '.PERMISSIONS_CATEGORY', 'FlexSlider Permissions'), |
||
| 186 | 'sort' => 100, |
||
| 187 | ], |
||
| 188 | ]; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param null $member |
||
| 193 | * @param array $context |
||
| 194 | * @return bool|int |
||
| 195 | */ |
||
| 196 | public function canCreate($member = null, $context = []) |
||
| 197 | { |
||
| 198 | if (!$member) { |
||
| 199 | $member = Security::getCurrentUser(); |
||
| 200 | } |
||
| 201 | |||
| 202 | // Standard mechanism for accepting permission changes from extensions |
||
| 203 | $extended = $this->extendedCan(__FUNCTION__, $member, $context); |
||
| 204 | if ($extended !== null) { |
||
| 205 | return $extended; |
||
| 206 | } |
||
| 207 | |||
| 208 | // Check permission |
||
| 209 | if ($member && Permission::checkMember($member, "ADMIN") || Permission::checkMember($member, static::SLIDE_CREATE)) { |
||
| 210 | return true; |
||
| 211 | } |
||
| 212 | |||
| 213 | // This doesn't necessarily mean we are creating a root page, but that |
||
| 214 | // we don't know if there is a parent, so default to this permission |
||
| 215 | return SiteConfig::current_site_config()->canCreateTopLevel($member); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param null $member |
||
| 220 | * @return bool|int |
||
| 221 | */ |
||
| 222 | public function canPublish($member = null) |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param null $member |
||
| 243 | * @return bool|int |
||
| 244 | */ |
||
| 245 | public function canEdit($member = null) |
||
| 246 | { |
||
| 247 | if (!$member) { |
||
| 248 | $member = Security::getCurrentUser(); |
||
| 249 | } |
||
| 250 | |||
| 251 | // Standard mechanism for accepting permission changes from extensions |
||
| 252 | $extended = $this->extendedCan('canEdit', $member); |
||
| 253 | if ($extended !== null) { |
||
| 254 | return $extended; |
||
| 255 | } |
||
| 256 | |||
| 257 | // Default permissions |
||
| 258 | if (Permission::checkMember($member, "ADMIN") || Permission::checkMember($member, static::SLIDE_EDIT)) { |
||
| 259 | return true; |
||
| 260 | } |
||
| 261 | |||
| 262 | |||
| 263 | return false; |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param null $member |
||
| 268 | * @return bool|null |
||
| 269 | */ |
||
| 270 | public function canView($member = null) |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param null $member |
||
| 318 | * @return bool|int |
||
| 319 | */ |
||
| 320 | public function canDelete($member = null) |
||
| 321 | { |
||
| 322 | if (!$member) { |
||
| 323 | $member = Security::getCurrentUser(); |
||
| 324 | } |
||
| 325 | |||
| 326 | // Standard mechanism for accepting permission changes from extensions |
||
| 327 | $extended = $this->extendedCan('canDelete', $member); |
||
| 328 | if ($extended !== null) { |
||
| 329 | return $extended; |
||
| 330 | } |
||
| 331 | |||
| 332 | if (!$member) { |
||
| 333 | return false; |
||
| 334 | } |
||
| 335 | |||
| 336 | // Default permission check |
||
| 337 | if (Permission::checkMember($member, ["ADMIN", static::SLIDE_DELETE])) { |
||
| 338 | return true; |
||
| 339 | } |
||
| 340 | |||
| 341 | return false; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Stub method to get the site config, unless the current class can provide an alternate. |
||
| 346 | * |
||
| 347 | * @return SiteConfig |
||
| 348 | */ |
||
| 349 | public function getSiteConfig() |
||
| 350 | { |
||
| 351 | $configs = $this->invokeWithExtensions('alternateSiteConfig'); |
||
| 352 | foreach (array_filter($configs) as $config) { |
||
| 353 | return $config; |
||
| 354 | } |
||
| 355 | |||
| 356 | return SiteConfig::current_site_config(); |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @return string |
||
| 361 | */ |
||
| 362 | public function getSlideType() |
||
| 365 | } |
||
| 366 | } |
||
| 367 |
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