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 |
||
22 | class UsageRenderer extends ColumnRendererAbstract |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * Render usage of an asset in the grid. |
||
27 | * |
||
28 | * @return string |
||
29 | */ |
||
30 | public function render() |
||
31 | { |
||
32 | $file = $this->getFileConverter()->convert($this->object); |
||
33 | |||
34 | $result = ''; |
||
35 | |||
36 | // Add number of references on the top! |
||
37 | if ($this->object['number_of_references'] > 1) { |
||
38 | $result .= sprintf( |
||
39 | '<div><strong>%s (%s)</strong></div>', |
||
40 | $this->getLanguageService()->sL('LLL:EXT:media/Resources/Private/Language/locallang.xlf:references'), |
||
41 | $this->object['number_of_references'] |
||
42 | ); |
||
43 | } |
||
44 | |||
45 | // Render File usage |
||
46 | $fileReferences = $this->getFileReferenceService()->findFileReferences($file); |
||
47 | View Code Duplication | if (!empty($fileReferences)) { |
|
|
|||
48 | |||
49 | // Finalize file references assembling. |
||
50 | $result .= sprintf( |
||
51 | $this->getWrappingTemplate(), |
||
52 | $this->getLanguageService()->sL('LLL:EXT:media/Resources/Private/Language/locallang.xlf:file_reference'), |
||
53 | $this->assembleOutput($fileReferences, array('referenceIdentifier' => 'uid_foreign', 'tableName' => 'tablenames')) |
||
54 | ); |
||
55 | } |
||
56 | |||
57 | // Render link usage in RTE |
||
58 | $linkSoftReferences = $this->getFileReferenceService()->findSoftLinkReferences($file); |
||
59 | View Code Duplication | if (!empty($linkSoftReferences)) { |
|
60 | |||
61 | // Finalize link references assembling. |
||
62 | $result .= sprintf( |
||
63 | $this->getWrappingTemplate(), |
||
64 | $this->getLanguageService()->sL('LLL:EXT:media/Resources/Private/Language/locallang.xlf:link_references_in_rte'), |
||
65 | $this->assembleOutput($linkSoftReferences, array('referenceIdentifier' => 'recuid', 'tableName' => 'tablename')) |
||
66 | ); |
||
67 | } |
||
68 | |||
69 | // Render image usage in RTE |
||
70 | $imageSoftReferences = $this->getFileReferenceService()->findSoftImageReferences($file); |
||
71 | View Code Duplication | if (!empty($imageSoftReferences)) { |
|
72 | |||
73 | // Finalize image references assembling. |
||
74 | $result .= sprintf( |
||
75 | $this->getWrappingTemplate(), |
||
76 | $this->getLanguageService()->sL('LLL:EXT:media/Resources/Private/Language/locallang.xlf:image_references_in_rte'), |
||
77 | $this->assembleOutput($imageSoftReferences, array('referenceIdentifier' => 'recuid', 'tableName' => 'tablename')) |
||
78 | ); |
||
79 | } |
||
80 | |||
81 | return $result; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Assemble output reference. |
||
86 | * |
||
87 | * @param array $references |
||
88 | * @param array $mapping |
||
89 | * @return string |
||
90 | */ |
||
91 | protected function assembleOutput(array $references, array $mapping) |
||
114 | |||
115 | /** |
||
116 | * @param string $tableName |
||
117 | * @param int $identifier |
||
118 | * @return string |
||
119 | */ |
||
120 | protected function computeTitle($tableName, $identifier) |
||
131 | |||
132 | /** |
||
133 | * @return object|LinkButton |
||
134 | */ |
||
135 | protected function makeLinkButton() |
||
139 | |||
140 | /** |
||
141 | * @param array $reference |
||
142 | * @param array $mapping |
||
143 | * @return string |
||
144 | */ |
||
145 | protected function getEditUri(array $reference, array $mapping) |
||
158 | |||
159 | /** |
||
160 | * @return string |
||
161 | */ |
||
162 | protected function getModuleUrl() |
||
173 | |||
174 | /** |
||
175 | * Return the title given a table name and an identifier. |
||
176 | * |
||
177 | * @param string $tableName |
||
178 | * @param string $identifier |
||
179 | * @return string |
||
180 | */ |
||
181 | protected function getRecordTitle($tableName, $identifier) |
||
200 | |||
201 | /** |
||
202 | * @return object|DataService |
||
203 | */ |
||
204 | protected function getDataService(): DataService |
||
208 | |||
209 | /** |
||
210 | * Return the wrapping HTML template. |
||
211 | * |
||
212 | * @return string |
||
213 | */ |
||
214 | protected function getWrappingTemplate() |
||
218 | |||
219 | /** |
||
220 | * @return \Fab\Media\Resource\FileReferenceService|object |
||
221 | */ |
||
222 | protected function getFileReferenceService() |
||
226 | |||
227 | /** |
||
228 | * @return \Fab\Media\TypeConverter\ContentToFileConverter|object |
||
229 | */ |
||
230 | protected function getFileConverter() |
||
234 | } |
||
235 |
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.