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 |
||
37 | class VettingController extends Controller |
||
38 | { |
||
39 | /** |
||
40 | * @Template |
||
41 | * @param Request $request |
||
42 | * @return array|Response |
||
43 | */ |
||
44 | public function startProcedureAction(Request $request) |
||
130 | |||
131 | public function cancelProcedureAction($procedureId) |
||
145 | |||
146 | /** |
||
147 | * @Template |
||
148 | * @param Request $request |
||
149 | * @param string $procedureId |
||
150 | * @return array|Response |
||
151 | */ |
||
152 | public function verifyIdentityAction(Request $request, $procedureId) |
||
153 | { |
||
154 | $this->denyAccessUnlessGranted(['ROLE_RA']); |
||
155 | |||
156 | $logger = $this->get('ra.procedure_logger')->forProcedure($procedureId); |
||
157 | $logger->notice('Verify Identity Form requested'); |
||
158 | |||
159 | View Code Duplication | if (!$this->getVettingService()->hasProcedure($procedureId)) { |
|
160 | $logger->notice(sprintf('Vetting procedure "%s" not found', $procedureId)); |
||
161 | throw new NotFoundHttpException(sprintf('Vetting procedure "%s" not found', $procedureId)); |
||
162 | } |
||
163 | |||
164 | $command = new VerifyIdentityCommand(); |
||
165 | $form = $this->createForm('ra_verify_identity', $command)->handleRequest($request); |
||
166 | |||
167 | /** @var SubmitButton $cancelButton */ |
||
168 | $cancelButton = $form->get('cancel'); |
||
169 | View Code Duplication | if ($cancelButton->isClicked()) { |
|
170 | $this->getVettingService()->cancelProcedure($procedureId); |
||
171 | $this->addFlash('info', $this->get('translator')->trans('ra.vetting.flash.cancelled')); |
||
172 | |||
173 | return $this->redirectToRoute('ra_vetting_search'); |
||
174 | } |
||
175 | |||
176 | $vettingService = $this->getVettingService(); |
||
177 | $commonName = $vettingService->getIdentityCommonName($procedureId); |
||
178 | |||
179 | $showForm = function ($error = null) use ($form, $commonName) { |
||
180 | if ($error) { |
||
181 | $form->addError(new FormError($error)); |
||
182 | } |
||
183 | |||
184 | return ['commonName' => $commonName, 'form' => $form->createView()]; |
||
185 | }; |
||
186 | |||
187 | if (!$form->isValid()) { |
||
188 | $logger->notice('Verify Identity Form not submitted, displaying form'); |
||
189 | |||
190 | return $showForm(); |
||
191 | } |
||
192 | |||
193 | try { |
||
194 | $vettingService->verifyIdentity($procedureId, $command); |
||
195 | } catch (DomainException $e) { |
||
196 | $this->get('logger')->error( |
||
197 | "RA attempted to verify identity, but the vetting procedure does not allow it", |
||
198 | ['exception' => $e, 'procedure' => $procedureId] |
||
199 | ); |
||
200 | |||
201 | return $showForm('ra.verify_identity.identity_verification_failed'); |
||
202 | } |
||
203 | |||
204 | try { |
||
205 | $vetted = $vettingService->vet($procedureId); |
||
206 | if ($vetted) { |
||
207 | $logger->notice('Identity Verified, vetting completed'); |
||
208 | |||
209 | return $this->redirectToRoute('ra_vetting_completed', ['procedureId' => $procedureId]); |
||
210 | } |
||
211 | |||
212 | $logger->error('RA attempted to vet second factor, but the command failed'); |
||
213 | |||
214 | return $showForm('ra.verify_identity.second_factor_vetting_failed'); |
||
215 | } catch (DomainException $e) { |
||
216 | $logger->error( |
||
217 | "RA attempted to vet second factor, but the vetting procedure didn't allow it", |
||
218 | ['exception' => $e] |
||
219 | ); |
||
220 | |||
221 | return $showForm('ra.verify_identity.second_factor_vetting_failed'); |
||
222 | } |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @Template |
||
227 | */ |
||
228 | public function vettingCompletedAction() |
||
232 | |||
233 | /** |
||
234 | * @return SecondFactorService |
||
235 | */ |
||
236 | private function getSecondFactorService() |
||
240 | |||
241 | /** |
||
242 | * @return VettingService |
||
243 | */ |
||
244 | private function getVettingService() |
||
248 | |||
249 | /** |
||
250 | * @return \Surfnet\StepupMiddlewareClientBundle\Identity\Dto\Identity |
||
251 | */ |
||
252 | private function getIdentity() |
||
256 | } |
||
257 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.