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 |
||
16 | class SegmentBillingRepository implements CacheableInterface |
||
17 | { |
||
18 | use CachableTrait; |
||
19 | |||
20 | const BASE_URL = 'https://api.adnxs.com/segment-billing-category'; |
||
21 | |||
22 | const SANDBOX_BASE_URL = 'http://api-test.adnxs.com/segment-billing-category'; |
||
23 | |||
24 | /** @var Client */ |
||
25 | protected $client; |
||
26 | |||
27 | /** @var int */ |
||
28 | protected $memberId; |
||
29 | |||
30 | /** @var Cache */ |
||
31 | protected $cache; |
||
32 | |||
33 | /** @var string */ |
||
34 | protected $baseUrl; |
||
35 | |||
36 | const CACHE_NAMESPACE = 'appnexus_segment_billing_repository_find_all'; |
||
37 | |||
38 | const CACHE_EXPIRATION = 3600; |
||
39 | |||
40 | /** |
||
41 | * SegmentRepository constructor. |
||
42 | * |
||
43 | * @param ClientInterface $client |
||
44 | * @param Cache|null $cache |
||
45 | */ |
||
46 | View Code Duplication | public function __construct(ClientInterface $client, Cache $cache = null) |
|
53 | |||
54 | /** |
||
55 | * @return string |
||
56 | */ |
||
57 | public function getBaseUrl() |
||
61 | |||
62 | /** |
||
63 | * @param string $baseUrl |
||
64 | */ |
||
65 | public function setBaseUrl($baseUrl) |
||
69 | |||
70 | |||
71 | /** |
||
72 | * @param SegmentBilling $segmentBilling |
||
73 | * |
||
74 | * @return RepositoryResponse |
||
75 | * @throws RepositoryException |
||
76 | */ |
||
77 | public function add(SegmentBilling $segmentBilling) |
||
107 | |||
108 | /** |
||
109 | * @param SegmentBilling $segmentBilling |
||
110 | * |
||
111 | * @return RepositoryResponse |
||
112 | * @throws RepositoryException |
||
113 | */ |
||
114 | View Code Duplication | public function update(SegmentBilling $segmentBilling) |
|
133 | |||
134 | /** |
||
135 | * @param $memberId |
||
136 | * @param $segmentId |
||
137 | * @return SegmentBilling |
||
138 | * @throws RepositoryException |
||
139 | */ |
||
140 | public function findOneBySegmentId($memberId, $segmentId) |
||
169 | |||
170 | /** |
||
171 | * @param $memberId |
||
172 | * @param int $start |
||
173 | * @param int $maxResults |
||
174 | * |
||
175 | * @return SegmentBilling[]|null |
||
176 | * @throws RepositoryException |
||
177 | */ |
||
178 | public function findAll($memberId, $start = 0, $maxResults = 100) |
||
222 | |||
223 | /** |
||
224 | * @param $memberId |
||
225 | * @param $id |
||
226 | * |
||
227 | * @return RepositoryResponse |
||
228 | */ |
||
229 | View Code Duplication | public function remove($memberId, $id) |
|
240 | } |
||
241 |
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.