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 PageSubscriber implements EventSubscriberInterface |
||
| 28 | { |
||
| 29 | const FIELD = 'pageNumber'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var DocumentInspector |
||
| 33 | */ |
||
| 34 | private $documentInspector; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var PropertyEncoder |
||
| 38 | */ |
||
| 39 | private $propertyEncoder; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param DocumentInspector $documentInspector |
||
| 43 | * @param PropertyEncoder $propertyEncoder |
||
| 44 | */ |
||
| 45 | public function __construct(DocumentInspector $documentInspector, PropertyEncoder $propertyEncoder) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * {@inheritdoc} |
||
| 53 | */ |
||
| 54 | View Code Duplication | public static function getSubscribedEvents() |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Set the page-number to existing pages. |
||
| 66 | * |
||
| 67 | * @param HydrateEvent $event |
||
| 68 | */ |
||
| 69 | public function handleHydrate(HydrateEvent $event) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Set the page-number to new pages. |
||
| 84 | * |
||
| 85 | * @param PersistEvent $event |
||
| 86 | */ |
||
| 87 | public function handlePersist(PersistEvent $event) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Copy page-number to live workspace. |
||
| 117 | * |
||
| 118 | * @param PublishEvent $event |
||
| 119 | */ |
||
| 120 | public function handlePublishPageNumber(PublishEvent $event) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Adjust the page-numbers of siblings when removing a page. |
||
| 134 | * |
||
| 135 | * @param RemoveEvent $event |
||
| 136 | */ |
||
| 137 | public function handleRemove(RemoveEvent $event) |
||
| 154 | } |
||
| 155 |
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.