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 |
||
20 | class DoctrineListener implements EventSubscriber { |
||
21 | |||
22 | /** |
||
23 | * |
||
24 | * @var EntityToDocumentMapperInterface |
||
25 | */ |
||
26 | protected $entityToDocumentMapper; |
||
27 | |||
28 | /** |
||
29 | * |
||
30 | * @var SearchService |
||
31 | */ |
||
32 | protected $searchService; |
||
33 | |||
34 | protected $needsFlush = false; |
||
35 | |||
36 | public function getSubscribedEvents() { |
||
44 | |||
45 | /** |
||
46 | * Constructor |
||
47 | * |
||
48 | * @param SearchService $searchService |
||
49 | */ |
||
50 | public function __construct(EntityToDocumentMapperInterface $entityToDocumentMapper, SearchService $searchService) { |
||
54 | |||
55 | /** |
||
56 | * Index the entity after it is persisted for the first time |
||
57 | * |
||
58 | * @param LifecycleEventArgs $args |
||
59 | */ |
||
60 | public function postPersist(LifecycleEventArgs $args) { |
||
63 | |||
64 | public function postFlush(PostFlushEventArgs $eventArgs) { |
||
70 | |||
71 | /** |
||
72 | * Removes the entity from the index when it marked for deletion |
||
73 | * |
||
74 | * @param LifecycleEventArgs $args |
||
75 | */ |
||
76 | public function preRemove(LifecycleEventArgs $args) { |
||
79 | |||
80 | /** |
||
81 | * Updates the entity in the index after it is updated |
||
82 | * |
||
83 | * @param LifecycleEventArgs $args |
||
84 | */ |
||
85 | public function postUpdate(LifecycleEventArgs $args) { |
||
88 | |||
89 | /** |
||
90 | * |
||
91 | * @return EntityToDocumentMapperInterface |
||
92 | */ |
||
93 | protected function getEntityToDocumentMapper() { |
||
96 | |||
97 | /** |
||
98 | * |
||
99 | * @return SearchService |
||
100 | */ |
||
101 | protected function getSearchService(ObjectManager $manager) { |
||
105 | |||
106 | /** |
||
107 | * |
||
108 | * @param object $entity |
||
109 | */ |
||
110 | View Code Duplication | protected function updateEntity($entity, ObjectManager $manager) { |
|
117 | |||
118 | /** |
||
119 | * |
||
120 | * @param object $entity |
||
121 | */ |
||
122 | View Code Duplication | protected function removeEntity($entity, ObjectManager $manager) { |
|
129 | } |
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.