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 |
||
18 | View Code Duplication | class ImportSourceManager implements SourceManagerInterface |
|
|
|||
19 | { |
||
20 | /** |
||
21 | * @var SourceManagerInterface |
||
22 | */ |
||
23 | protected $sourceManager; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $sources = []; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $originSources = []; |
||
34 | |||
35 | /** |
||
36 | * @param SourceManagerInterface $sourceManager |
||
37 | */ |
||
38 | public function __construct(SourceManagerInterface $sourceManager) |
||
42 | |||
43 | /** |
||
44 | * @inheritdoc |
||
45 | */ |
||
46 | public function getRepository() |
||
50 | |||
51 | /** |
||
52 | * @inheritdoc |
||
53 | */ |
||
54 | public function findById($sourceId) |
||
63 | |||
64 | /** |
||
65 | * @inheritdoc |
||
66 | */ |
||
67 | public function findSourceByFeed(Feed $feed, $originalId) |
||
76 | |||
77 | /** |
||
78 | * @inheritdoc |
||
79 | */ |
||
80 | public function findSourceByScraper(Scraper $scraper, $originalId) |
||
89 | |||
90 | /** |
||
91 | * @inheritdoc |
||
92 | */ |
||
93 | public function findSourceByFeedOrCreate(Feed $feed, $originalId, $originalUrl = null) |
||
102 | |||
103 | /** |
||
104 | * @inheritdoc |
||
105 | */ |
||
106 | public function findSourceByScraperOrCreate(Scraper $scraper, $originalId, $originalUrl) |
||
115 | |||
116 | /** |
||
117 | * @inheritdoc |
||
118 | */ |
||
119 | public function persist(SourceInterface $source) |
||
123 | |||
124 | /** |
||
125 | * @inheritdoc |
||
126 | */ |
||
127 | public function remove(SourceInterface $source) |
||
131 | |||
132 | /** |
||
133 | * @inheritdoc |
||
134 | */ |
||
135 | public function detach(SourceInterface $source) |
||
139 | |||
140 | /** |
||
141 | * @inheritdoc |
||
142 | */ |
||
143 | public function flush(SourceInterface $source = null) |
||
147 | |||
148 | /** |
||
149 | * @inheritdoc |
||
150 | */ |
||
151 | public function clear() |
||
157 | |||
158 | /** |
||
159 | * Adds source to the internal cache. |
||
160 | * |
||
161 | * @param SourceInterface $source |
||
162 | */ |
||
163 | protected function cache(SourceInterface $source = null) |
||
191 | |||
192 | /** |
||
193 | * @param int $sourceId |
||
194 | * |
||
195 | * @return SourceInterface|null |
||
196 | */ |
||
197 | protected function findCachedById($sourceId) |
||
205 | |||
206 | /** |
||
207 | * @param string $hash |
||
208 | * @param string $originalId |
||
209 | * |
||
210 | * @return SourceInterface|null |
||
211 | */ |
||
212 | protected function findCachedByOrigin($hash, $originalId) |
||
226 | |||
227 | /** |
||
228 | * @param Feed $feed |
||
229 | * @param string $originalId |
||
230 | * |
||
231 | * @return SourceInterface|null |
||
232 | */ |
||
233 | protected function findCachedByFeed(Feed $feed, $originalId) |
||
239 | |||
240 | /** |
||
241 | * @param Scraper $scraper |
||
242 | * @param string $originalId |
||
243 | * |
||
244 | * @return SourceInterface|null |
||
245 | */ |
||
246 | protected function findCachedByScraper(Scraper $scraper, $originalId) |
||
252 | |||
253 | /** |
||
254 | * Returns a unique hash for a feed. |
||
255 | * |
||
256 | * @param Feed $feed |
||
257 | * |
||
258 | * @return string |
||
259 | */ |
||
260 | protected function getFeedHash(Feed $feed) |
||
264 | |||
265 | /** |
||
266 | * Returns a unique hash for a scraper. |
||
267 | * |
||
268 | * @param Scraper $scraper |
||
269 | * |
||
270 | * @return string |
||
271 | */ |
||
272 | protected function getScraperHash(Scraper $scraper) |
||
276 | } |
||
277 |
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.