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 |
||
13 | final class Report implements Common |
||
14 | { |
||
15 | private $client; |
||
16 | |||
17 | /** |
||
18 | * Security constructor. |
||
19 | * |
||
20 | * @param Client $client |
||
21 | * @throws Exception |
||
22 | */ |
||
23 | 7 | public function __construct(Client $client) |
|
31 | |||
32 | 7 | public function getUrl($method, array $params = []) |
|
33 | { |
||
34 | switch ($method) { |
||
35 | 7 | case 'getBooksViewsStatistics': |
|
36 | return [ |
||
37 | 1 | 'url' => '/1.0/report/stat/book', |
|
38 | 'method' => 'GET', |
||
39 | 'code' => 200 |
||
40 | ]; |
||
41 | 6 | case 'getJournalsViewsStatistics': |
|
42 | return [ |
||
43 | 1 | 'url' => '/1.0/report/stat/journal', |
|
44 | 'method' => 'GET', |
||
45 | 'code' => 200 |
||
46 | ]; |
||
47 | 5 | case 'getUsersVisitsSatistics': |
|
48 | return [ |
||
49 | 1 | 'url' => '/1.0/report/stat/visit', |
|
50 | 'method' => 'GET', |
||
51 | 'code' => 200 |
||
52 | ]; |
||
53 | 4 | case 'getAvailablePackets': |
|
54 | return [ |
||
55 | 1 | 'url' => '/1.0/report/available/packet', |
|
56 | 'method' => 'GET', |
||
57 | 'params' => [], |
||
58 | 'code' => 200 |
||
59 | ]; |
||
60 | 3 | case 'getAvailableBooks': |
|
61 | return [ |
||
62 | 1 | 'url' => vsprintf('/1.0/report/available/book/%d', $params), |
|
63 | 1 | 'method' => 'GET', |
|
64 | 1 | 'code' => 200 |
|
65 | ]; |
||
66 | 2 | case 'getAvailableJournals': |
|
67 | return [ |
||
68 | 1 | 'url' => '/1.0/report/available/journal', |
|
69 | 'params' => [], |
||
70 | 'method' => 'GET', |
||
71 | 'code' => 200 |
||
72 | ]; |
||
73 | 1 | case 'getFormReportEBooks': |
|
74 | return [ |
||
75 | 1 | 'url' => '/1.0/report/form/eBooks', |
|
76 | 'params' => [], |
||
77 | 'method' => 'GET', |
||
78 | 'code' => 200 |
||
79 | ]; |
||
80 | default: |
||
81 | throw new Exception('Route for ' . $method . ' not found'); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | 1 | View Code Duplication | public function getBooksViewsStatistics($groupBy = null, $periodFrom = null, $periodTo = null) |
|
|||
86 | { |
||
87 | 1 | return $this->client->getResponse( |
|
88 | 1 | $this->getUrl(__FUNCTION__), |
|
89 | [ |
||
90 | 1 | 'group_by' => $groupBy, |
|
91 | 1 | 'period_range_from' => $periodFrom, |
|
92 | 1 | 'period_range_to' => $periodTo, |
|
93 | ] |
||
94 | )['data']; |
||
95 | } |
||
96 | |||
97 | 1 | View Code Duplication | public function getJournalsViewsStatistics($groupBy = null, $periodFrom = null, $periodTo = null) |
98 | { |
||
99 | 1 | return $this->client->getResponse( |
|
100 | 1 | $this->getUrl(__FUNCTION__), |
|
101 | [ |
||
102 | 1 | 'group_by' => $groupBy, |
|
103 | 1 | 'period_range_from' => $periodFrom, |
|
104 | 1 | 'period_range_to' => $periodTo, |
|
105 | ] |
||
106 | )['data']; |
||
107 | } |
||
108 | |||
109 | 1 | View Code Duplication | public function getUsersVisitsSatistics($groupBy = null, $periodFrom = null, $periodTo = null) |
110 | { |
||
111 | 1 | return $this->client->getResponse( |
|
112 | 1 | $this->getUrl(__FUNCTION__), |
|
113 | [ |
||
114 | 1 | 'group_by' => $groupBy, |
|
115 | 1 | 'period_range_from' => $periodFrom, |
|
116 | 1 | 'period_range_to' => $periodTo, |
|
117 | ] |
||
118 | )['data']; |
||
119 | } |
||
120 | |||
121 | 1 | public function getAvailablePackets() |
|
125 | |||
126 | 1 | public function getAvailableBooks($pdKey) |
|
130 | |||
131 | 1 | public function getAvailableJournals() |
|
135 | |||
136 | 1 | public function getFormReportEBooks() |
|
140 | } |
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.