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 | const PAGE_TITLE_TAG_NAME = 'sulu_article.page_title'; |
||
| 30 | const PAGE_TITLE_PROPERTY_NAME = 'pageTitle'; |
||
| 31 | |||
| 32 | use ArticleTypeTrait; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var StructureManagerInterface |
||
| 36 | 49 | */ |
|
| 37 | private $structureManager; |
||
| 38 | 49 | ||
| 39 | 49 | /** |
|
| 40 | * @var StructureMetadataFactoryInterface |
||
| 41 | */ |
||
| 42 | private $structureMetadataFactory; |
||
| 43 | |||
| 44 | 1 | /** |
|
| 45 | * @param StructureManagerInterface $structureManager |
||
| 46 | * @param StructureMetadataFactoryInterface $structureMetadataFactory |
||
| 47 | */ |
||
| 48 | 1 | public function __construct( |
|
| 55 | |||
| 56 | /** |
||
| 57 | * {@inheritdoc} |
||
| 58 | */ |
||
| 59 | public static function getSubscribedEvents() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Append type to result. |
||
| 77 | * |
||
| 78 | * @param ObjectEvent $event |
||
| 79 | */ |
||
| 80 | View Code Duplication | public function addTypeOnPostSerialize(ObjectEvent $event) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Append page-title-property to result. |
||
| 96 | * |
||
| 97 | * @param ObjectEvent $event |
||
| 98 | */ |
||
| 99 | View Code Duplication | public function addPageTitlePropertyNameOnPostSerialize(ObjectEvent $event) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Find page-title property. |
||
| 117 | * |
||
| 118 | * @param ArticleInterface $document |
||
| 119 | * |
||
| 120 | * @return PropertyMetadata |
||
| 121 | */ |
||
| 122 | View Code Duplication | private function getPageTitleProperty(ArticleInterface $document) |
|
| 139 | } |
||
| 140 |
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.