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 | abstract class AbstractOembedProvider extends AbstractProvider implements OembedProviderInterface |
||
11 | { |
||
12 | /** |
||
13 | * @var array |
||
14 | */ |
||
15 | protected $oembedData; |
||
16 | |||
17 | /** |
||
18 | * @param Media $media |
||
19 | * @param bool $providerReferenceUpdated |
||
20 | * @throws \Exception |
||
21 | */ |
||
22 | public function update(Media $media, $providerReferenceUpdated) |
||
47 | |||
48 | /** |
||
49 | * @param FormMapper $formMapper |
||
50 | */ |
||
51 | View Code Duplication | public function buildProviderCreateForm(FormMapper $formMapper) |
|
59 | |||
60 | /** |
||
61 | * @param FormMapper $formMapper |
||
62 | */ |
||
63 | View Code Duplication | public function buildProviderEditForm(FormMapper $formMapper) |
|
71 | |||
72 | /** |
||
73 | * @param ErrorElement $errorElement |
||
74 | * @param Media $media |
||
75 | */ |
||
76 | public function validate(ErrorElement $errorElement, Media $media) |
||
85 | |||
86 | /** |
||
87 | * @param \MediaMonks\SonataMediaBundle\Entity\Media $media |
||
88 | */ |
||
89 | public function refreshImage(Media $media) |
||
100 | |||
101 | /** |
||
102 | * @param string $id |
||
103 | * @return string |
||
104 | */ |
||
105 | public function getImageUrl($id) |
||
109 | |||
110 | /** |
||
111 | * @param $id |
||
112 | * @return mixed |
||
113 | * @throws \Exception |
||
114 | */ |
||
115 | protected function getProviderOembedData($id) |
||
132 | |||
133 | /** |
||
134 | * @return string |
||
135 | */ |
||
136 | public function getReferenceName() |
||
140 | |||
141 | /** |
||
142 | * @return bool |
||
143 | */ |
||
144 | public function supportsDownload() |
||
148 | |||
149 | /** |
||
150 | * @return bool |
||
151 | */ |
||
152 | public function supportsEmbed() |
||
156 | |||
157 | /** |
||
158 | * @return bool |
||
159 | */ |
||
160 | public function supportsImage() |
||
164 | } |
||
165 |
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.