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 |
||
10 | class Analytics |
||
11 | { |
||
12 | use Macroable; |
||
13 | |||
14 | /** @var \Spatie\Analytics\AnalyticsClient */ |
||
15 | protected $client; |
||
16 | |||
17 | /** @var string */ |
||
18 | protected $viewId; |
||
19 | |||
20 | /** |
||
21 | * @param \Spatie\Analytics\AnalyticsClient $client |
||
22 | * @param string $viewId |
||
23 | */ |
||
24 | public function __construct(AnalyticsClient $client, string $viewId) |
||
30 | |||
31 | /** |
||
32 | * @param string $viewId |
||
33 | * |
||
34 | * @return $this |
||
35 | */ |
||
36 | public function setViewId(string $viewId) |
||
42 | |||
43 | public function getViewId() |
||
47 | |||
48 | View Code Duplication | public function fetchVisitorsAndPageViews(Period $period): Collection |
|
65 | |||
66 | View Code Duplication | public function fetchTotalVisitorsAndPageViews(Period $period): Collection |
|
82 | |||
83 | View Code Duplication | public function fetchMostVisitedPages(Period $period, int $maxResults = 20): Collection |
|
104 | |||
105 | View Code Duplication | public function fetchTopReferrers(Period $period, int $maxResults = 20): Collection |
|
123 | |||
124 | public function fetchUserTypes(Period $period): Collection |
||
141 | |||
142 | public function fetchTopBrowsers(Period $period, int $maxResults = 10): Collection |
||
166 | |||
167 | protected function summarizeTopBrowsers(Collection $topBrowsers, int $maxResults): Collection |
||
176 | |||
177 | /** |
||
178 | * Call the query method on the authenticated client. |
||
179 | * |
||
180 | * @param Period $period |
||
181 | * @param string $metrics |
||
182 | * @param array $others |
||
183 | * |
||
184 | * @return array|null |
||
185 | */ |
||
186 | public function performQuery(Period $period, string $metrics, array $others = []) |
||
196 | |||
197 | /* |
||
198 | * Get the underlying Google_Service_Analytics object. You can use this |
||
199 | * to basically call anything on the Google Analytics API. |
||
200 | */ |
||
201 | public function getAnalyticsService(): Google_Service_Analytics |
||
205 | } |
||
206 |
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.