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 ShareThisSTE 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ShareThisSTE, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | use SilverStripe\Forms\LiteralField; |
||
| 10 | use SunnySideUp\ShareThis\ShareThisOptions; |
||
| 11 | use SilverStripe\View\Requirements; |
||
| 12 | use SilverStripe\Core\Config\Config; |
||
| 13 | use SilverStripe\Core\Convert; |
||
| 14 | use SilverStripe\View\ArrayData; |
||
| 15 | use SilverStripe\ORM\ArrayList; |
||
| 16 | use SunnySideUp\ShareThis\ShareThisDataObject; |
||
| 17 | use SilverStripe\CMS\Model\SiteTreeExtension; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Add a field to each SiteTree object and it's subclasses to enable Share icons. |
||
| 21 | * @author nicolaas [at] sunnysideup.co.nz |
||
| 22 | * @inspiration: Silverstripe Original Module - full credits to them. We made our own to improve their module |
||
| 23 | * @todo fix populateDefaults to make sure SiteConfig table is built first |
||
| 24 | */ |
||
| 25 | class ShareThisSTE extends SiteTreeExtension |
||
| 26 | { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Use the font-awesome icon collection? |
||
| 30 | * @var Boolean |
||
| 31 | */ |
||
| 32 | private static $use_font_awesome = true; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * list of sitetree extending classnames where |
||
| 36 | * the ShareThis functionality should be included |
||
| 37 | * @var Array |
||
| 38 | */ |
||
| 39 | private static $always_include_in = array(); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * list of sitetree extending classnames where |
||
| 43 | * the ShareThis functionality should NEVER be included |
||
| 44 | * @var Array |
||
| 45 | */ |
||
| 46 | private static $never_include_in = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * use BW icons |
||
| 50 | * @var boolean |
||
| 51 | */ |
||
| 52 | private static $use_bw_effect = false; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * specify icons to be included, if left empty, this variable will be ignored |
||
| 56 | * We have this variable so that you can setup a bunch of default icons |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | private static $included_icons = array(); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * specify icons to be excluded, if left empty, this variable will be ignored |
||
| 63 | * We have this variable so that you can setup a bunch of default icons |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | private static $excluded_icons = array(); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * standard SS method |
||
| 70 | * @var Array |
||
| 71 | **/ |
||
| 72 | private static $db = array( |
||
| 73 | 'ShareIcons' => 'Boolean' |
||
| 74 | ); |
||
| 75 | |||
| 76 | public function updateCMSFields(FieldList $fields) |
||
| 98 | |||
| 99 | public function ShowShareIcons() |
||
| 109 | |||
| 110 | public function ShareIcons() |
||
| 115 | |||
| 116 | public function ShareAllExpandedList() |
||
| 123 | |||
| 124 | public function IncludeShareAll() |
||
| 129 | |||
| 130 | public function ShareAll() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * eturns array |
||
| 139 | */ |
||
| 140 | protected function makeShareIcons($bookmarks) |
||
| 185 | |||
| 186 | protected function makeBookmarks($field) |
||
| 207 | |||
| 208 | private function applyToOwnerClass() |
||
| 209 | { |
||
| 210 | $always = Config::inst()->get(ShareThisSTE::class, "always_include_in"); |
||
| 211 | $never = Config::inst()->get(ShareThisSTE::class, "never_include_in"); |
||
| 212 | if (count($always) == 0 && count($never) == 0) { |
||
| 213 | return true; |
||
| 214 | View Code Duplication | } elseif (count($never) && count($always) == 0) { |
|
| 215 | if (in_array($this->owner->ClassName, $never)) { |
||
| 216 | return false; |
||
| 217 | } |
||
| 218 | return true; |
||
| 219 | } elseif (count($always) && count($never) == 0) { |
||
| 220 | if (in_array($this->owner->ClassName, $always)) { |
||
| 221 | return true; |
||
| 222 | } |
||
| 223 | return false; |
||
| 239 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.