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 |
||
| 22 | class ContentProxyFactory |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var ContentTypeManagerInterface |
||
| 26 | */ |
||
| 27 | private $contentTypeManager; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var LazyLoadingValueHolderFactory |
||
| 31 | */ |
||
| 32 | private $proxyFactory; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param ContentTypeManagerInterface $contentTypeManager |
||
| 36 | * @param LazyLoadingValueHolderFactory $proxyFactory |
||
| 37 | */ |
||
| 38 | public function __construct( |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Create content-proxy for given structure. |
||
| 48 | * |
||
| 49 | * @param StructureInterface $structure |
||
| 50 | * @param array $data |
||
| 51 | * |
||
| 52 | * @return array |
||
| 53 | */ |
||
| 54 | View Code Duplication | public function createContentProxy(StructureInterface $structure, array $data) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Resolve content from given data with the structure. |
||
| 75 | * |
||
| 76 | * @param StructureInterface $structure |
||
| 77 | * @param array $data |
||
| 78 | * |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | View Code Duplication | private function resolveContent(StructureInterface $structure, array $data) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Create view-proxy for given structure. |
||
| 98 | * |
||
| 99 | * @param StructureInterface $structure |
||
| 100 | * @param array $data |
||
| 101 | * |
||
| 102 | * @return array |
||
| 103 | */ |
||
| 104 | View Code Duplication | public function createViewProxy(StructureInterface $structure, array $data) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Resolve view from given data with the structure. |
||
| 125 | * |
||
| 126 | * @param StructureInterface $structure |
||
| 127 | * @param array $data |
||
| 128 | * |
||
| 129 | * @return array |
||
| 130 | */ |
||
| 131 | View Code Duplication | private function resolveView(StructureInterface $structure, array $data) |
|
| 145 | } |
||
| 146 |
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.