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 |
||
11 | class Analytics |
||
12 | { |
||
13 | /** |
||
14 | * @var \Spatie\Analytics\GoogleClient |
||
15 | */ |
||
16 | protected $client; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $siteId; |
||
22 | |||
23 | /** |
||
24 | * @param \Spatie\Analytics\GoogleClient $client |
||
25 | * @param string $siteId |
||
26 | */ |
||
27 | public function __construct(GoogleClient $client, $siteId = '') |
||
32 | |||
33 | /** |
||
34 | * Get the amount of visitors and pageViews. |
||
35 | * |
||
36 | * @param int $numberOfDays |
||
37 | * @param string $groupBy Possible values: date, yearMonth |
||
38 | * @return array |
||
39 | */ |
||
40 | public function getVisitorsAndPageViews($numberOfDays = 365, $groupBy = 'date') |
||
46 | |||
47 | /** |
||
48 | * Get the amount of visitors and pageviews for the given period. |
||
49 | * |
||
50 | * @param \DateTime $startDate |
||
51 | * @param \DateTime $endDate |
||
52 | * @param string $groupBy Possible values: date, yearMonth |
||
53 | * @return array |
||
54 | */ |
||
55 | public function getVisitorsAndPageViewsForPeriod(DateTime $startDate, DateTime $endDate, $groupBy = 'date') |
||
80 | |||
81 | /** |
||
82 | * Get the top keywords. |
||
83 | * |
||
84 | * @param int $numberOfDays |
||
85 | * @param int $maxResults |
||
86 | * @return array |
||
87 | */ |
||
88 | public function getTopKeywords($numberOfDays = 365, $maxResults = 30) |
||
94 | |||
95 | /** |
||
96 | * Get the top keywords for the given period. |
||
97 | * |
||
98 | * @param \DateTime $startDate |
||
99 | * @param \DateTime $endDate |
||
100 | * @param int $maxResults |
||
101 | * @return array |
||
102 | */ |
||
103 | public function getTopKeyWordsForPeriod(DateTime $startDate, DateTime $endDate, $maxResults = 30) |
||
132 | |||
133 | /** |
||
134 | * Get the top referrers. |
||
135 | * |
||
136 | * @param int $numberOfDays |
||
137 | * @param int $maxResults |
||
138 | * @return array |
||
139 | */ |
||
140 | public function getTopReferrers($numberOfDays = 365, $maxResults = 20) |
||
146 | |||
147 | /** |
||
148 | * Get the top referrers for the given period. |
||
149 | * |
||
150 | * @param \DateTime $startDate |
||
151 | * @param \DateTime $endDate |
||
152 | * @param int $maxResults |
||
153 | * @return array |
||
154 | */ |
||
155 | View Code Duplication | public function getTopReferrersForPeriod(DateTime $startDate, DateTime $endDate, $maxResults) |
|
183 | |||
184 | /** |
||
185 | * Get the top browsers. |
||
186 | * |
||
187 | * @param int $numberOfDays |
||
188 | * @param int $maxResults |
||
189 | * @return array |
||
190 | */ |
||
191 | public function getTopBrowsers($numberOfDays = 365, $maxResults = 6) |
||
197 | |||
198 | /** |
||
199 | * Get the top browsers for the given period. |
||
200 | * |
||
201 | * @param \DateTime $startDate |
||
202 | * @param \DateTime $endDate |
||
203 | * @param int $maxResults |
||
204 | * @return array |
||
205 | */ |
||
206 | public function getTopBrowsersForPeriod(DateTime $startDate, DateTime $endDate, $maxResults) |
||
248 | |||
249 | /** |
||
250 | * Get the most visited pages. |
||
251 | * |
||
252 | * @param int $numberOfDays |
||
253 | * @param int $maxResults |
||
254 | * @return array |
||
255 | */ |
||
256 | public function getMostVisitedPages($numberOfDays = 365, $maxResults = 20) |
||
262 | |||
263 | /** |
||
264 | * Get the number of active users currently on the site |
||
265 | * |
||
266 | * @param array $others |
||
267 | * @return int |
||
268 | */ |
||
269 | public function getActiveUsers($others = array()) |
||
279 | |||
280 | /** |
||
281 | * Get the most visited pages for the given period. |
||
282 | * |
||
283 | * @param \DateTime $startDate |
||
284 | * @param \DateTime $endDate |
||
285 | * @param int $maxResults |
||
286 | * @return array |
||
287 | */ |
||
288 | View Code Duplication | public function getMostVisitedPagesForPeriod(DateTime $startDate, DateTime $endDate, $maxResults = 20) |
|
316 | |||
317 | /** |
||
318 | * Returns the site id (ga:xxxxxxx) for the given url. |
||
319 | * |
||
320 | * @param string $url |
||
321 | * @return string |
||
322 | */ |
||
323 | public function getSiteIdByUrl($url) |
||
327 | |||
328 | /** |
||
329 | * Call the query method on the authenticated client. |
||
330 | * |
||
331 | * @param \DateTime $startDate |
||
332 | * @param \DateTime $endDate |
||
333 | * @param string $metrics |
||
334 | * @param array $others |
||
335 | * @return mixed |
||
336 | */ |
||
337 | public function performQuery(DateTime $startDate, DateTime $endDate, $metrics, $others = array()) |
||
347 | |||
348 | /** |
||
349 | * Call the real time query method on the authenticated client. |
||
350 | * |
||
351 | * @param string $metrics |
||
352 | * @param array $others |
||
353 | * @return mixed |
||
354 | */ |
||
355 | public function performRealTimeQuery($metrics, $others = array()) |
||
363 | |||
364 | /** |
||
365 | * Return true if this site is configured to use Google Analytics. |
||
366 | * |
||
367 | * @return bool |
||
368 | */ |
||
369 | public function isEnabled() |
||
373 | |||
374 | /** |
||
375 | * Returns an array with the current date and the date minus the number of days specified. |
||
376 | * |
||
377 | * @param int $numberOfDays |
||
378 | * @return array |
||
379 | */ |
||
380 | protected function calculateNumberOfDays($numberOfDays) |
||
390 | |||
391 | /** |
||
392 | * Create a new instance via a set of parameters |
||
393 | * |
||
394 | * @param string $siteId |
||
395 | * Ex. ga:xxxxxxxx |
||
396 | * @param string $clientId |
||
397 | * Ex. xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com |
||
398 | * @param string $serviceEmail |
||
399 | * Ex. xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com |
||
400 | * @param string $certificatePath |
||
401 | * Ex. /../keys/analytics/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-privatekey.p12 |
||
402 | * |
||
403 | * @param \Spatie\Analytics\Cache|null $cache |
||
404 | * @param int $cacheLifetimeInMinutes |
||
405 | * @param int $realTimeCacheLifetime |
||
406 | * |
||
407 | * @return \Spatie\Analytics\Analytics |
||
408 | * |
||
409 | * @throws \Exception |
||
410 | */ |
||
411 | public static function create($siteId, $clientId, $serviceEmail, $certificatePath, Cache $cache = null, |
||
443 | } |
||
444 |
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.