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 |
||
21 | class FileHandler |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * Brings the abstract access to the filesystem. |
||
26 | * @var FilesystemInterface |
||
27 | */ |
||
28 | protected $filesystem; |
||
29 | |||
30 | /** |
||
31 | * Holds the entity definition. |
||
32 | * @var EntityDefinition |
||
33 | */ |
||
34 | protected $entityDefinition; |
||
35 | |||
36 | /** |
||
37 | * Constructs a file system path for the given parameters for storing the |
||
38 | * file of the file field. |
||
39 | * |
||
40 | * @param string $entityName |
||
41 | * the entity name |
||
42 | * @param Entity $entity |
||
43 | * the entity |
||
44 | * @param string $field |
||
45 | * the file field in the entity |
||
46 | * |
||
47 | * @return string |
||
48 | * the constructed path for storing the file of the file field |
||
49 | */ |
||
50 | 7 | protected function getPath($entityName, Entity $entity, $field) |
|
54 | |||
55 | /** |
||
56 | * Executes a function for each file field of this entity. |
||
57 | * |
||
58 | * @param Entity $entity |
||
59 | * the just created entity |
||
60 | * @param string $entityName |
||
61 | * the name of the entity as this class here is not aware of it |
||
62 | * @param \Closure $function |
||
63 | * the function to perform, takes $entity, $entityName and $field as parameter |
||
64 | */ |
||
65 | 8 | protected function performOnFiles(Entity $entity, $entityName, $function) |
|
74 | |||
75 | /** |
||
76 | * Writes the uploaded files. |
||
77 | * |
||
78 | * @param AbstractData $data |
||
79 | * the AbstractData instance who should receive the events |
||
80 | * @param Request $request |
||
81 | * the HTTP request containing the file data |
||
82 | * @param Entity $entity |
||
83 | * the just manipulated entity |
||
84 | * @param string $entityName |
||
85 | * the name of the entity as this class here is not aware of it |
||
86 | * @param string $action |
||
87 | * the name of the performed action |
||
88 | * |
||
89 | * @return boolean |
||
90 | * true if all before events passed |
||
91 | */ |
||
92 | 6 | protected function shouldWriteFile(AbstractData $data, Request $request, Entity $entity, $entityName, $action) |
|
115 | |||
116 | /** |
||
117 | * FileHandler constructor. |
||
118 | * @param FilesystemInterface $filesystem |
||
119 | * the filesystem to use |
||
120 | * @param EntityDefinition $entityDefinition |
||
121 | * the entity definition to use |
||
122 | */ |
||
123 | 10 | public function __construct(FilesystemInterface $filesystem, EntityDefinition $entityDefinition) |
|
128 | |||
129 | |||
130 | /** |
||
131 | * Renders (outputs) a file of an entity. This includes setting headers |
||
132 | * like the file size, mimetype and name, too. |
||
133 | * |
||
134 | * @param Entity $entity |
||
135 | * the entity to render the file from |
||
136 | * @param string $entityName |
||
137 | * the name of the entity as this class here is not aware of it |
||
138 | * @param string $field |
||
139 | * the field of the entity containing the file to be rendered |
||
140 | * |
||
141 | * @return StreamedResponse |
||
142 | * the HTTP streamed response |
||
143 | */ |
||
144 | 2 | public function renderFile(Entity $entity, $entityName, $field) |
|
145 | { |
||
146 | 2 | $targetPath = $this->getPath($entityName, $entity, $field); |
|
147 | 2 | $fileName = $entity->get($field); |
|
148 | 2 | $file = $targetPath.'/'.$fileName; |
|
149 | 2 | $mimeType = $this->filesystem->getMimetype($file); |
|
150 | 2 | $size = $this->filesystem->getSize($file); |
|
151 | 2 | $stream = $this->filesystem->readStream($file); |
|
152 | $response = new StreamedResponse(function() use ($stream) { |
||
153 | 1 | while ($data = fread($stream, 1024)) { |
|
154 | 1 | echo $data; |
|
155 | 1 | flush(); |
|
156 | } |
||
157 | 1 | fclose($stream); |
|
158 | 2 | }, 200, [ |
|
159 | 2 | 'Cache-Control' => 'public, max-age=86400', |
|
160 | 2 | 'Content-length' => $size, |
|
161 | 2 | 'Content-Type' => $mimeType, |
|
162 | 2 | 'Content-Disposition' => 'attachment; filename="'.$fileName.'"' |
|
163 | ]); |
||
164 | 2 | return $response; |
|
165 | } |
||
166 | |||
167 | |||
168 | /** |
||
169 | * Deletes all files of an existing entity. |
||
170 | * |
||
171 | * @param AbstractData $data |
||
172 | * the AbstractData instance who should receive the events |
||
173 | * @param Entity $entity |
||
174 | * the entity to delete the files from |
||
175 | * @param string $entityName |
||
176 | * the name of the entity as this class here is not aware of it |
||
177 | * |
||
178 | * @return boolean |
||
179 | * true on successful deletion |
||
180 | */ |
||
181 | 2 | View Code Duplication | public function deleteFiles(AbstractData $data, Entity $entity, $entityName) |
193 | |||
194 | /** |
||
195 | * Deletes a specific file from an existing entity. |
||
196 | * |
||
197 | * @param AbstractData $data |
||
198 | * the AbstractData instance who should receive the events |
||
199 | * @param Entity $entity |
||
200 | * the entity to delete the file from |
||
201 | * @param string $entityName |
||
202 | * the name of the entity as this class here is not aware of it |
||
203 | * @param string $field |
||
204 | * the field of the entity containing the file to be deleted |
||
205 | * @return bool true on successful deletion |
||
206 | * true on successful deletion |
||
207 | */ |
||
208 | 2 | View Code Duplication | public function deleteFile(AbstractData $data, Entity $entity, $entityName, $field) |
218 | |||
219 | /** |
||
220 | * Creates the uploaded files of a newly created entity. |
||
221 | * |
||
222 | * @param AbstractData $data |
||
223 | * the AbstractData instance who should receive the events |
||
224 | * @param Request $request |
||
225 | * the HTTP request containing the file data |
||
226 | * @param Entity $entity |
||
227 | * the just created entity |
||
228 | * @param string $entityName |
||
229 | * the name of the entity as this class here is not aware of it |
||
230 | * |
||
231 | * @return boolean |
||
232 | * true if all before events passed |
||
233 | */ |
||
234 | 4 | public function createFiles(AbstractData $data, Request $request, Entity $entity, $entityName) |
|
238 | |||
239 | /** |
||
240 | * Updates the uploaded files of an updated entity. |
||
241 | * |
||
242 | * @param AbstractData $data |
||
243 | * the AbstractData instance who should receive the events |
||
244 | * @param Request $request |
||
245 | * the HTTP request containing the file data |
||
246 | * @param Entity $entity |
||
247 | * the updated entity |
||
248 | * @param string $entityName |
||
249 | * the name of the entity as this class here is not aware of it |
||
250 | * |
||
251 | * @return boolean |
||
252 | * true on successful update |
||
253 | */ |
||
254 | 2 | public function updateFiles(AbstractData $data, Request $request, Entity $entity, $entityName) |
|
259 | |||
260 | } |
||
261 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.