Conditions | 9 |
Paths | 17 |
Total Lines | 92 |
Lines | 9 |
Ratio | 9.78 % |
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 |
||
54 | public function index(Request $request, $id = null) |
||
55 | { |
||
56 | $this->entityManager->getFilters()->enable('incomplete_order_status_hidden'); |
||
57 | // 編集 |
||
58 | if ($id) { |
||
59 | $Customer = $this->customerRepository |
||
60 | ->find($id); |
||
61 | |||
62 | if (is_null($Customer)) { |
||
63 | throw new NotFoundHttpException(); |
||
64 | } |
||
65 | |||
66 | $oldStatusId = $Customer->getStatus()->getId(); |
||
67 | // 編集用にデフォルトパスワードをセット |
||
68 | $previous_password = $Customer->getPassword(); |
||
69 | $Customer->setPassword($this->eccubeConfig['eccube_default_password']); |
||
70 | // 新規登録 |
||
71 | } else { |
||
72 | $Customer = $this->customerRepository->newCustomer(); |
||
73 | $Customer->setBuyTimes(0); |
||
74 | $Customer->setBuyTotal(0); |
||
75 | $oldStatusId = null; |
||
76 | } |
||
77 | |||
78 | // 会員登録フォーム |
||
79 | $builder = $this->formFactory |
||
80 | ->createBuilder(CustomerType::class, $Customer); |
||
81 | |||
82 | $event = new EventArgs( |
||
83 | [ |
||
84 | 'builder' => $builder, |
||
85 | 'Customer' => $Customer, |
||
86 | ], |
||
87 | $request |
||
88 | ); |
||
89 | $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_EDIT_INDEX_INITIALIZE, $event); |
||
90 | |||
91 | $form = $builder->getForm(); |
||
92 | |||
93 | $form->handleRequest($request); |
||
94 | |||
95 | if ($form->isSubmitted()) { |
||
96 | if ($form->isValid()) { |
||
97 | log_info('会員登録開始', [$Customer->getId()]); |
||
98 | |||
99 | $encoder = $this->encoderFactory->getEncoder($Customer); |
||
100 | |||
101 | View Code Duplication | if ($Customer->getPassword() === $this->eccubeConfig['eccube_default_password']) { |
|
|
|||
102 | $Customer->setPassword($previous_password); |
||
103 | } else { |
||
104 | if ($Customer->getSalt() === null) { |
||
105 | $Customer->setSalt($encoder->createSalt()); |
||
106 | $Customer->setSecretKey($this->customerRepository->getUniqueSecretKey()); |
||
107 | } |
||
108 | $Customer->setPassword($encoder->encodePassword($Customer->getPassword(), $Customer->getSalt())); |
||
109 | } |
||
110 | |||
111 | // 退会ステータスに更新の場合、ダミーのアドレスに更新 |
||
112 | $newStatusId = $Customer->getStatus()->getId(); |
||
113 | if ($oldStatusId != $newStatusId && $newStatusId == CustomerStatus::WITHDRAWING) { |
||
114 | $Customer->setEmail(StringUtil::random(60).'@dummy.dummy'); |
||
115 | } |
||
116 | |||
117 | $this->entityManager->persist($Customer); |
||
118 | $this->entityManager->flush(); |
||
119 | |||
120 | log_info('会員登録完了', [$Customer->getId()]); |
||
121 | |||
122 | $event = new EventArgs( |
||
123 | [ |
||
124 | 'form' => $form, |
||
125 | 'Customer' => $Customer, |
||
126 | ], |
||
127 | $request |
||
128 | ); |
||
129 | $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_EDIT_INDEX_COMPLETE, $event); |
||
130 | |||
131 | $this->addSuccess('admin.customer.save.complete', 'admin'); |
||
132 | |||
133 | return $this->redirectToRoute('admin_customer_edit', [ |
||
134 | 'id' => $Customer->getId(), |
||
135 | ]); |
||
136 | } else { |
||
137 | $this->addError('admin.customer.save.failed', 'admin'); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | return [ |
||
142 | 'form' => $form->createView(), |
||
143 | 'Customer' => $Customer, |
||
144 | ]; |
||
145 | } |
||
146 | } |
||
147 |
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.