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 |
||
14 | class MyController |
||
15 | { |
||
16 | /** |
||
17 | * @param Application $app |
||
18 | * |
||
19 | * @return Response |
||
20 | */ |
||
21 | public function indexAction(Application $app) |
||
27 | |||
28 | /** |
||
29 | * @param Application $app |
||
30 | * |
||
31 | * @return Response |
||
32 | */ |
||
33 | public function profileAction(Application $app) |
||
41 | |||
42 | /** |
||
43 | * @param Request $request |
||
44 | * @param Application $app |
||
45 | * |
||
46 | * @return Response |
||
47 | */ |
||
48 | public function settingsAction(Request $request, Application $app) |
||
49 | { |
||
50 | $data = array(); |
||
51 | |||
52 | $form = $app['form.factory']->create( |
||
53 | new SettingsType(), |
||
54 | $app['user'] |
||
55 | ); |
||
56 | |||
57 | View Code Duplication | if ($request->getMethod() == 'POST') { |
|
|
|||
58 | $form->handleRequest($request); |
||
59 | |||
60 | if ($form->isValid()) { |
||
61 | $userEntity = $form->getData(); |
||
62 | |||
63 | /*** Image ***/ |
||
64 | $userEntity |
||
65 | ->getProfile() |
||
66 | ->setImageUploadPath($app['baseUrl'].'/assets/uploads/') |
||
67 | ->setImageUploadDir(WEB_DIR.'/assets/uploads/') |
||
68 | ->imageUpload() |
||
69 | ; |
||
70 | |||
71 | $app['orm.em']->persist($userEntity); |
||
72 | $app['orm.em']->flush(); |
||
73 | |||
74 | $app['flashbag']->add( |
||
75 | 'success', |
||
76 | $app['translator']->trans( |
||
77 | 'Your settings were successfully saved!' |
||
78 | ) |
||
79 | ); |
||
80 | } else { |
||
81 | $app['orm.em']->refresh($app['user']); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | $data['form'] = $form->createView(); |
||
86 | |||
87 | return new Response( |
||
88 | $app['twig']->render( |
||
89 | 'contents/members-area/my/settings.html.twig', |
||
90 | $data |
||
91 | ) |
||
92 | ); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param Request $request |
||
97 | * @param Application $app |
||
98 | * |
||
99 | * @return Response |
||
100 | */ |
||
101 | public function passwordAction(Request $request, Application $app) |
||
144 | } |
||
145 |
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.