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 |
||
19 | class ImportPartExecutor extends AbstractExecutor implements ObjectPayloadInterface |
||
20 | { |
||
21 | const NAME = 'import.part'; |
||
22 | |||
23 | /** |
||
24 | * @var ManagerRegistry |
||
25 | */ |
||
26 | protected $doctrine; |
||
27 | |||
28 | /** |
||
29 | * @var ImportFactory |
||
30 | */ |
||
31 | protected $importFactory; |
||
32 | |||
33 | /** |
||
34 | * @var LoggerInterface |
||
35 | */ |
||
36 | protected $logger; |
||
37 | |||
38 | /** |
||
39 | * @param ManagerRegistry $doctrine |
||
40 | * @param ImportFactory $importFactory |
||
41 | * @param LoggerInterface $logger |
||
42 | */ |
||
43 | public function __construct(ManagerRegistry $doctrine, ImportFactory $importFactory, LoggerInterface $logger) |
||
49 | |||
50 | /** |
||
51 | * @return string |
||
52 | */ |
||
53 | public function getName() |
||
57 | |||
58 | /** |
||
59 | * @inheritdoc |
||
60 | */ |
||
61 | public function getObjectPayload($object) |
||
65 | |||
66 | /** |
||
67 | * @inheritdoc |
||
68 | */ |
||
69 | public function supportsObject($object) |
||
73 | |||
74 | /** |
||
75 | * @inheritdoc |
||
76 | */ |
||
77 | View Code Duplication | public function configurePayload(OptionsResolver $resolver) |
|
89 | |||
90 | /** |
||
91 | * @inheritdoc |
||
92 | */ |
||
93 | public function execute(array $payload) |
||
133 | |||
134 | /** |
||
135 | * @param int $partId |
||
136 | * |
||
137 | * @return ImportPart |
||
138 | */ |
||
139 | protected function findImportPart($partId) |
||
143 | |||
144 | /** |
||
145 | * @param ImportPart $part |
||
146 | * |
||
147 | * @return bool |
||
148 | */ |
||
149 | protected function validate(ImportPart $part) |
||
162 | } |
||
163 |
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.