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 |
||
41 | class ThemingController extends Controller { |
||
42 | /** @var Template */ |
||
43 | private $template; |
||
44 | /** @var IL10N */ |
||
45 | private $l; |
||
46 | /** @var IConfig */ |
||
47 | private $config; |
||
48 | /** @var IRootFolder */ |
||
49 | private $rootFolder; |
||
50 | |||
51 | /** |
||
52 | * ThemingController constructor. |
||
53 | * |
||
54 | * @param string $appName |
||
55 | * @param IRequest $request |
||
56 | * @param IConfig $config |
||
57 | * @param Template $template |
||
58 | * @param IL10N $l |
||
59 | * @param IRootFolder $rootFolder |
||
60 | */ |
||
61 | public function __construct( |
||
62 | $appName, |
||
63 | IRequest $request, |
||
64 | IConfig $config, |
||
65 | Template $template, |
||
66 | IL10N $l, |
||
67 | IRootFolder $rootFolder |
||
68 | ) { |
||
69 | parent::__construct($appName, $request); |
||
70 | |||
71 | $this->template = $template; |
||
72 | $this->l = $l; |
||
73 | $this->config = $config; |
||
74 | $this->rootFolder = $rootFolder; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param string $setting |
||
79 | * @param string $value |
||
80 | * @return DataResponse |
||
81 | * @internal param string $color |
||
82 | */ |
||
83 | View Code Duplication | public function updateStylesheet($setting, $value) { |
|
|
|||
84 | $this->template->set($setting, $value); |
||
85 | return new DataResponse( |
||
86 | [ |
||
87 | 'data' => |
||
88 | [ |
||
89 | 'message' => $this->l->t('Saved') |
||
90 | ], |
||
91 | 'status' => 'success' |
||
92 | ] |
||
93 | ); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Update the logos and background image |
||
98 | * |
||
99 | * @return DataResponse |
||
100 | */ |
||
101 | public function updateLogo() { |
||
102 | $newLogo = $this->request->getUploadedFile('uploadlogo'); |
||
103 | $newBackgroundLogo = $this->request->getUploadedFile('upload-login-background'); |
||
104 | View Code Duplication | if (empty($newLogo) && empty($newBackgroundLogo)) { |
|
105 | return new DataResponse( |
||
106 | [ |
||
107 | 'data' => [ |
||
108 | 'message' => $this->l->t('No file uploaded') |
||
109 | ] |
||
110 | ], |
||
111 | Http::STATUS_UNPROCESSABLE_ENTITY); |
||
112 | } |
||
113 | $name = ''; |
||
114 | View Code Duplication | if(!empty($newLogo)) { |
|
115 | $target = $this->rootFolder->newFile('themedinstancelogo'); |
||
116 | stream_copy_to_stream(fopen($newLogo['tmp_name'], 'r'), $target->fopen('w')); |
||
117 | $this->template->set('logoMime', $newLogo['type']); |
||
118 | $name = $newLogo['name']; |
||
119 | } |
||
120 | View Code Duplication | if(!empty($newBackgroundLogo)) { |
|
121 | $target = $this->rootFolder->newFile('themedbackgroundlogo'); |
||
122 | stream_copy_to_stream(fopen($newBackgroundLogo['tmp_name'], 'r'), $target->fopen('w')); |
||
123 | $this->template->set('backgroundMime', $newBackgroundLogo['type']); |
||
124 | $name = $newBackgroundLogo['name']; |
||
125 | } |
||
126 | |||
127 | return new DataResponse( |
||
128 | [ |
||
129 | 'data' => |
||
130 | [ |
||
131 | 'name' => $name, |
||
132 | 'message' => $this->l->t('Saved') |
||
133 | ], |
||
134 | 'status' => 'success' |
||
135 | ] |
||
136 | ); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Revert setting to default value |
||
141 | * |
||
142 | * @param string $setting setting which should be reverted |
||
143 | * @return DataResponse |
||
144 | */ |
||
145 | View Code Duplication | public function undo($setting) { |
|
146 | $value = $this->template->undo($setting); |
||
147 | return new DataResponse( |
||
148 | [ |
||
149 | 'data' => |
||
150 | [ |
||
151 | 'value' => $value, |
||
152 | 'message' => $this->l->t('Saved') |
||
153 | ], |
||
154 | 'status' => 'success' |
||
155 | ] |
||
156 | ); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @PublicPage |
||
161 | * @NoCSRFRequired |
||
162 | * |
||
163 | * @return Http\StreamResponse |
||
164 | */ |
||
165 | View Code Duplication | public function getLogo() { |
|
179 | |||
180 | /** |
||
181 | * @PublicPage |
||
182 | * @NoCSRFRequired |
||
183 | * |
||
184 | * @return Http\StreamResponse |
||
185 | */ |
||
186 | View Code Duplication | public function getLoginBackground() { |
|
200 | |||
201 | /** |
||
202 | * @NoCSRFRequired |
||
203 | * @PublicPage |
||
204 | * |
||
205 | * @return Http\DataDownloadResponse |
||
206 | */ |
||
207 | public function getStylesheet() { |
||
241 | } |
||
242 |
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.