Completed
Pull Request — master (#7)
by
unknown
13:44
created

SocialNetworksSTE   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 93
Duplicated Lines 24.73 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 23
loc 93
rs 10
c 0
b 0
f 0
wmc 21
lcom 0
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A updateCMSFields() 0 12 3
A ShowSocialNetworks() 0 11 3
A SocialNetworks() 0 8 2
C applyToOwnerClass() 23 31 13

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace SunnySideUp\ShareThis;
4
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\Forms\HeaderField;
7
use SilverStripe\Forms\CheckboxField;
8
use SilverStripe\Forms\LiteralField;
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)
48
    {
49
        if ($this->applyToOwnerClass()) {
50
            $config = $this->owner->getSiteConfig();
51
            if (! $config->AlwaysIncludeSocialNetworkingLinks) {
52
                $fields->addFieldToTab('Root.SocialMedia', new HeaderField('SocialNetworksHeader', 'Ask visitors to JOIN YOU on your social media'));
53
                $fields->addFieldToTab('Root.SocialMedia', new CheckboxField('HasSocialNetworkingLinks', 'Show Join Us on our Social Networks Links on this Page (e.g. follow us on Twitter) - make sure to specify social networking links!'));
54
            }
55
            $fields->addFieldToTab('Root.SocialMedia', new LiteralField('LinkToSiteConfigSocialMedia', "<p>There are more social media settings in the <a href=\"{$config->CMSEditLink()}\">Site Config</a>.</p>"));
56
        }
57
        return $fields;
58
    }
59
60
    public function ShowSocialNetworks()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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.

Loading history...
61
    {
62
        if ($this->applyToOwnerClass()) {
63
            $config = $this->owner->getSiteConfig();
64
            if ($config->AlwaysIncludeSocialNetworkingLinks) {
65
                return true;
66
            }
67
            return $this->owner->HasSocialNetworkingLinks;
68
        }
69
        return false;
70
    }
71
72
    public function SocialNetworks()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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.

Loading history...
73
    {
74
        Requirements::themedCSS('SocialNetworking', "sharethis");
75
        if (Config::inst()->get(SocialNetworksSTE::class, "use_font_awesome")) {
76
            Requirements::css("//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css");
77
        }
78
        return SocialNetworkingLinksDataObject::get();
79
    }
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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
            if (in_array($this->owner->ClassName, $never)) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !in_array($this->...er->ClassName, $never);.
Loading history...
90
                return false;
91
            }
92
            return true;
93
        }
94 View Code Duplication
        if (count($always) && count($never) == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
            if (in_array($this->owner->ClassName, $always)) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return in_array($this->o...r->ClassName, $always);.
Loading history...
96
                return true;
97
            }
98
            return false;
99
        }
100 View Code Duplication
        if (count($never) && count($always)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
            if (in_array($this->owner->ClassName, $never)) {
102
                return false;
103
            }
104
            if (in_array($this->owner->ClassName, $always)) {
0 ignored issues
show
Unused Code introduced by
This if statement and the following return statement are superfluous as you return always true.
Loading history...
105
                return true;
106
            }
107
            //exception... if dev sets both always and never
108
            //then the ones not set will be included by default.
109
            return true;
110
        }
111
    }
112
}
113