Page   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 271
Duplicated Lines 11.81 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 34
lcom 1
cbo 0
dl 32
loc 271
rs 9.68
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A normalize() 0 4 1
A __isset() 0 12 2
A __get() 16 16 3
A __call() 16 16 3
B shareCount() 0 42 10
A getUrl() 0 4 1
A getTitle() 0 4 1
A getText() 0 4 1
A getImage() 0 4 1
A getIcon() 0 4 1
A getTwitterUser() 0 4 1
A get() 0 22 5
A getConfig() 0 4 2

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 SocialLinks;
4
5
/**
6
 * @method html()
7
 * @method openGraph()
8
 * @method schema()
9
 * @method twitterCard()
10
 */
11
class Page
12
{
13
    protected $config = array();
14
    protected $providers = array();
15
    protected $metas = array();
16
    protected $info = array(
17
        'url' => null,
18
        'title' => null,
19
        'text' => null,
20
        'image' => null,
21
        'icon' => null,
22
        'twitterUser' => null,
23
    );
24
25
    /**
26
     * Constructor.
27
     *
28
     * @param array $info   The page info. Only url, title, text, image, icon and twitterUser fields are available
29
     * @param array $config Configuration options
30
     */
31
    public function __construct(array $info, array $config = array())
32
    {
33
        if (array_diff_key($info, $this->info)) {
34
            throw new \Exception('Only the following fields are available:'.implode(',', array_keys($this->info)));
35
        }
36
37
        $this->info = array_map('static::normalize', $info + $this->info);
38
39
        $this->config = $config;
40
    }
41
42
    /**
43
     * Normalize value before save it:
44
     * - remove html tags
45
     * - remove line-ending and multiple spaces
46
     * - remove spaces around
47
     * - decode escaped html entities.
48
     *
49
     * @param string
50
     *
51
     * @return string
52
     */
53
    protected static function normalize($value)
54
    {
55
        return trim(strip_tags(htmlspecialchars_decode(preg_replace('/\s+/', ' ', $value))));
56
    }
57
58
    /**
59
     * Magic method to check if a provider exists.
60
     *
61
     * @param string $key
62
     *
63
     * @return bool
64
     */
65
    public function __isset($key)
66
    {
67
        $key = strtolower($key);
68
69
        if (isset($this->providers[$key])) {
70
            return true;
71
        }
72
73
        $class = 'SocialLinks\\Providers\\'.ucfirst($key);
74
75
        return class_exists($class);
76
    }
77
78
    /**
79
     * Magic method to instantiate and return providers in lazy mode.
80
     *
81
     * @param string $key The provider name
82
     *
83
     * @throws \Exception if the provider does not exists
84
     *
85
     * @return Providers\ProviderInterface
86
     */
87 View Code Duplication
    public function __get($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
88
    {
89
        $key = strtolower($key);
90
91
        if (isset($this->providers[$key])) {
92
            return $this->providers[$key];
93
        }
94
95
        $class = 'SocialLinks\\Providers\\'.ucfirst($key);
96
97
        if (class_exists($class)) {
98
            return $this->providers[$key] = new $class($this);
99
        }
100
101
        throw new \Exception("The provider $key does not exists");
102
    }
103
104
    /**
105
     * Magic method to instantiate and return metas in lazy mode.
106
     *
107
     * @param string $key       The meta collection name
108
     * @param array  $arguments The arguments passed to the method
109
     *
110
     * @throws \Exception if the meta does not exists
111
     *
112
     * @return Metas\MetaInterface
113
     */
114 View Code Duplication
    public function __call($key, $arguments)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
115
    {
116
        $key = strtolower($key);
117
118
        if (isset($this->metas[$key])) {
119
            return $this->metas[$key];
120
        }
121
122
        $class = 'SocialLinks\\Metas\\'.ucfirst($key);
123
124
        if (class_exists($class)) {
125
            return $this->metas[$key] = new $class($this);
126
        }
127
128
        throw new \Exception("The meta $key does not exists");
129
    }
130
131
    /**
132
     * Preload the counter.
133
     *
134
     * @param array $providers
135
     */
136
    public function shareCount(array $providers)
137
    {
138
        if (count($providers) < 2) {
139
            return;
140
        }
141
142
        $connections = array();
143
        $curl = curl_multi_init();
144
145
        foreach ($providers as $provider) {
146
            $request = $this->$provider->shareCountRequest();
147
148
            if ($request !== null) {
149
                $connections[$provider] = $request;
150
                curl_multi_add_handle($curl, $request);
151
            } else {
152
                $this->$provider->shareCount = null;
153
            }
154
        }
155
156
        do {
157
            $return = curl_multi_exec($curl, $active);
158
        } while ($return === CURLM_CALL_MULTI_PERFORM);
159
160
        while ($active && $return === CURLM_OK) {
161
            if (curl_multi_select($curl) === -1) {
162
                usleep(100);
163
            }
164
165
            do {
166
                $return = curl_multi_exec($curl, $active);
167
            } while ($return === CURLM_CALL_MULTI_PERFORM);
168
        }
169
170
        foreach ($connections as $provider => $request) {
171
            $this->$provider->shareCount = $this->$provider->shareCount(curl_multi_getcontent($request));
172
173
            curl_multi_remove_handle($curl, $request);
174
        }
175
176
        curl_multi_close($curl);
177
    }
178
179
    /**
180
     * Gets the page url.
181
     *
182
     * @return string|null
183
     */
184
    public function getUrl()
185
    {
186
        return $this->info['url'];
187
    }
188
189
    /**
190
     * Gets the page title.
191
     *
192
     * @return string|null
193
     */
194
    public function getTitle()
195
    {
196
        return $this->info['title'];
197
    }
198
199
    /**
200
     * Gets the page text description.
201
     *
202
     * @return string|null
203
     */
204
    public function getText()
205
    {
206
        return $this->info['text'];
207
    }
208
209
    /**
210
     * Gets the page image.
211
     *
212
     * @return string|null
213
     */
214
    public function getImage()
215
    {
216
        return $this->info['image'];
217
    }
218
219
    /**
220
     * Gets the page icon.
221
     *
222
     * @return array|null
223
     */
224
    public function getIcon()
225
    {
226
        return $this->info['icon'];
227
    }
228
229
    /**
230
     * Gets the page twitterUser.
231
     *
232
     * @return string|null
233
     */
234
    public function getTwitterUser()
235
    {
236
        return $this->info['twitterUser'];
237
    }
238
239
    /**
240
     * Gets some page info.
241
     *
242
     * @param array|null Array with the page fields to return as $name => $rename. Set null to return all info
243
     *
244
     * @return array
245
     */
246
    public function get(array $info = null)
247
    {
248
        if ($info === null) {
249
            return $this->info;
250
        }
251
252
        $data = array();
253
254
        foreach ($info as $name => $rename) {
255
            if (is_int($name)) {
256
                $name = $rename;
257
            }
258
259
            if (!isset($this->info[$name])) {
260
                continue;
261
            }
262
263
            $data[$rename] = $this->info[$name];
264
        }
265
266
        return $data;
267
    }
268
269
    /**
270
     * Gets one or all configuration option.
271
     *
272
     * @param string $name
273
     * @param null   $default
274
     *
275
     * @return mixed
276
     */
277
    public function getConfig($name, $default = null)
278
    {
279
        return isset($this->config[$name]) ? $this->config[$name] : $default;
280
    }
281
}
282