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 |
||
23 | trait ContextTrait |
||
24 | { |
||
25 | /** |
||
26 | * Import the context defined in metadata and set some default values. |
||
27 | * |
||
28 | * @param string $resourceClass |
||
29 | * @param array $context |
||
30 | * |
||
31 | * @return array |
||
32 | */ |
||
33 | private function createContext(string $resourceClass, array $context, string $format) : array |
||
45 | |||
46 | /** |
||
47 | * Import the context defined in metadata and set some default values. |
||
48 | * |
||
49 | * @param string $resourceClass |
||
50 | * @param array $context |
||
51 | * |
||
52 | * @return array |
||
53 | */ |
||
54 | View Code Duplication | private function createHalContext(string $resourceClass, array $context) : array |
|
66 | |||
67 | /** |
||
68 | * Import the context defined in metadata and set some default values. |
||
69 | * |
||
70 | * @param string $resourceClass |
||
71 | * @param array $context |
||
72 | * |
||
73 | * @return array |
||
74 | */ |
||
75 | View Code Duplication | private function createJsonLdContext(string $resourceClass, array $context) : array |
|
91 | |||
92 | /** |
||
93 | * Updates the given document to add its keys. |
||
94 | * |
||
95 | * @param ContextBuilderInterface $contextBuilder |
||
96 | * @param string $resourceClass |
||
97 | * @param array $context |
||
98 | * @param array $data |
||
99 | * @param string $format |
||
100 | * |
||
101 | * @return array |
||
102 | */ |
||
103 | View Code Duplication | private function addContext(ContextBuilderInterface $contextBuilder, string $resourceClass, array $context, array $data, string $format) : array |
|
124 | } |
||
125 |
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.