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 |
||
19 | class Report implements CacheableInterface |
||
20 | { |
||
21 | |||
22 | use CachableTrait; |
||
23 | |||
24 | const BASE_URL = 'http://api.adnxs.com/report'; |
||
25 | const BASE_URL_DOWNLOAD = 'http://api.adnxs.com/'; |
||
26 | |||
27 | const SANDBOX_BASE_URL = 'http://api-test.adnxs.com/report'; |
||
28 | const SANDBOX_BASE_URL_DOWNLOAD = 'http://api-test.adnxs.com/'; |
||
29 | |||
30 | |||
31 | /** @var \SplQueue */ |
||
32 | protected $userSegments; |
||
33 | |||
34 | /** @var Client|Auth */ |
||
35 | protected $client; |
||
36 | |||
37 | /** @var int */ |
||
38 | protected $memberId; |
||
39 | |||
40 | /** @var Cache */ |
||
41 | protected $cache; |
||
42 | |||
43 | const CACHE_NAMESPACE = 'appnexus_report'; |
||
44 | |||
45 | const CACHE_EXPIRATION = 3600; |
||
46 | |||
47 | const REVENUE_REPORT = [ |
||
48 | 'report' => |
||
49 | [ |
||
50 | 'report_type' => 'seller_platform_billing', |
||
51 | 'timezone' => 'PST', |
||
52 | 'report_interval' => 'last_7_days', |
||
53 | 'name' => 'Weekly SSP Revenue Report', |
||
54 | 'columns' => |
||
55 | [ |
||
56 | 0 => 'day', |
||
57 | 1 => 'seller_member', |
||
58 | 2 => 'publisher_id', |
||
59 | 3 => 'publisher_name', |
||
60 | 4 => 'publisher_code', |
||
61 | 5 => 'buyer_member_id', |
||
62 | 6 => 'buyer_member_name', |
||
63 | 7 => 'imps', |
||
64 | 8 => 'imps_delivered', |
||
65 | 9 => 'seller_revenue', |
||
66 | ], |
||
67 | ], |
||
68 | ]; |
||
69 | |||
70 | /** @var string */ |
||
71 | protected $baseUrl; |
||
72 | |||
73 | /** @var string */ |
||
74 | protected $baseUrlDownload; |
||
75 | |||
76 | /** |
||
77 | * SegmentRepository constructor. |
||
78 | * |
||
79 | * @param ClientInterface $client |
||
80 | * @param Cache|null $cache |
||
81 | */ |
||
82 | View Code Duplication | public function __construct(ClientInterface $client, Cache $cache = null) |
|
92 | |||
93 | /** |
||
94 | * @return string |
||
95 | */ |
||
96 | public function getBaseUrl() |
||
100 | |||
101 | /** |
||
102 | * @param string $baseUrl |
||
103 | */ |
||
104 | public function setBaseUrl($baseUrl) |
||
108 | |||
109 | /** |
||
110 | * @return string |
||
111 | */ |
||
112 | public function getBaseUrlDownload() |
||
116 | |||
117 | /** |
||
118 | * @param string $baseUrlDownload |
||
119 | */ |
||
120 | public function setBaseUrlDownload($baseUrlDownload) |
||
124 | |||
125 | |||
126 | /** |
||
127 | * @param array $reportFormat |
||
128 | * |
||
129 | * @return ReportTicket |
||
130 | * @throws ReportException |
||
131 | */ |
||
132 | public function getReportTicket($reportFormat = self::REVENUE_REPORT) |
||
156 | |||
157 | |||
158 | /** |
||
159 | * @param ReportTicket $reportTicket |
||
160 | * |
||
161 | * @return ReportStatus |
||
162 | * @throws ReportException |
||
163 | */ |
||
164 | public function getReportStatus(ReportTicket $reportTicket) |
||
194 | |||
195 | |||
196 | /** |
||
197 | * @param ReportStatus $reportStatus |
||
198 | * |
||
199 | * @return array |
||
200 | * @throws ReportException |
||
201 | */ |
||
202 | public function getReport(ReportStatus $reportStatus) |
||
239 | } |
||
240 |
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.