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 |
||
26 | class MediaManager implements MediaManagerInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var Filesystem |
||
30 | */ |
||
31 | protected $filesystem; |
||
32 | |||
33 | /** |
||
34 | * @var RouterInterface |
||
35 | */ |
||
36 | protected $router; |
||
37 | |||
38 | /** |
||
39 | * @var ArticleMediaRepositoryInterface |
||
40 | */ |
||
41 | protected $mediaRepository; |
||
42 | |||
43 | /** |
||
44 | * @var FactoryInterface |
||
45 | */ |
||
46 | protected $imageFactory; |
||
47 | |||
48 | /** |
||
49 | * @var FileFactoryInterface |
||
50 | */ |
||
51 | protected $fileFactory; |
||
52 | |||
53 | /** |
||
54 | * MediaManager constructor. |
||
55 | 12 | * |
|
56 | * @param ArticleMediaRepositoryInterface $mediaRepository |
||
57 | * @param Filesystem $filesystem |
||
58 | * @param RouterInterface $router |
||
59 | * @param FactoryInterface $imageFactory |
||
60 | * @param FileFactoryInterface $fileFactory |
||
61 | 12 | */ |
|
62 | 12 | public function __construct( |
|
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | public function handleUploadedFile(UploadedFile $uploadedFile, $mediaId) |
||
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | public function getFile(FileInterface $media) |
||
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | public function saveFile(UploadedFile $uploadedFile, $fileName) |
||
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | public function getMediaPublicUrl(FileInterface $media) |
||
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | View Code Duplication | public function getMediaUri(FileInterface $media, $type = RouterInterface::ABSOLUTE_PATH) |
|
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | public function createMediaAsset(UploadedFile $uploadedFile, string $assetId): FileInterface |
||
144 | |||
145 | /** |
||
146 | * @return string |
||
147 | */ |
||
148 | protected function getMediaBasePath(): string |
||
154 | |||
155 | private function guessExtension(UploadedFile $uploadedFile): string |
||
165 | } |
||
166 |
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.