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 |
||
10 | class PodcastScraper |
||
11 | { |
||
12 | /** |
||
13 | * @var VendorInterface |
||
14 | */ |
||
15 | private $vendor; |
||
16 | |||
17 | /** |
||
18 | * @param VendorInterface $vendor |
||
19 | */ |
||
20 | public function __construct(VendorInterface $vendor) |
||
24 | |||
25 | /** |
||
26 | * @param string $value |
||
27 | * @return array |
||
28 | */ |
||
29 | public function get($value) |
||
53 | |||
54 | /** |
||
55 | * @param Request $request |
||
56 | * @param string $value |
||
57 | * @return array |
||
58 | * @throws Exception |
||
59 | */ |
||
60 | View Code Duplication | private function search(Request $request, $value) |
|
73 | |||
74 | /** |
||
75 | * @param string $feedUrl |
||
76 | * @return array |
||
77 | */ |
||
78 | public function find($feedUrl) |
||
110 | |||
111 | /** |
||
112 | * @param int $limit |
||
113 | * @return PodcastScraper |
||
114 | */ |
||
115 | public function limit($limit) |
||
122 | |||
123 | /** |
||
124 | * @param Request $request |
||
125 | * @param string $feedUrl |
||
126 | * @return array |
||
127 | * @throws Exception |
||
128 | */ |
||
129 | View Code Duplication | private function read(Request $request, $feedUrl) |
|
142 | } |
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.