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 ArticleSubscriber implements EventSubscriberInterface |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var IndexerInterface |
||
| 31 | */ |
||
| 32 | private $indexer; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var IndexerInterface |
||
| 36 | */ |
||
| 37 | private $liveIndexer; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var DocumentManagerInterface |
||
| 41 | */ |
||
| 42 | private $documentManager; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | private $documents = []; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | private $liveDocuments = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param IndexerInterface $indexer |
||
| 56 | * @param IndexerInterface $liveIndexer |
||
| 57 | * @param DocumentManagerInterface $documentManager |
||
| 58 | */ |
||
| 59 | public function __construct( |
||
| 68 | |||
| 69 | /** |
||
| 70 | * {@inheritdoc} |
||
| 71 | */ |
||
| 72 | public static function getSubscribedEvents() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Schedule article document for index. |
||
| 86 | * |
||
| 87 | * @param AbstractMappingEvent $event |
||
| 88 | */ |
||
| 89 | View Code Duplication | public function handleScheduleIndex(AbstractMappingEvent $event) |
|
| 101 | 31 | ||
| 102 | /** |
||
| 103 | * Schedule article document for live index. |
||
| 104 | 31 | * |
|
| 105 | 31 | * @param AbstractMappingEvent $event |
|
| 106 | 31 | */ |
|
| 107 | 31 | View Code Duplication | public function handleScheduleIndexLive(AbstractMappingEvent $event) |
| 119 | |||
| 120 | /** |
||
| 121 | 31 | * Index all scheduled article documents with default indexer. |
|
| 122 | * |
||
| 123 | 31 | * @param FlushEvent $event |
|
| 124 | 31 | */ |
|
| 125 | 29 | View Code Duplication | public function handleFlush(FlushEvent $event) |
| 142 | |||
| 143 | 32 | /** |
|
| 144 | 32 | * Index all scheduled article documents with live indexer. |
|
| 145 | 9 | * |
|
| 146 | * @param FlushEvent $event |
||
| 147 | */ |
||
| 148 | 32 | View Code Duplication | public function handleFlushLive(FlushEvent $event) |
| 165 | 31 | ||
| 166 | /** |
||
| 167 | 30 | * Indexes for article-document in live index. |
|
| 168 | * |
||
| 169 | * @param AbstractMappingEvent $event |
||
| 170 | 1 | */ |
|
| 171 | 1 | public function handleIndexLive(AbstractMappingEvent $event) |
|
| 181 | 30 | ||
| 182 | /** |
||
| 183 | 30 | * Removes document from live index and unpublish document in default index. |
|
| 184 | 30 | * |
|
| 185 | 1 | * @param UnpublishEvent $event |
|
| 186 | */ |
||
| 187 | public function handleUnpublish(UnpublishEvent $event) |
||
| 199 | 11 | ||
| 200 | /** |
||
| 201 | 11 | * Removes article-document. |
|
| 202 | 11 | * |
|
| 203 | 1 | * @param RemoveEvent $event |
|
| 204 | */ |
||
| 205 | public function handleRemove(RemoveEvent $event) |
||
| 215 | |||
| 216 | /** |
||
| 217 | 30 | * Removes article-document. |
|
| 218 | * |
||
| 219 | 30 | * @param RemoveEvent|UnpublishEvent $event |
|
| 220 | 3 | */ |
|
| 221 | public function handleRemoveLive($event) |
||
| 231 | } |
||
| 232 |
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.