Completed
Push — master ( f28713...f81a71 )
by Dominik
14:21
created

SocialBarTwigExtension   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 242
Duplicated Lines 28.51 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 16
Bugs 5 Features 4
Metric Value
wmc 40
c 16
b 5
f 4
lcom 0
cbo 3
dl 69
loc 242
ccs 131
cts 131
cp 1
rs 8.2608

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getName() 0 4 1
A getFunctions() 0 10 1
F getSocialButtons() 47 72 16
B getFacebookButton() 11 25 4
B getTwitterButton() 0 24 5
B getGooglePlusButton() 0 26 4
A getLinkedInButton() 11 21 4
A getXingButton() 0 18 4

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like SocialBarTwigExtension 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 SocialBarTwigExtension, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Azine\SocialBarBundle\Templating;
3
4
use Symfony\Component\DependencyInjection\ContainerInterface;
5
6
class SocialBarTwigExtension extends \Twig_Extension
7
{
8
    protected $container;
9
10
    /**
11
     * Constructor.
12
     *
13
     * @param ContainerInterface $container
14
     */
15 20
    public function __construct(ContainerInterface $container)
16
    {
17 20
        $this->container = $container;
18 20
    }
19
20 1
    public function getName()
21
    {
22 1
        return 'azine_social_bar';
23
    }
24
25 1
    public function getFunctions()
26
    {
27 1
        $functions['socialButtons'   ] = new \Twig_SimpleFunction('socialButtons'   , array($this,'getSocialButtons'), array('is_safe' => array('html')));
0 ignored issues
show
Coding Style Comprehensibility introduced by
$functions was never initialized. Although not strictly required by PHP, it is generally a good practice to add $functions = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
28 1
        $functions['facebookButton'  ] = new \Twig_SimpleFunction('facebookButton'  , array($this,'getFacebookButton'), array('is_safe' => array('html')));
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
29 1
        $functions['twitterButton'   ] = new \Twig_SimpleFunction('twitterButton'   , array($this,'getTwitterButton'), array('is_safe' => array('html')));
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
30 1
        $functions['googlePlusButton'] = new \Twig_SimpleFunction('googlePlusButton', array($this,'getGooglePlusButton'), array('is_safe' => array('html')));
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
31 1
        $functions['xingButton'      ] = new \Twig_SimpleFunction('xingButton'      , array($this,'getXingButton'), array('is_safe' => array('html')));
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
32 1
        $functions['linkedInButton'  ] = new \Twig_SimpleFunction('linkedInButton'  , array($this,'getLinkedInButton'), array('is_safe' => array('html')));
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
33 1
        return $functions;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $functions; (array<string,Twig_SimpleFunction>) is incompatible with the return type declared by the interface Twig_ExtensionInterface::getFunctions of type Twig_SimpleFunction[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
34
    }
35
36
    /**
37
     * Get all the buttons in one row
38
     * @param array  $parameters
39
     * @param string $action
40
     */
41 3
    public function getSocialButtons(array $parameters = array(), $action = "share")
42
    {
43 3
        $commonParams = $parameters;
44 3
        $render_parameters = array();
45 3
        if(array_key_exists('facebook', $commonParams)) unset($commonParams['facebook']);
46 3
        if(array_key_exists('twitter', $commonParams)) unset($commonParams['twitter']);
47 3
        if(array_key_exists('googleplus', $commonParams)) unset($commonParams['googleplus']);
48 3
        if(array_key_exists('xing', $commonParams)) unset($commonParams['xing']);
49 3
        if(array_key_exists('linkedin', $commonParams)) unset($commonParams['linkedin']);
50
51
        // no parameters were defined, keeps default values
52 3 View Code Duplication
        if (!array_key_exists('facebook', $parameters)) {
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...
53 1
            $render_parameters['facebook'] = $parameters;
54
55
            // parameters are defined, overrides default values
56 3
        } elseif (is_array($parameters['facebook'])) {
57 1
            $render_parameters['facebook'] = array_merge($commonParams, $parameters['facebook']);
58
59
        // the button is not displayed
60 1
        } else {
61 1
            $render_parameters['facebook'] = false;
62
        }
63
64 3 View Code Duplication
        if (!array_key_exists('twitter', $parameters)) {
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...
65 1
            $render_parameters['twitter'] = $parameters;
66
67 3
        } elseif (is_array($parameters['twitter'])) {
68 1
            $render_parameters['twitter'] = array_merge($commonParams, $parameters['twitter']);
69
70 1
        } else {
71 1
            $render_parameters['twitter'] = false;
72
        }
73
74 3 View Code Duplication
        if (!array_key_exists('googleplus', $parameters)) {
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...
75 1
            $render_parameters['googleplus'] = $parameters;
76
77 3
        } elseif (is_array($parameters['googleplus'])) {
78 1
            $render_parameters['googleplus'] = array_merge($commonParams, $parameters['googleplus']);
79
80 1
        } else {
81 1
            $render_parameters['googleplus'] = false;
82
        }
83
84 3 View Code Duplication
        if (!array_key_exists('xing', $parameters)) {
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...
85 1
            $render_parameters['xing'] = $parameters;
86
87 3
        } elseif (is_array($parameters['xing'])) {
88 1
            $render_parameters['xing'] = array_merge($commonParams, $parameters['xing']);
89
90 1
        } else {
91 1
            $render_parameters['xing'] = false;
92
        }
93
94
95 3 View Code Duplication
        if (!array_key_exists('linkedin', $parameters)) {
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...
96 1
            $render_parameters['linkedin'] = $parameters;
97
98 3
        } elseif (is_array($parameters['linkedin'])) {
99 1
            $render_parameters['linkedin'] = array_merge($commonParams, $parameters['linkedin']);
100
101 1
        } else {
102 1
            $render_parameters['linkedin'] = false;
103
        }
104
105 3
        $render_parameters['action'] = $action;
106 3
        $render_parameters['width'] = 130;
107 3
        $render_parameters['height'] = 20;
108
109
110
     // get the helper service and display the template
111 3
        return $this->container->get('azine.socialBarHelper')->socialButtons($render_parameters);
112
    }
113
114
    /**
115
     * Render the html for the facebook like button
116
     * => https://developers.facebook.com/docs/reference/plugins/like/
117
     * @param array $parameters
118
     */
119 3
    public function getFacebookButton($parameters = array(), $action = "share")
120
    {
121
        // default values, you can override the values by setting them
122
        $parameters = $parameters + array(
123 3
            'locale' => 'en_US',
124 3
            'send' => false,
125 3
            'width' => 130,
126 3
            'showFaces' => false,
127 3
            'layout' => 'button_count',
128 3
            );
129
130 3 View Code Duplication
        if ($action == "share") {
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...
131 1
            $parameters['url'] = array_key_exists('url', $parameters) ? $parameters['url'] : null;
132 1
            $parameters['action'] = 'fb-like';
133
134 3
        } elseif ($action == "follow") {
135 1
            $parameters['url'] = $this->container->getParameter("azine_social_bar_fb_profile_url");
136 1
            $parameters['action'] = "fb-follow";
137
138 1
        } else {
139 1
            throw new \Exception("Unknown social action. Only 'share' and 'follow' are known at the moment.");
140
        }
141
142 2
        return $this->container->get('azine.socialBarHelper')->facebookButton($parameters);
143
    }
144
145
    /**
146
     * Render the html for the twitter button
147
     * =>
148
     * @param array $parameters
149
     */
150 3
    public function getTwitterButton($parameters = array(), $action = "share")
151
    {
152
        $parameters = $parameters + array(
153 3
            'url' => array_key_exists('url', $parameters) ? $parameters['url'] : null,
154 3
            'locale' => 'en',
155 3
            'message' => 'I want to share that page with you',
156 3
            'text' => 'Tweet',
157 3
            'via' => $this->container->getParameter("azine_social_bar_twitter_username"),
158 3
            'tag' => array_key_exists('tag', $parameters) ? $parameters['tag'] : $this->container->getParameter("azine_social_bar_twitter_username"),
159 3
            );
160 3
        if ($action == "share") {
161 1
            $parameters['actionClass'] = "twitter-share-button";
162 1
            $parameters['action'] = "share";
163
164 3
        } elseif ($action == "follow") {
165 1
            $parameters['actionClass'] = "twitter-follow-button";
166 1
            $parameters['action'] = $this->container->getParameter("azine_social_bar_twitter_username");
167
168 1
        } else {
169 1
            throw new \Exception("Unknown social action. Only 'share' and 'follow' are known at the moment.");
170
        }
171
172 2
        return $this->container->get('azine.socialBarHelper')->twitterButton($parameters);
173
    }
174
175
    /**
176
     * Render the html for the Google+ button
177
     * =>
178
     * @param array $parameters
179
     */
180 3
    public function getGooglePlusButton($parameters = array(), $action = "share")
181
    {
182
        $parameters = $parameters + array(
183 3
            'locale' => 'en',
184 3
            'size' => 'medium',
185 3
            'annotation' => 'bubble',
186 3
            'width' => 130,
187 3
            'height' => 20,
188 3
        );
189
190 3
        if ($action == "share") {
191 1
            $parameters['url'] = array_key_exists('url', $parameters) ? $parameters['url'] : null;
192 1
            $parameters['rel'] = 'author';
193 1
            $parameters['action'] = 'g-plusone';
194
195 3
        } elseif ($action == "follow") {
196 1
            $parameters['url'] = $this->container->getParameter("azine_social_bar_google_plus_profile_url");
197 1
            $parameters['rel'] = "publisher";
198 1
            $parameters['action'] = "g-follow";
199
200 1
        } else {
201 1
            throw new \Exception("Unknown social action. Only 'share' and 'follow' are known at the moment.");
202
        }
203
204 2
        return $this->container->get('azine.socialBarHelper')->googlePlusButton($parameters);
205
    }
206
207 3
    public function getLinkedInButton($parameters = array(), $action = "share")
208
    {
209
        $parameters = $parameters + array(
210 3
            'locale' => 'en',
211 3
            'counterLocation' => 'right',
212 3
        );
213
214 3 View Code Duplication
        if ($action == "share") {
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...
215 1
            $parameters['action'] = 'IN/Share';
216 1
            $parameters['url'] = array_key_exists('url', $parameters) ? $parameters['url'] : null;
217
218 3
        } elseif ($action == "follow") {
219 1
            $parameters['action'] = 'IN/FollowCompany';
220 1
            $parameters['companyId'] = $this->container->getParameter("azine_social_bar_linked_in_company_id");
221
222 1
        } else {
223 1
            throw new \Exception("Unknown social action. Only 'share' and 'follow' are known at the moment.");
224
        }
225
226 2
        return $this->container->get('azine.socialBarHelper')->linkedInButton($parameters);
227
    }
228
229 3
    public function getXingButton($parameters = array(), $action = "share")
230
    {
231
        $parameters = $parameters + array(
232 3
            'locale' => 'en',
233 3
            'action' => 'XING/Share',
234 3
            'counterLocation' => 'right',
235 3
        );
236
237 3
        if ($action == "share") {
238 1
            $parameters['url'] = array_key_exists('url', $parameters) ? $parameters['url'] : null;
239 3
        } elseif ($action == "follow") {
240 1
            $parameters['url'] = $this->container->getParameter("azine_social_bar_xing_profile_url");
241 1
        } else {
242 1
            throw new \Exception("Unknown social action. Only 'share' and 'follow' are known at the moment.");
243
        }
244
245 2
        return $this->container->get('azine.socialBarHelper')->xingButton($parameters);
246
    }
247
}
248