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 |
||
50 | class CategoryController extends AbstractController |
||
51 | { |
||
52 | /** |
||
53 | * @Inject(CsvExportService::class) |
||
54 | * @var CsvExportService |
||
55 | */ |
||
56 | protected $csvExportService; |
||
57 | |||
58 | /** |
||
59 | * @Inject("orm.em") |
||
60 | * @var EntityManager |
||
61 | */ |
||
62 | protected $entityManager; |
||
63 | |||
64 | /** |
||
65 | * @Inject("config") |
||
66 | * @var array |
||
67 | */ |
||
68 | protected $appConfig; |
||
69 | |||
70 | /** |
||
71 | * @Inject("eccube.event.dispatcher") |
||
72 | * @var EventDispatcher |
||
73 | */ |
||
74 | protected $eventDispatcher; |
||
75 | |||
76 | /** |
||
77 | * @Inject("form.factory") |
||
78 | * @var FormFactory |
||
79 | */ |
||
80 | protected $formFactory; |
||
81 | |||
82 | /** |
||
83 | * @Inject(CategoryRepository::class) |
||
84 | * @var CategoryRepository |
||
85 | */ |
||
86 | protected $categoryRepository; |
||
87 | |||
88 | /** |
||
89 | * @Route("/{_admin}/product/category", name="admin_product_category") |
||
90 | * @Route("/{_admin}/product/category/{parent_id}", requirements={"parent_id" = "\d+"}, name="admin_product_category_show") |
||
91 | * @Route("/{_admin}/product/category/{id}/edit", requirements={"id" = "\d+"}, name="admin_product_category_edit") |
||
92 | * @Template("Product/category.twig") |
||
93 | */ |
||
94 | 5 | public function index(Application $app, Request $request, $parent_id = null, $id = null) |
|
182 | |||
183 | /** |
||
184 | * @Method("DELETE") |
||
185 | * @Route("/{_admin}/product/category/{id}/delete", requirements={"id" = "\d+"}, name="admin_product_category_delete") |
||
186 | */ |
||
187 | 1 | public function delete(Application $app, Request $request, $id) |
|
188 | { |
||
189 | 1 | $this->isTokenValid($app); |
|
190 | |||
191 | 1 | $TargetCategory = $this->categoryRepository->find($id); |
|
192 | 1 | if (!$TargetCategory) { |
|
193 | $app->deleteMessage(); |
||
194 | return $app->redirect($app->url('admin_product_category')); |
||
195 | } |
||
196 | 1 | $Parent = $TargetCategory->getParent(); |
|
197 | |||
198 | 1 | log_info('カテゴリ削除開始', array($id)); |
|
199 | |||
200 | try { |
||
201 | 1 | $this->categoryRepository->delete($TargetCategory); |
|
202 | |||
203 | 1 | $event = new EventArgs( |
|
204 | array( |
||
205 | 1 | 'Parent' => $Parent, |
|
206 | 1 | 'TargetCategory' => $TargetCategory, |
|
207 | 1 | ), $request |
|
208 | ); |
||
209 | 1 | $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CATEGORY_DELETE_COMPLETE, $event); |
|
210 | |||
211 | 1 | $app->addSuccess('admin.category.delete.complete', 'admin'); |
|
212 | |||
213 | 1 | log_info('カテゴリ削除完了', array($id)); |
|
214 | |||
215 | } catch (\Exception $e) { |
||
216 | log_info('カテゴリ削除エラー', [$id, $e]); |
||
217 | |||
218 | $message = $app->trans('admin.delete.failed.foreign_key', ['%name%' => 'カテゴリ']); |
||
219 | $app->addError($message, 'admin'); |
||
220 | } |
||
221 | |||
222 | 1 | if ($Parent) { |
|
223 | return $app->redirect($app->url('admin_product_category_show', array('parent_id' => $Parent->getId()))); |
||
224 | } else { |
||
225 | 1 | return $app->redirect($app->url('admin_product_category')); |
|
226 | } |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * @Method("POST") |
||
231 | * @Route("/{_admin}/product/category/sort_no/move", name="admin_product_category_sort_no_move") |
||
232 | */ |
||
233 | 2 | View Code Duplication | public function moveSortNo(Application $app, Request $request) |
234 | { |
||
235 | 2 | if ($request->isXmlHttpRequest()) { |
|
236 | 2 | $sortNos = $request->request->all(); |
|
237 | 2 | foreach ($sortNos as $categoryId => $sortNo) { |
|
238 | /* @var $Category \Eccube\Entity\Category */ |
||
239 | 2 | $Category = $this->categoryRepository |
|
240 | 2 | ->find($categoryId); |
|
241 | 2 | $Category->setSortNo($sortNo); |
|
242 | 2 | $this->entityManager->persist($Category); |
|
243 | } |
||
244 | 2 | $this->entityManager->flush(); |
|
245 | } |
||
246 | 2 | return true; |
|
247 | } |
||
248 | |||
249 | |||
250 | /** |
||
251 | * カテゴリCSVの出力. |
||
252 | * |
||
253 | * @Route("/{_admin}/product/category/export", name="admin_product_category_export") |
||
254 | * |
||
255 | * @param Application $app |
||
256 | * @param Request $request |
||
257 | * @return StreamedResponse |
||
258 | */ |
||
259 | public function export(Application $app, Request $request) |
||
325 | } |
||
326 |