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:
1 | <?php |
||
8 | use SilverStripe\ORM\FieldType\DBField; |
||
9 | use SilverStripe\Forms\LiteralField; |
||
10 | use SunnySideUp\ShareThis\ShareThisOptions; |
||
11 | use SunnySideUp\ShareThis\ShareThisSTE; |
||
12 | use SilverStripe\ORM\DB; |
||
13 | use SilverStripe\ORM\DataObject; |
||
14 | use SilverStripe\Security\PermissionProvider; |
||
15 | |||
16 | /** |
||
17 | * @author nicolaas[at]sunnysideup.co.nz |
||
18 | * @description: list of Share This Options that can be shown |
||
19 | * @todo finish onAfterWrite and delete objects |
||
20 | */ |
||
21 | class ShareThisDataObject extends DataObject implements PermissionProvider |
||
22 | { |
||
23 | private static $permission_framework = array( |
||
24 | "SOCIAL_MEDIA" => array( |
||
25 | 'name' => "Social Media Management", |
||
26 | 'category' => "Social Media", |
||
27 | 'help' => 'Edit relationships, links and data of various social media platforms.', |
||
28 | 'sort' => 0 |
||
29 | ) |
||
30 | ); |
||
31 | |||
32 | private static $db = array( |
||
33 | 'Title' => 'Varchar(20)', |
||
34 | 'IncludeThisIcon' => 'Boolean', |
||
35 | 'IncludeThisIconInExtendedList' => 'Boolean', |
||
36 | 'Sort' => 'Int' |
||
37 | ); |
||
38 | |||
39 | private static $has_one = array( |
||
40 | 'AlternativeIcon' => Image::class |
||
41 | ); |
||
42 | |||
43 | private static $casting = array( |
||
44 | 'Icon' => 'HTMLText', |
||
45 | 'IncludeThisIconNice' => 'Varchar', |
||
46 | 'IncludeThisIconInExtendedListNice' => 'IncludeThisIconInExtendedList' |
||
47 | ); |
||
48 | |||
49 | private static $field_labels = array( |
||
50 | 'Title' => 'Name', |
||
51 | 'IncludeThisIcon' => 'Include in main list', |
||
52 | 'IncludeThisIconNice' => 'Include in primary list', |
||
53 | 'IncludeThisIconInExtendedList' => 'Include in secondary list', |
||
54 | 'IncludeThisIconInExtendedListNice' => 'Include in secondary list', |
||
55 | 'Sort' => 'Sort Index (lower numbers shown first)', |
||
56 | 'AlternativeIcon' => 'Optional Alternative Icon (can be any size, a 32px by 32px square is recommended)' |
||
57 | ); |
||
58 | |||
59 | private static $summary_fields = array( |
||
60 | 'Icon' => 'Icon', |
||
61 | 'Title' => 'Name', |
||
62 | 'IncludeThisIconNice' => 'IncludeThisIcon' |
||
63 | //'IncludeThisIconInExtendedListNice' => 'IncludeThisIconInExtendedList' |
||
64 | ); |
||
65 | |||
66 | private static $singular_name = 'Icon to share this page'; |
||
67 | |||
68 | private static $plural_name = 'Icons to share this page'; |
||
69 | |||
70 | private static $default_sort = 'IncludeThisIcon DESC, IncludeThisIconInExtendedList ASC, Sort ASC, Title ASC'; |
||
71 | |||
72 | public function providePermissions() |
||
76 | |||
77 | public function canView($member = null) |
||
81 | |||
82 | public function canCreate($member = null, $context = []) |
||
86 | |||
87 | public function canEdit($member = null) |
||
91 | |||
92 | public function canDelete($member = null) |
||
104 | |||
105 | public function IncludeThisIconInExtendedListNice() |
||
113 | |||
114 | public function Icon() |
||
127 | |||
128 | public function getCMSFields() |
||
137 | |||
138 | public function onAfterWrite() |
||
144 | |||
145 | public function validate() |
||
161 | |||
162 | public function requireDefaultRecords() |
||
163 | { |
||
164 | parent::requireDefaultRecords(); |
||
165 | $actualArray = ShareThisOptions::get_general_data(); |
||
166 | Config::inst()->update(ShareThisSTE::class, "included_icons", array()); |
||
167 | Config::inst()->update(ShareThisSTE::class, "excluded_icons", array()); |
||
168 | ShareThisOptions::set_general_data(null); |
||
169 | $fullArray = ShareThisOptions::get_general_data(); |
||
170 | foreach ($fullArray as $key) { |
||
171 | $object = ShareThisDataObject::get()->filter('Title', $key); |
||
172 | if (! $object->exists()) { |
||
173 | $object = new ShareThisDataObject(); |
||
174 | $object->Title = $key; |
||
175 | $style = 'excluded'; |
||
176 | $object->IncludeThisIcon = false; |
||
177 | if (in_array($key, $actualArray)) { |
||
178 | $object->IncludeThisIcon = true; |
||
179 | $style = 'included'; |
||
180 | } |
||
181 | $object->write(); |
||
182 | DB::alteration_message("Added Bookmark Icon for $key ($style)", 'created'); |
||
183 | } |
||
184 | } |
||
185 | $inc = Config::inst()->get(ShareThisSTE::class, "included_icons"); |
||
186 | View Code Duplication | foreach ($inc as $key) { |
|
187 | $object = ShareThisDataObject::get()->filter(array('Title' => $key, 'IncludeThisIcon' => 0)); |
||
188 | if ($object->exists()) { |
||
189 | $object = $object->first(); |
||
190 | $object->IncludeThisIcon = true; |
||
191 | $object->write(); |
||
192 | DB::alteration_message("Updated inclusion for $key", 'created'); |
||
193 | } |
||
194 | } |
||
207 |
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
@return
annotation as described here.