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 |
||
31 | class ApiController extends Controller |
||
32 | { |
||
33 | /** |
||
34 | * Get a value from a key. |
||
35 | * |
||
36 | * @param Request $request |
||
37 | * @param string $key |
||
38 | * |
||
39 | * @throw Webmozart\KeyValueStore\Api\NoSuchKeyException |
||
40 | * |
||
41 | * @return KeyValue |
||
42 | */ |
||
43 | View Code Duplication | public function getAction(Request $request, $key) |
|
53 | |||
54 | /** |
||
55 | * Create a key value. |
||
56 | * |
||
57 | * @param Request $request |
||
58 | * |
||
59 | * @return mixed|FormInterface |
||
60 | */ |
||
61 | public function createAction(Request $request) |
||
62 | { |
||
63 | return $this->handleForm( |
||
64 | $this->createRestForm(new KeyValueType(), new KeyValue()), |
||
65 | $request |
||
66 | ); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Create a key. |
||
71 | * |
||
72 | * @param Request $request |
||
73 | * @param string $key |
||
74 | * |
||
75 | * @return Response |
||
76 | */ |
||
77 | public function updateAction(Request $request, $key) |
||
78 | { |
||
79 | $storeHandler = $this->get('maikuro_distributed_configuration.store_handler'); |
||
80 | $keyValue = $storeHandler->get($key); |
||
81 | |||
82 | return $this->handleForm($this->createRestForm( |
||
83 | new KeyValueType(), |
||
84 | $keyValue, |
||
85 | ['method' => $request->getMethod()] |
||
86 | ), $request); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Delete a key. |
||
91 | * |
||
92 | * @param Request $request |
||
93 | * @param string $key |
||
94 | * |
||
95 | * @throw Webmozart\KeyValueStore\Api\WriteException |
||
96 | * |
||
97 | * @return Response |
||
98 | */ |
||
99 | View Code Duplication | public function deleteAction(Request $request, $key) |
|
110 | |||
111 | /** |
||
112 | * handleForm. |
||
113 | * |
||
114 | * @param FormInterface $form |
||
115 | * @param Request $request |
||
116 | * |
||
117 | * @return Response |
||
118 | */ |
||
119 | protected function handleForm(FormInterface $form, Request $request) |
||
120 | { |
||
121 | $method = $request->getMethod(); |
||
122 | $form->handleRequest($request); |
||
123 | |||
124 | if ($form->isSubmitted() && $form->isValid()) { |
||
125 | $entity = $form->getData(); |
||
126 | |||
127 | $storeHandler = $this->get('maikuro_distributed_configuration.store_handler'); |
||
128 | $storeHandler->flush($entity); |
||
129 | |||
130 | if (in_array($method, ['PUT', 'PATCH'])) { |
||
131 | return $this->getViewHandler()->handle($this->onUpdateSuccess($entity)); |
||
132 | } |
||
133 | |||
134 | return $this->getViewHandler()->handle($this->onCreateSuccess($entity)); |
||
135 | } |
||
136 | |||
137 | return $this->getViewHandler()->handle($this->onError($form)); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * onUpdateSuccess. |
||
142 | * |
||
143 | * @param KeyValue $entity |
||
144 | * |
||
145 | * @return View |
||
146 | */ |
||
147 | protected function onUpdateSuccess(KeyValue $entity) |
||
148 | { |
||
149 | return View::create() |
||
150 | ->setStatusCode(Codes::HTTP_NO_CONTENT) |
||
151 | ; |
||
152 | |||
153 | return $view; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Returns a HTTP_BAD_REQUEST response when the form submission fails. |
||
158 | * |
||
159 | * @param KeyValue $entity |
||
160 | * |
||
161 | * @return View |
||
162 | */ |
||
163 | protected function onCreateSuccess(KeyValue $entity) |
||
164 | { |
||
165 | $view = View::create() |
||
166 | ->setStatusCode(Codes::HTTP_CREATED) |
||
167 | ->setData( |
||
168 | $entity |
||
169 | ) |
||
170 | ; |
||
171 | |||
172 | return $view; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Returns a HTTP_BAD_REQUEST response when the form submission fails. |
||
177 | * |
||
178 | * @param FormInterface $form |
||
179 | * |
||
180 | * @return View |
||
181 | */ |
||
182 | protected function onError(FormInterface $form) |
||
191 | |||
192 | /** |
||
193 | * createRestForm. |
||
194 | * |
||
195 | * @param string $type |
||
196 | * @param mixed $data |
||
197 | * @param array $options |
||
198 | * |
||
199 | * @return FormInterface |
||
200 | */ |
||
201 | protected function createRestForm($type, $data = null, array $options = []) |
||
210 | |||
211 | /** |
||
212 | * ViewHandler. |
||
213 | */ |
||
214 | protected function getViewHandler() |
||
218 | } |
||
219 |
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.