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 |
||
27 | class JobEventSubscriber implements EventSubscriber |
||
28 | { |
||
29 | /** |
||
30 | * @var Manager |
||
31 | */ |
||
32 | protected $solrManager; |
||
33 | |||
34 | /** |
||
35 | * JobEventSubscriber constructor. |
||
36 | * @param Manager $manager |
||
37 | */ |
||
38 | public function __construct(Manager $manager) |
||
42 | |||
43 | /** |
||
44 | * Define what event this subscriber listen to |
||
45 | * |
||
46 | * @return array |
||
47 | */ |
||
48 | public function getSubscribedEvents() |
||
55 | |||
56 | /** |
||
57 | * Handle doctrine post persist event |
||
58 | * |
||
59 | * @param LifecycleEventArgs $eventArgs |
||
60 | */ |
||
61 | View Code Duplication | public function postPersist(LifecycleEventArgs $eventArgs) |
|
76 | |||
77 | /** |
||
78 | * Handle doctrine postUpdate event |
||
79 | * |
||
80 | * @param LifecycleEventArgs $eventArgs |
||
81 | */ |
||
82 | View Code Duplication | public function postUpdate(LifecycleEventArgs $eventArgs) |
|
96 | |||
97 | /** |
||
98 | * @param ServiceLocatorInterface $serviceLocator |
||
99 | * @return mixed |
||
100 | */ |
||
101 | static public function factory(ServiceLocatorInterface $serviceLocator) |
||
106 | |||
107 | /** |
||
108 | * Generate input document |
||
109 | * |
||
110 | * @param Job $job |
||
111 | * @param \SolrInputDocument $document |
||
112 | * @return \SolrInputDocument |
||
113 | */ |
||
114 | public function generateInputDocument(Job $job, $document) |
||
151 | |||
152 | /** |
||
153 | * Processing organization part |
||
154 | * |
||
155 | * @param Job $job |
||
156 | * @param \SolrInputDocument $document |
||
157 | */ |
||
158 | public function processOrganization(Job $job,$document) |
||
167 | |||
168 | /** |
||
169 | * Processing location part |
||
170 | * @param Job $job |
||
171 | * @param \SolrInputDocument $document |
||
172 | */ |
||
173 | public function processLocation(Job $job,$document) |
||
185 | } |
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.