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 |
||
27 | class IndexAction extends AbstractMiddlewareAction |
||
28 | { |
||
29 | /** |
||
30 | * @var EnvironmentInterface |
||
31 | */ |
||
32 | protected $environmentManager; |
||
33 | |||
34 | /** |
||
35 | * @var StorageInterface[] |
||
36 | */ |
||
37 | private $dataStorages; |
||
38 | |||
39 | /** |
||
40 | * IndexAction constructor. |
||
41 | * |
||
42 | * @param EnvironmentInterface $environmentManager |
||
43 | * @param StorageInterface[] ...$dataStorages |
||
44 | */ |
||
45 | public function __construct(EnvironmentInterface $environmentManager, StorageInterface ...$dataStorages) |
||
46 | { |
||
47 | $this->environmentManager = $environmentManager; |
||
48 | |||
49 | foreach ($dataStorages as $storage) { |
||
50 | $this->dataStorages[get_class($storage)] = $storage; |
||
51 | } |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Returns a stored storage instance. |
||
56 | * |
||
57 | * @param string $storageClass |
||
58 | * @return StorageInterface |
||
59 | */ |
||
60 | private function getStorage(string $storageClass) : StorageInterface |
||
61 | { |
||
62 | if (!isset($this->dataStorages[$storageClass])) { |
||
63 | throw new InvalidArgumentException( |
||
64 | sprintf('Storage class reference "%s" is not defined in this class.', $storageClass), |
||
65 | 1000 |
||
66 | ); |
||
67 | } |
||
68 | |||
69 | return $this->dataStorages[$storageClass]; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Gets the application storage instance. |
||
74 | * |
||
75 | * @return Storage\ApplicationStorage |
||
76 | */ |
||
77 | protected function getApplicationStorage() : Storage\ApplicationStorage |
||
78 | { |
||
79 | /** @var Storage\ApplicationStorage $storage */ |
||
80 | $storage = $this->getStorage(Storage\ApplicationStorage::class); |
||
81 | |||
82 | return $storage; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Gets the filesystem storage instance. |
||
87 | * |
||
88 | * @return Storage\FilesystemStorage |
||
89 | */ |
||
90 | protected function getFilesystemStorage() : Storage\FilesystemStorage |
||
91 | { |
||
92 | /** @var Storage\FilesystemStorage $storage */ |
||
93 | $storage = $this->getStorage(Storage\FilesystemStorage::class); |
||
94 | |||
95 | return $storage; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Gets the user storage instance. |
||
100 | * |
||
101 | * @return Storage\UserStorage |
||
102 | */ |
||
103 | protected function getUserStorage() : Storage\UserStorage |
||
104 | { |
||
105 | /** @var Storage\UserStorage $storage */ |
||
106 | $storage = $this->getStorage(Storage\UserStorage::class); |
||
107 | |||
108 | return $storage; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Gets template map name or template file path. |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | public function getTemplateName() : string |
||
120 | |||
121 | /** |
||
122 | * Gets template data. |
||
123 | * |
||
124 | * @return array |
||
125 | */ |
||
126 | public function getTemplateData() : array |
||
156 | |||
157 | /** |
||
158 | * Gets application data to render. |
||
159 | * |
||
160 | * @param Entity\ApplicationEntity $applicationEntity |
||
161 | * @return array |
||
162 | */ |
||
163 | protected function getApplicationData(Entity\ApplicationEntity $applicationEntity) : array |
||
175 | |||
176 | /** |
||
177 | * Collets the blog post data |
||
178 | * |
||
179 | * @param Entity\ApplicationEntity $applicationEntity |
||
180 | * @param Entity\FilesystemPublishedDocumentEntity $publishedDocumentEntity |
||
181 | * @return array |
||
182 | */ |
||
183 | protected function getBlobPostData( |
||
215 | |||
216 | /** |
||
217 | * Gets author information for a filesystem record. |
||
218 | * |
||
219 | * @param int $applicationId |
||
220 | * @param int $userId |
||
221 | * @return array |
||
222 | */ |
||
223 | View Code Duplication | protected function getPublicationAuthor(int $applicationId, int $userId) : array |
|
250 | |||
251 | /** |
||
252 | * Collects all the tags for a filesystem record. |
||
253 | * |
||
254 | * @param int $applicationId |
||
255 | * @param int $filesystemId |
||
256 | * @return array |
||
257 | */ |
||
258 | protected function getPublicationTags(int $applicationId, int $filesystemId) : array |
||
286 | |||
287 | /** |
||
288 | * Gets the category for a filesystem record. |
||
289 | * |
||
290 | * @param int $applicationId |
||
291 | * @param int $categoryId |
||
292 | * @return array |
||
293 | */ |
||
294 | View Code Duplication | protected function getPublicationCategory(int $applicationId, int $categoryId) : array |
|
315 | } |
||
316 |
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.