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 |
||
| 23 | class EventRegisterPass implements CompilerPassInterface |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * The event subscriber tag name. |
||
| 27 | */ |
||
| 28 | const SUBSCRIBER_TAG = 'ldap_tools.event_subscriber'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The event listener tag name. |
||
| 32 | */ |
||
| 33 | const LISTENER_TAG = 'ldap_tools.event_listener'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The event dispatcher service name. |
||
| 37 | */ |
||
| 38 | const DISPATCHER = 'ldap_tools.event_dispatcher'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @inheritdoc |
||
| 42 | */ |
||
| 43 | public function process(ContainerBuilder $container) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param ContainerBuilder $container |
||
| 63 | * @param array $events |
||
| 64 | * @param Definition $dispatcher |
||
| 65 | */ |
||
| 66 | protected function addSubscribersToEventDispatcher(ContainerBuilder $container, array $events, Definition $dispatcher) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param ContainerBuilder $container |
||
| 76 | * @param array $events |
||
| 77 | * @param Definition $dispatcher |
||
| 78 | */ |
||
| 79 | protected function addListenersToEventDispatcher(ContainerBuilder $container, array $events, Definition $dispatcher) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param ContainerBuilder $container |
||
| 95 | * @param string $id |
||
| 96 | */ |
||
| 97 | protected function validateTaggedService(ContainerBuilder $container, $id) |
||
| 106 | } |
||
| 107 |
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.