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 SegmentRepository implements CacheableInterface |
||
17 | { |
||
18 | |||
19 | use CachableTrait; |
||
20 | |||
21 | const BASE_URL = 'https://api.adnxs.com/segment/'; |
||
22 | |||
23 | const SANDBOX_BASE_URL = 'http://api-test.adnxs.com/segment/'; |
||
24 | |||
25 | /** @var Client */ |
||
26 | protected $client; |
||
27 | |||
28 | /** @var int */ |
||
29 | protected $memberId; |
||
30 | |||
31 | /** @var Cache */ |
||
32 | protected $cache; |
||
33 | |||
34 | /** @var string */ |
||
35 | protected $baseUrl; |
||
36 | |||
37 | const CACHE_NAMESPACE = 'appnexus_segment_repository_find_all'; |
||
38 | |||
39 | const CACHE_EXPIRATION = 3600; |
||
40 | |||
41 | /** |
||
42 | * SegmentRepository constructor. |
||
43 | * |
||
44 | * @param ClientInterface $client |
||
45 | * @param Cache|null $cache |
||
46 | */ |
||
47 | View Code Duplication | public function __construct(ClientInterface $client, Cache $cache = null) |
|
54 | |||
55 | /** |
||
56 | * @return string |
||
57 | */ |
||
58 | public function getBaseUrl() |
||
62 | |||
63 | /** |
||
64 | * @param string $baseUrl |
||
65 | */ |
||
66 | public function setBaseUrl($baseUrl) |
||
70 | |||
71 | |||
72 | /** |
||
73 | * @param Segment $segment |
||
74 | * |
||
75 | * @return RepositoryResponse |
||
76 | * @throws RepositoryException |
||
77 | */ |
||
78 | public function add(Segment $segment) |
||
106 | |||
107 | /** |
||
108 | * @param $memberId |
||
109 | * @param $id |
||
110 | * |
||
111 | * @return RepositoryResponse |
||
112 | */ |
||
113 | public function remove($memberId, $id) |
||
125 | |||
126 | /** |
||
127 | * @param Segment $segment |
||
128 | * |
||
129 | * @return RepositoryResponse |
||
130 | * @throws RepositoryException |
||
131 | */ |
||
132 | public function update(Segment $segment) |
||
152 | |||
153 | /** |
||
154 | * @param $id |
||
155 | * @param $memberId |
||
156 | * |
||
157 | * @return Segment|null |
||
158 | */ |
||
159 | public function findOneById($memberId, $id) |
||
179 | |||
180 | /** |
||
181 | * @param $memberId |
||
182 | * @param int $start |
||
183 | * @param int $maxResults |
||
184 | * |
||
185 | * @return Segment[]|null |
||
186 | * @throws RepositoryException |
||
187 | */ |
||
188 | public function findAll($memberId, $start = 0, $maxResults = 100) |
||
230 | } |
||
231 |
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.