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 |
||
15 | class LeadController extends StructureController |
||
16 | { |
||
17 | /** |
||
18 | * @ApiDoc( |
||
19 | * resource=true, |
||
20 | * description="Liste les leads.", |
||
21 | * section="Lead", |
||
22 | * parameters={ |
||
23 | * { |
||
24 | * "name"="offset", |
||
25 | * "dataType"="integer", |
||
26 | * "requirement"="\d+", |
||
27 | * "description"="starkerxp_structure.doc.offset.result", |
||
28 | * "required"="false" |
||
29 | * }, |
||
30 | * { |
||
31 | * "name"="limit", |
||
32 | * "dataType"="integer", |
||
33 | * "requirement"="\d+", |
||
34 | * "description"="starkerxp_structure.doc.limit.result", |
||
35 | * "required"="false" |
||
36 | * }, |
||
37 | * { |
||
38 | * "name"="fields", |
||
39 | * "dataType"="string", |
||
40 | * "requirement"="\w+", |
||
41 | * "description"="starkerxp_structure.doc.list_field.entity", |
||
42 | * "required"="false" |
||
43 | * }, |
||
44 | * { |
||
45 | * "name"="sort", |
||
46 | * "dataType"="string", |
||
47 | * "requirement"="\w+", |
||
48 | * "description"="starkerxp_structure.doc.sort.result", |
||
49 | * "required"="false" |
||
50 | * } |
||
51 | * }, |
||
52 | * views = { "default" } |
||
53 | * ) |
||
54 | * |
||
55 | */ |
||
56 | public function cgetAction(Request $request) |
||
78 | |||
79 | |||
80 | /** |
||
81 | * @ApiDoc( |
||
82 | * resource=true, |
||
83 | * description="Affiche un lead.", |
||
84 | * section="Lead", |
||
85 | * requirements={ |
||
86 | * { |
||
87 | * "name"="lead_id", |
||
88 | * "dataType"="integer", |
||
89 | * "requirement"="\d+", |
||
90 | * "description"="Show an element" |
||
91 | * } |
||
92 | * }, |
||
93 | * parameters={ |
||
94 | * { |
||
95 | * "name"="fields", |
||
96 | * "dataType"="string", |
||
97 | * "requirement"="\w+", |
||
98 | * "description"="starkerxp_structure.doc.list_field.entity", |
||
99 | * "required"="false" |
||
100 | * } |
||
101 | * }, |
||
102 | * views = { "default" } |
||
103 | * ) |
||
104 | */ |
||
105 | View Code Duplication | public function getAction(Request $request) |
|
|
|||
106 | { |
||
107 | $manager = $this->get("starkerxp_lead.manager.lead"); |
||
108 | try { |
||
109 | $options = $this->resolveParams()->resolve($request->query->all()); |
||
110 | /** @var Lead $entite */ |
||
111 | if (!$entite = $manager->findOneBy(['id' => $request->get('lead_id')])) { |
||
112 | return new JsonResponse(["payload" => $this->translate("entity.not_found", "lead")], 404); |
||
113 | } |
||
114 | } catch (\Exception $e) { |
||
115 | return new JsonResponse(["payload" => $e->getMessage()], 400); |
||
116 | } |
||
117 | |||
118 | $retour = $manager->toArray($entite, $this->getFields($options['fields'])); |
||
119 | |||
120 | return new JsonResponse($retour); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @ApiDoc( |
||
125 | * resource=true, |
||
126 | * description="Ajoute un lead.", |
||
127 | * section="Lead", |
||
128 | * views = { "default" } |
||
129 | * ) |
||
130 | */ |
||
131 | View Code Duplication | public function postAction(Request $request) |
|
132 | { |
||
133 | $manager = $this->get("starkerxp_lead.manager.lead"); |
||
134 | try { |
||
135 | $entite = new Lead(); |
||
136 | $form = $this->createForm(LeadType::class, $entite, ['method' => 'POST']); |
||
137 | $form->submit($this->getRequestData($request)); |
||
138 | if ($form->isValid()) { |
||
139 | $lead = $form->getData(); |
||
140 | $manager->insert($lead); |
||
141 | $this->dispatch(Events::LEAD_CREATED, new GenericEvent($lead)); |
||
142 | |||
143 | return new JsonResponse(["payload" => $this->translate("entity.created", "lead")], 201); |
||
144 | } |
||
145 | } catch (\Exception $e) { |
||
146 | $manager->rollback(); |
||
147 | |||
148 | return new JsonResponse(["payload" => $e->getMessage()], 400); |
||
149 | } |
||
150 | |||
151 | return new JsonResponse(["payload" => $this->getFormErrors($form)], 400); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @ApiDoc( |
||
156 | * resource=true, |
||
157 | * description="Edit lead.", |
||
158 | * section="Lead", |
||
159 | * requirements={ |
||
160 | * { |
||
161 | * "name"="lead_id", |
||
162 | * "dataType"="integer", |
||
163 | * "requirement"="\d+", |
||
164 | * "description"="Edit an element." |
||
165 | * } |
||
166 | * }, |
||
167 | * views = { "default" } |
||
168 | * ) |
||
169 | */ |
||
170 | public function putAction(Request $request) |
||
195 | |||
196 | /** |
||
197 | * @ApiDoc( |
||
198 | * resource=true, |
||
199 | * description="Edit lead.", |
||
200 | * section="Lead", |
||
201 | * views = { "default" } |
||
202 | * ) |
||
203 | */ |
||
204 | public function putWithoutIdAction(Request $request) |
||
220 | } |
||
221 |
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.