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 |
||
22 | class LeadController extends Controller |
||
23 | { |
||
24 | /** |
||
25 | * @Route("/view/{id}", name="orocrm_sales_lead_view", requirements={"id"="\d+"}) |
||
26 | * @Template |
||
27 | * @Acl( |
||
28 | * id="orocrm_sales_lead_view", |
||
29 | * type="entity", |
||
30 | * permission="VIEW", |
||
31 | * class="OroCRMSalesBundle:Lead" |
||
32 | * ) |
||
33 | */ |
||
34 | public function viewAction(Lead $lead) |
||
40 | |||
41 | /** |
||
42 | * @Route("/info/{id}", name="orocrm_sales_lead_info", requirements={"id"="\d+"}) |
||
43 | * @AclAncestor("orocrm_sales_lead_view") |
||
44 | * @Template() |
||
45 | */ |
||
46 | public function infoAction(Lead $lead) |
||
52 | |||
53 | /** |
||
54 | * @Route("/address-book/{id}", name="orocrm_sales_lead_address_book", requirements={"id"="\d+"}) |
||
55 | * @AclAncestor("orocrm_sales_lead_view") |
||
56 | * @Template() |
||
57 | */ |
||
58 | public function addressBookAction(Lead $lead) |
||
64 | |||
65 | /** |
||
66 | * Create lead form |
||
67 | * @Route("/create", name="orocrm_sales_lead_create") |
||
68 | * @Template("OroCRMSalesBundle:Lead:update.html.twig") |
||
69 | * @Acl( |
||
70 | * id="orocrm_sales_lead_create", |
||
71 | * type="entity", |
||
72 | * permission="CREATE", |
||
73 | * class="OroCRMSalesBundle:Lead" |
||
74 | * ) |
||
75 | */ |
||
76 | public function createAction() |
||
80 | |||
81 | /** |
||
82 | * Update user form |
||
83 | * @Route("/update/{id}", name="orocrm_sales_lead_update", requirements={"id"="\d+"}, defaults={"id"=0}) |
||
84 | * |
||
85 | * @Template |
||
86 | * @Acl( |
||
87 | * id="orocrm_sales_lead_update", |
||
88 | * type="entity", |
||
89 | * permission="EDIT", |
||
90 | * class="OroCRMSalesBundle:Lead" |
||
91 | * ) |
||
92 | */ |
||
93 | public function updateAction(Lead $entity) |
||
97 | |||
98 | /** |
||
99 | * @Route( |
||
100 | * "/{_format}", |
||
101 | * name="orocrm_sales_lead_index", |
||
102 | * requirements={"_format"="html|json"}, |
||
103 | * defaults={"_format" = "html"} |
||
104 | * ) |
||
105 | * @Template |
||
106 | * @AclAncestor("orocrm_sales_lead_view") |
||
107 | */ |
||
108 | public function indexAction() |
||
114 | |||
115 | /** |
||
116 | * @Route("/widget/account-leads/{id}", name="orocrm_sales_widget_account_leads", requirements={"id"="\d+"}) |
||
117 | * @AclAncestor("orocrm_sales_lead_view") |
||
118 | * @Template() |
||
119 | */ |
||
120 | public function accountLeadsAction(Account $account) |
||
124 | |||
125 | /** |
||
126 | * Create lead form with data channel |
||
127 | * |
||
128 | * @Route("/create/{channelIds}", name="orocrm_sales_lead_data_channel_aware_create") |
||
129 | * @Template("OroCRMSalesBundle:Lead:update.html.twig") |
||
130 | * @AclAncestor("orocrm_sales_lead_view") |
||
131 | * |
||
132 | * @ParamConverter( |
||
133 | * "channel", |
||
134 | * class="OroCRMChannelBundle:Channel", |
||
135 | * options={"id" = "channelIds"} |
||
136 | * ) |
||
137 | */ |
||
138 | public function leadWithDataChannelCreateAction(Channel $channel) |
||
145 | |||
146 | /** |
||
147 | * @Route("/datagrid/lead-with-datachannel/{channelIds}", name="orocrm_sales_datagrid_lead_datachannel_aware") |
||
148 | * @Template("OroCRMSalesBundle:Widget:entityWithDataChannelGrid.html.twig") |
||
149 | * @AclAncestor("orocrm_sales_lead_view") |
||
150 | */ |
||
151 | View Code Duplication | public function leadWithDataChannelGridAction($channelIds, Request $request) |
|
167 | |||
168 | /** |
||
169 | * @param Lead $entity |
||
170 | * |
||
171 | * @return array |
||
172 | */ |
||
173 | View Code Duplication | protected function update(Lead $entity) |
|
194 | } |
||
195 |
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.