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 |
||
16 | class BadgeCompletionController extends Controller |
||
17 | { |
||
18 | /** |
||
19 | * Lists all pending badge completions. |
||
20 | * |
||
21 | * @return Response |
||
22 | */ |
||
23 | View Code Duplication | public function indexAction() |
|
32 | |||
33 | /** |
||
34 | * Reject a badge completion by removing it from the database. |
||
35 | * |
||
36 | * @param string $id |
||
37 | * |
||
38 | * @return RedirectResponse |
||
39 | */ |
||
40 | View Code Duplication | public function rejectAction($id) |
|
63 | |||
64 | /** |
||
65 | * Accept a badge completion. |
||
66 | * |
||
67 | * @param string $id |
||
68 | * |
||
69 | * @return RedirectResponse |
||
70 | */ |
||
71 | View Code Duplication | public function acceptAction($id) |
|
102 | |||
103 | /** |
||
104 | * Give a badge to a user directly by creating a completed badge completion. |
||
105 | * |
||
106 | * @param Request $request |
||
107 | * |
||
108 | * @return Response |
||
109 | */ |
||
110 | public function giveAction(Request $request) |
||
111 | { |
||
112 | $badgeRepository = $this->get('badger.game.repository.badge'); |
||
113 | |||
114 | if ('POST' === $request->getMethod()) { |
||
115 | $validator = $this->get('validator'); |
||
116 | |||
117 | $user = $this->get('fos_user.user_manager')->findUserByUsername($request->get('user')); |
||
118 | $badge = $badgeRepository->find($request->get('badge')); |
||
119 | |||
120 | $token = new UsernamePasswordToken($user, 'none', 'none', $user->getRoles()); |
||
121 | $isUnlockable = $this->get('security.access.decision_manager')->decide($token, ['view'], $badge); |
||
122 | |||
123 | View Code Duplication | if (!$isUnlockable) { |
|
124 | $this->addFlash('error', sprintf('%s does not have access to badge "%s"', |
||
125 | $user->getUsername(), |
||
126 | $badge->getTitle() |
||
127 | )); |
||
128 | |||
129 | return $this->redirectToRoute('admin_unlocked_badge_give'); |
||
130 | } |
||
131 | |||
132 | $badgeCompletion = $this->get('badger.game.repository.badge_completion') |
||
133 | ->findOneBy([ |
||
134 | 'user' => $user, |
||
135 | 'badge' => $badge |
||
136 | ]); |
||
137 | |||
138 | if (null === $badgeCompletion) { |
||
139 | $badgeCompletion = $this->get('badger.game.badge_completion.factory') |
||
140 | ->create($user, $badge); |
||
141 | } |
||
142 | |||
143 | View Code Duplication | if (!$badgeCompletion->isPending()) { |
|
144 | $this->addFlash('error', sprintf('%s already has the badge "%s"', |
||
145 | $user->getUsername(), |
||
146 | $badge->getTitle() |
||
147 | )); |
||
148 | |||
149 | return $this->redirectToRoute('admin_unlocked_badge_give'); |
||
150 | } |
||
151 | |||
152 | $badgeCompletion->setPending(false); |
||
153 | $badgeCompletion->setCompletionDate(new \DateTime()); |
||
154 | |||
155 | $errors = $validator->validate($badgeCompletion); |
||
156 | |||
157 | if (0 === count($errors)) { |
||
158 | $this->get('badger.game.saver.badge_completion')->save($badgeCompletion); |
||
159 | |||
160 | $this->addFlash('notice', sprintf( |
||
161 | '%s successfully received the badge "%s"!', |
||
162 | $user->getUsername(), |
||
163 | $badge->getTitle() |
||
164 | )); |
||
165 | } else { |
||
166 | $this->addFlash('error', (string) $errors); |
||
167 | } |
||
168 | } |
||
169 | |||
170 | $badges = $badgeRepository->findAll(); |
||
171 | $usernames = $this->get('badger.user.repository.user')->getAllUsernames(); |
||
172 | |||
173 | return $this->render('@Game/unlocked-badges/give.html.twig', [ |
||
174 | 'badges' => json_encode($badges), |
||
175 | 'users' => json_encode($usernames) |
||
176 | ]); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Remove a badge from a user by removing the badge completion. |
||
181 | * |
||
182 | * @param Request $request |
||
183 | * |
||
184 | * @return Response |
||
185 | */ |
||
186 | public function removeAction(Request $request) |
||
225 | } |
||
226 |
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.