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 | /** @var \SplQueue */ |
||
28 | protected $userSegments; |
||
29 | |||
30 | /** @var Client|Auth */ |
||
31 | protected $client; |
||
32 | |||
33 | /** @var int */ |
||
34 | protected $memberId; |
||
35 | |||
36 | /** @var Cache */ |
||
37 | protected $cache; |
||
38 | |||
39 | const CACHE_NAMESPACE = 'appnexus_report'; |
||
40 | |||
41 | const CACHE_EXPIRATION = 3600; |
||
42 | |||
43 | const REVENUE_REPORT = [ |
||
44 | 'report' => |
||
45 | [ |
||
46 | 'report_type' => 'seller_platform_billing', |
||
47 | 'timezone' => 'PST', |
||
48 | 'report_interval' => 'last_7_days', |
||
49 | 'name' => 'Weekly SSP Revenue Report', |
||
50 | 'columns' => |
||
51 | [ |
||
52 | 0 => 'day', |
||
53 | 1 => 'seller_member', |
||
54 | 2 => 'publisher_id', |
||
55 | 3 => 'publisher_name', |
||
56 | 4 => 'publisher_code', |
||
57 | 5 => 'buyer_member_id', |
||
58 | 6 => 'buyer_member_name', |
||
59 | 7 => 'imps', |
||
60 | 8 => 'imps_delivered', |
||
61 | 9 => 'seller_revenue', |
||
62 | ], |
||
63 | ], |
||
64 | ]; |
||
65 | |||
66 | /** |
||
67 | * SegmentRepository constructor. |
||
68 | * |
||
69 | * @param ClientInterface $client |
||
70 | * @param Cache|null $cache |
||
71 | */ |
||
72 | View Code Duplication | public function __construct(ClientInterface $client, Cache $cache = null) |
|
79 | |||
80 | /** |
||
81 | * @param array $reportFormat |
||
82 | * |
||
83 | * @return ReportTicket |
||
84 | * @throws ReportException |
||
85 | */ |
||
86 | public function getReportTicket($reportFormat = self::REVENUE_REPORT) |
||
110 | |||
111 | |||
112 | /** |
||
113 | * @param ReportTicket $reportTicket |
||
114 | * |
||
115 | * @return ReportStatus |
||
116 | * @throws ReportException |
||
117 | */ |
||
118 | public function getReportStatus(ReportTicket $reportTicket) |
||
148 | |||
149 | |||
150 | /** |
||
151 | * @param ReportStatus $reportStatus |
||
152 | * |
||
153 | * @return array |
||
154 | * @throws ReportException |
||
155 | */ |
||
156 | public function getReport(ReportStatus $reportStatus) |
||
193 | } |
||
194 |
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.