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 |
||
| 29 | class PageTreeRepository implements PageTreeUpdaterInterface, PageTreeMoverInterface |
||
| 30 | { |
||
| 31 | const ROUTE_PROPERTY = 'routePath'; |
||
| 32 | const TAG_NAME = 'sulu_article.article_route'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var DocumentManagerInterface |
||
| 36 | */ |
||
| 37 | private $documentManager; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var StructureMetadataFactoryInterface |
||
| 41 | */ |
||
| 42 | protected $metadataFactory; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var PropertyEncoder |
||
| 46 | */ |
||
| 47 | protected $propertyEncoder; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var DocumentInspector |
||
| 51 | */ |
||
| 52 | protected $documentInspector; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param DocumentManagerInterface $documentManager |
||
| 56 | * @param StructureMetadataFactoryInterface $metadataFactory |
||
| 57 | * @param PropertyEncoder $propertyEncoder |
||
| 58 | * @param DocumentInspector $documentInspector |
||
| 59 | */ |
||
| 60 | public function __construct( |
||
| 71 | |||
| 72 | /** |
||
| 73 | * {@inheritdoc} |
||
| 74 | */ |
||
| 75 | View Code Duplication | public function update(BasePageDocument $document) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | View Code Duplication | public function move($source, BasePageDocument $document) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Find articles linked to the given page. |
||
| 96 | * |
||
| 97 | * @param string $field |
||
| 98 | * @param string $value |
||
| 99 | * @param string $locale |
||
| 100 | * |
||
| 101 | * @return ArticleInterface[] |
||
| 102 | */ |
||
| 103 | private function findLinkedArticles($field, $value, $locale) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Update route of given article. |
||
| 139 | * |
||
| 140 | * @param ArticleDocument $article |
||
| 141 | * @param BasePageDocument $document |
||
| 142 | */ |
||
| 143 | private function updateArticle(ArticleDocument $article, BasePageDocument $document) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Returns "routePath" property. |
||
| 169 | * |
||
| 170 | * @param string $structureType |
||
| 171 | * |
||
| 172 | * @return PropertyMetadata |
||
| 173 | */ |
||
| 174 | View Code Duplication | private function getRoutePathPropertyNameByStructureType($structureType) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Returns "routePath" property. |
||
| 186 | * |
||
| 187 | * @param StructureMetadata $metadata |
||
| 188 | * |
||
| 189 | * @return PropertyMetadata |
||
| 190 | */ |
||
| 191 | View Code Duplication | private function getRoutePathPropertyName(StructureMetadata $metadata) |
|
| 203 | } |
||
| 204 |
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.