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 |
||
31 | class IdentityController extends Controller |
||
32 | { |
||
33 | public function getAction($id) |
||
34 | { |
||
35 | $this->denyAccessUnlessGranted(['ROLE_RA', 'ROLE_SS']); |
||
36 | |||
37 | $identity = $this->getService()->find($id); |
||
38 | |||
39 | if ($identity === null) { |
||
40 | throw new NotFoundHttpException(sprintf("Identity '%s' does not exist", $id)); |
||
41 | } |
||
42 | |||
43 | return new JsonResponse($identity); |
||
44 | } |
||
45 | |||
46 | public function collectionAction(Request $request, Institution $institution) |
||
47 | { |
||
48 | $this->denyAccessUnlessGranted(['ROLE_RA', 'ROLE_SS']); |
||
49 | |||
50 | $query = new IdentityQuery(); |
||
51 | $query->institution = $institution; |
||
52 | $query->nameId = $request->get('NameID'); |
||
53 | $query->commonName = $request->get('commonName'); |
||
54 | $query->email = $request->get('email'); |
||
55 | $query->pageNumber = (int) $request->get('p', 1); |
||
56 | |||
57 | $paginator = $this->getService()->search($query); |
||
58 | |||
59 | return JsonCollectionResponse::fromPaginator($paginator); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param string $identityId |
||
64 | * @return \Symfony\Component\HttpFoundation\Response |
||
65 | */ |
||
66 | View Code Duplication | public function getRegistrationAuthorityCredentialsAction($identityId) |
|
80 | |||
81 | /** |
||
82 | * @return IdentityService |
||
83 | */ |
||
84 | private function getService() |
||
88 | } |
||
89 |
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.