Completed
Push — master ( 868c6d...89e3b0 )
by Dominik
03:47
created

SocialBarTwigExtension   B

Complexity

Total Complexity 41

Size/Duplication

Total Lines 250
Duplicated Lines 27.6 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.76%

Importance

Changes 15
Bugs 5 Features 4
Metric Value
wmc 41
c 15
b 5
f 4
lcom 1
cbo 3
dl 69
loc 250
ccs 131
cts 134
cp 0.9776
rs 8.2769

10 Methods

Rating   Name   Duplication   Size   Complexity  
A initRuntime() 0 3 1
A __construct() 0 4 1
A getName() 0 4 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
A getFunctions() 0 10 1

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