Conditions | 12 |
Paths | 26 |
Total Lines | 87 |
Lines | 9 |
Ratio | 10.34 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
69 | public function index(Request $request, $class_name_id, $id = null) |
||
70 | { |
||
71 | $ClassName = $this->classNameRepository->find($class_name_id); |
||
72 | if (!$ClassName) { |
||
73 | throw new NotFoundHttpException(); |
||
74 | } |
||
75 | if ($id) { |
||
76 | $TargetClassCategory = $this->classCategoryRepository->find($id); |
||
77 | if (!$TargetClassCategory || $TargetClassCategory->getClassName() != $ClassName) { |
||
78 | throw new NotFoundHttpException(); |
||
79 | } |
||
80 | } else { |
||
81 | $TargetClassCategory = new \Eccube\Entity\ClassCategory(); |
||
82 | $TargetClassCategory->setClassName($ClassName); |
||
83 | } |
||
84 | |||
85 | $builder = $this->formFactory |
||
86 | ->createBuilder(ClassCategoryType::class, $TargetClassCategory); |
||
87 | |||
88 | $event = new EventArgs( |
||
89 | [ |
||
90 | 'builder' => $builder, |
||
91 | 'ClassName' => $ClassName, |
||
92 | 'TargetClassCategory' => $TargetClassCategory, |
||
93 | ], |
||
94 | $request |
||
95 | ); |
||
96 | $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CLASS_CATEGORY_INDEX_INITIALIZE, $event); |
||
97 | |||
98 | $ClassCategories = $this->classCategoryRepository->getList($ClassName); |
||
99 | |||
100 | $forms = []; |
||
101 | foreach ($ClassCategories as $ClassCategory) { |
||
102 | $id = $ClassCategory->getId(); |
||
103 | $forms[$id] = $this->formFactory->createNamed('class_category_'.$id, ClassCategoryType::class, $ClassCategory); |
||
104 | } |
||
105 | |||
106 | $form = $builder->getForm(); |
||
107 | |||
108 | if ($request->getMethod() === 'POST') { |
||
109 | $form->handleRequest($request); |
||
110 | if ($form->isValid()) { |
||
111 | log_info('規格分類登録開始', [$id]); |
||
112 | |||
113 | $this->classCategoryRepository->save($TargetClassCategory); |
||
114 | |||
115 | log_info('規格分類登録完了', [$id]); |
||
116 | |||
117 | $event = new EventArgs( |
||
118 | [ |
||
119 | 'form' => $form, |
||
120 | 'ClassName' => $ClassName, |
||
121 | 'TargetClassCategory' => $TargetClassCategory, |
||
122 | ], |
||
123 | $request |
||
124 | ); |
||
125 | $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CLASS_CATEGORY_INDEX_COMPLETE, $event); |
||
126 | |||
127 | $this->addSuccess('admin.common.save_complete', 'admin'); |
||
128 | |||
129 | return $this->redirectToRoute('admin_product_class_category', ['class_name_id' => $ClassName->getId()]); |
||
130 | } |
||
131 | |||
132 | View Code Duplication | foreach ($forms as $editForm) { |
|
|
|||
133 | $editForm->handleRequest($request); |
||
134 | if ($editForm->isSubmitted() && $editForm->isValid()) { |
||
135 | $this->classCategoryRepository->save($editForm->getData()); |
||
136 | $this->addSuccess('admin.common.save_complete', 'admin'); |
||
137 | |||
138 | return $this->redirectToRoute('admin_product_class_category', ['class_name_id' => $ClassName->getId()]); |
||
139 | } |
||
140 | } |
||
141 | } |
||
142 | |||
143 | $formViews = []; |
||
144 | foreach ($forms as $key => $value) { |
||
145 | $formViews[$key] = $value->createView(); |
||
146 | } |
||
147 | |||
148 | return [ |
||
149 | 'form' => $form->createView(), |
||
150 | 'ClassName' => $ClassName, |
||
151 | 'ClassCategories' => $ClassCategories, |
||
152 | 'TargetClassCategory' => $TargetClassCategory, |
||
153 | 'forms' => $formViews, |
||
154 | ]; |
||
155 | } |
||
156 | |||
269 |
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.