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 |
||
30 | class RaCandidateController extends Controller |
||
31 | { |
||
32 | /** |
||
33 | * @var RaCandidateService |
||
34 | */ |
||
35 | private $raCandidateService; |
||
36 | |||
37 | public function __construct(RaCandidateService $raCandidateService) |
||
41 | |||
42 | /** |
||
43 | * @param Institution $institution |
||
44 | * @param Request $request |
||
45 | * @return JsonCollectionResponse |
||
46 | */ |
||
47 | View Code Duplication | public function searchAction(Institution $institution, Request $request) |
|
|
|||
48 | { |
||
49 | $this->denyAccessUnlessGranted(['ROLE_RA']); |
||
50 | |||
51 | $query = new RaCandidateQuery(); |
||
52 | $query->institution = $institution; |
||
53 | $query->commonName = $request->get('commonName'); |
||
54 | $query->email = $request->get('email'); |
||
55 | $query->secondFactorTypes = $request->get('secondFactorTypes'); |
||
56 | $query->pageNumber = (int) $request->get('p', 1); |
||
57 | |||
58 | $paginator = $this->raCandidateService->search($query); |
||
59 | |||
60 | return JsonCollectionResponse::fromPaginator($paginator); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param Request $request |
||
65 | * @return JsonResponse |
||
66 | */ |
||
67 | public function getAction(Request $request) |
||
81 | } |
||
82 |
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.