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 |
||
7 | class YouTubeProvider extends AbstractOembedProvider implements ProviderInterface, EmbeddableProviderInterface |
||
8 | { |
||
9 | const URL_OEMBED = 'http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=%s&format=json'; |
||
10 | const URL_IMAGE_MAX_RES = 'https://i.ytimg.com/vi/%s/maxresdefault.jpg'; |
||
11 | const URL_IMAGE_HQ = 'https://i.ytimg.com/vi/%s/hqdefault.jpg'; |
||
12 | |||
13 | /** |
||
14 | * @param string $id |
||
15 | * @return string |
||
16 | */ |
||
17 | 2 | public function getOembedUrl($id): string |
|
21 | |||
22 | /** |
||
23 | * @param string $id |
||
24 | * @return string |
||
25 | */ |
||
26 | 3 | public function getImageUrl($id): string |
|
36 | |||
37 | /** |
||
38 | * @param $value |
||
39 | * @return string |
||
40 | * @throws \Exception |
||
41 | */ |
||
42 | 6 | public function parseProviderReference($value): string |
|
54 | |||
55 | /** |
||
56 | * @param string $url |
||
57 | * @return mixed |
||
58 | * @throws InvalidProviderUrlException |
||
59 | */ |
||
60 | 4 | View Code Duplication | protected function parseProviderReferenceFromUrl($url): string |
73 | |||
74 | /** |
||
75 | * @param string $url |
||
76 | * @return string |
||
77 | * @throws InvalidProviderUrlException |
||
78 | */ |
||
79 | 2 | View Code Duplication | protected function parseProviderReferenceFromShortUrl($url): string |
89 | |||
90 | /** |
||
91 | * @return string |
||
92 | */ |
||
93 | 12 | public function getIcon(): string |
|
97 | |||
98 | /** |
||
99 | * @return string |
||
100 | */ |
||
101 | 13 | public function getName(): string |
|
105 | |||
106 | /** |
||
107 | * @return string |
||
108 | */ |
||
109 | 2 | public function getType(): string |
|
113 | } |
||
114 |
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.