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 |
||
7 | class VaultConfiguration implements ArraySerializableInterface |
||
8 | { |
||
9 | /** |
||
10 | * An arbitrary user-defined title that helps to identity a vault by some user-specific information. |
||
11 | * |
||
12 | * @var string |
||
13 | */ |
||
14 | protected $title; |
||
15 | |||
16 | /** |
||
17 | * Identifier for the vault adapter to use. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $adapter; |
||
22 | |||
23 | /** |
||
24 | * Identifier for the lock adapter to use. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $lockAdapter = 'storage'; |
||
29 | |||
30 | /** |
||
31 | * Identifier for the index merger to be used. |
||
32 | * Refers to identifiers known to the IndexMergerFactory. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $indexMerger = 'standard'; |
||
37 | |||
38 | /** |
||
39 | * Identifier for the conflict handler to use. |
||
40 | * Refers to identifiers known to the ConflictHandlerFactory. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $conflictHandler = 'panicking'; |
||
45 | |||
46 | /** |
||
47 | * Identifier for the operation list builder to use. |
||
48 | * Refers to identifiers known to the OperationListBuilderFactory. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $operationListBuilder = 'standard'; |
||
53 | |||
54 | /** |
||
55 | * Map with additional storageDriver- or lockAdapter-specific settings. |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $settings; |
||
60 | |||
61 | public function __construct(string $storageDriver = 'unknown') |
||
66 | |||
67 | public function getTitle(): string |
||
71 | |||
72 | public function setTitle(string $title): VaultConfiguration |
||
78 | |||
79 | public function getAdapter(): string |
||
83 | |||
84 | public function setAdapter(string $adapter): VaultConfiguration |
||
90 | |||
91 | public function getLockAdapter(): string |
||
95 | |||
96 | public function setLockAdapter(string $lockAdapter): VaultConfiguration |
||
102 | |||
103 | public function getIndexMerger(): string |
||
107 | |||
108 | public function setIndexMerger(string $indexMerger): VaultConfiguration |
||
114 | |||
115 | public function getConflictHandler(): string |
||
119 | |||
120 | public function setConflictHandler(string $conflictHandler): VaultConfiguration |
||
126 | |||
127 | public function getOperationListBuilder(): string |
||
131 | |||
132 | public function setOperationListBuilder(string $operationListBuilder): VaultConfiguration |
||
138 | |||
139 | public function getSettings(): array |
||
143 | |||
144 | public function getSetting(string $name) |
||
148 | |||
149 | public function setSettings(array $settings): VaultConfiguration |
||
155 | |||
156 | public function setSetting(string $name, $value): VaultConfiguration |
||
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | public function exchangeArray(array $array) |
||
179 | |||
180 | /** |
||
181 | * {@inheritdoc} |
||
182 | */ |
||
183 | public function getArrayCopy() |
||
187 | } |
||
188 |
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.