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 | View Code Duplication | public function get($value) |
|
45 | |||
46 | /** |
||
47 | * @param Request $request |
||
48 | * @param string $value |
||
49 | * @return array |
||
50 | * @throws Exception |
||
51 | */ |
||
52 | View Code Duplication | private function search(Request $request, $value) |
|
65 | |||
66 | /** |
||
67 | * @param string $feedUrl |
||
68 | * @return array |
||
69 | */ |
||
70 | View Code Duplication | public function find($feedUrl) |
|
85 | |||
86 | /** |
||
87 | * @param int $limit |
||
88 | * @return PodcastScraper |
||
89 | */ |
||
90 | public function limit($limit) |
||
97 | |||
98 | /** |
||
99 | * @param Request $request |
||
100 | * @param string $feedUrl |
||
101 | * @return array |
||
102 | * @throws Exception |
||
103 | */ |
||
104 | View Code Duplication | private function read(Request $request, $feedUrl) |
|
117 | |||
118 | /** |
||
119 | * @param string $feedUrl |
||
120 | * |
||
121 | * @return mixed |
||
122 | */ |
||
123 | protected function getFeedFromUrl($feedUrl) |
||
137 | |||
138 | /** |
||
139 | * @param string $message |
||
140 | * |
||
141 | * @return array |
||
142 | */ |
||
143 | protected function failedResponse($message) |
||
150 | } |
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.