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 |
||
11 | use library\storage\Document; |
||
12 | use library\storage\Storage; |
||
13 | use library\storage\storage\DocumentTypesStorage; |
||
14 | |||
15 | class DocumentFactory |
||
16 | { |
||
17 | /** |
||
18 | * @param array $postValues |
||
19 | * @param DocumentTypesStorage $documentTypesStorage |
||
20 | * |
||
21 | * @return \library\storage\Document |
||
22 | */ |
||
23 | public static function createDocumentFromPostValues($postValues, DocumentTypesStorage $documentTypesStorage) |
||
40 | |||
41 | /** |
||
42 | * @param array $postValues |
||
43 | * @param \stdClass $documentType |
||
44 | * |
||
45 | * @return Document |
||
46 | */ |
||
47 | private static function createInitialDocumentObject($postValues, $documentType) |
||
62 | |||
63 | /** |
||
64 | * @param array $postValues |
||
65 | * @param Document $documentObj |
||
66 | * @param array $staticBricks |
||
67 | * |
||
68 | * @return Document |
||
69 | */ |
||
70 | private static function createBrickArrayForDocument($postValues, $documentObj, $staticBricks) |
||
110 | |||
111 | /** |
||
112 | * @param array $postValues |
||
113 | * @param Document $documentObj |
||
114 | * |
||
115 | * @return Document |
||
116 | */ |
||
117 | private static function createDynamicBrickArrayForDocument($postValues, $documentObj) |
||
118 | { |
||
119 | $documentObj->dynamicBricks = array(); |
||
120 | if (isset($postValues['dynamicBricks'])) { |
||
121 | View Code Duplication | foreach ($postValues['dynamicBricks'] as $brickTypeSlug => $brick) { |
|
122 | foreach ($brick as $brickContent) { |
||
123 | $brickObj = new \stdClass(); |
||
124 | $brickObj->type = $brickTypeSlug; |
||
125 | $brickObj->fields = $brickContent; |
||
126 | $dynamicBricks = $documentObj->dynamicBricks; |
||
127 | $dynamicBricks[] = $brickObj; |
||
128 | $documentObj->dynamicBricks = $dynamicBricks; |
||
129 | } |
||
130 | } |
||
131 | } |
||
134 | } |
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.