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 |
||
17 | View Code Duplication | class B2bCustomerEmailHandler |
|
|
|||
18 | { |
||
19 | /** @var FormInterface */ |
||
20 | protected $form; |
||
21 | |||
22 | /** @var Request */ |
||
23 | protected $request; |
||
24 | |||
25 | /** @var EntityManagerInterface */ |
||
26 | protected $manager; |
||
27 | |||
28 | /** @var B2bCustomerEmailDeleteValidator */ |
||
29 | protected $b2bCustomerEmailDeleteValidator; |
||
30 | |||
31 | /** @var SecurityFacade */ |
||
32 | protected $securityFacade; |
||
33 | |||
34 | /** |
||
35 | * @param FormInterface $form |
||
36 | * @param Request $request |
||
37 | * @param EntityManagerInterface $manager |
||
38 | * @param B2bCustomerEmailDeleteValidator $b2bCustomerEmailDeleteValidator |
||
39 | * @param SecurityFacade $securityFacade |
||
40 | */ |
||
41 | public function __construct( |
||
54 | |||
55 | /** |
||
56 | * Process form |
||
57 | * |
||
58 | * @param B2bCustomerEmail $entity |
||
59 | * |
||
60 | * @return bool True on successful processing, false otherwise |
||
61 | * |
||
62 | * @throws AccessDeniedException |
||
63 | */ |
||
64 | public function process(B2bCustomerEmail $entity) |
||
98 | |||
99 | /** |
||
100 | * @param $id |
||
101 | * @param ApiEntityManager $manager |
||
102 | * @throws \Exception |
||
103 | */ |
||
104 | public function handleDelete($id, ApiEntityManager $manager) |
||
120 | |||
121 | /** |
||
122 | * @param B2bCustomerEmail $entity |
||
123 | * @param B2bCustomer $customer |
||
124 | */ |
||
125 | protected function onSuccess(B2bCustomerEmail $entity, B2bCustomer $customer) |
||
131 | } |
||
132 |
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.