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 |
||
9 | use SilverStripe\View\Requirements; |
||
10 | use SilverStripe\Core\Config\Config; |
||
11 | use SunnySideUp\ShareThis\SocialNetworkingLinksDataObject; |
||
12 | use SilverStripe\CMS\Model\SiteTreeExtension; |
||
13 | |||
14 | /** |
||
15 | * Add a field to each SiteTree object and it's subclasses to enable "follow us on ...", this can be a blog, twitter, facebook or whatever else. |
||
16 | * it uses the SocialNetworkingLinksDataObject to get a list of icons. |
||
17 | * @author nicolaas [at] sunnysideup.co.nz |
||
18 | * @todo fix populateDefaults to make sure SiteConfig table is built first |
||
19 | */ |
||
20 | class SocialNetworksSTE extends SiteTreeExtension |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * Use the font-awesome icon collection? |
||
25 | * @var Boolean |
||
26 | */ |
||
27 | private static $use_font_awesome = false; |
||
28 | |||
29 | /** |
||
30 | * list of sitetree extending classnames where |
||
31 | * the ShareThis functionality should be included |
||
32 | * @var Array |
||
33 | */ |
||
34 | private static $always_include_in = array(); |
||
35 | |||
36 | /** |
||
37 | * list of sitetree extending classnames where |
||
38 | * the ShareThis functionality should NEVER be included |
||
39 | * @var Array |
||
40 | */ |
||
41 | private static $never_include_in = array(); |
||
42 | |||
43 | private static $db = array( |
||
44 | 'HasSocialNetworkingLinks' => 'Boolean' |
||
45 | ); |
||
46 | |||
47 | public function updateCMSFields(FieldList $fields) |
||
59 | |||
60 | public function ShowSocialNetworks() |
||
71 | |||
72 | public function SocialNetworks() |
||
80 | |||
81 | private function applyToOwnerClass() |
||
82 | { |
||
83 | $always = Config::inst()->get(SocialNetworksSTE::class, "always_include_in"); |
||
84 | $never = Config::inst()->get(SocialNetworksSTE::class, "never_include_in"); |
||
85 | if (count($always) == 0 && count($never) == 0) { |
||
86 | return true; |
||
87 | } |
||
88 | View Code Duplication | if (count($never) && count($always) == 0) { |
|
89 | if (in_array($this->owner->ClassName, $never)) { |
||
90 | return false; |
||
91 | } |
||
92 | return true; |
||
93 | } |
||
94 | View Code Duplication | if (count($always) && count($never) == 0) { |
|
95 | if (in_array($this->owner->ClassName, $always)) { |
||
96 | return true; |
||
97 | } |
||
98 | return false; |
||
99 | } |
||
100 | View Code Duplication | if (count($never) && count($always)) { |
|
101 | if (in_array($this->owner->ClassName, $never)) { |
||
102 | return false; |
||
113 |
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.