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 |
||
| 10 | class VaultConfiguration implements ArraySerializableInterface |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var Configuration |
||
| 14 | */ |
||
| 15 | protected $configuration; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * An arbitrary user-defined title that helps to identity a vault by some user-specific information. |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $title = 'unknown'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Identifier for the vault layout to use. |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $vaultLayout = 'amberjack'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Identifier for the vault adapter to use. |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $adapter = 'unknown'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Identifier for the lock adapter to use. |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $lockAdapter = 'storage'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Identifier for the index merger to be used. |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $indexMerger = 'standard'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Identifier for the conflict handler to use. |
||
| 54 | * |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | protected $conflictHandler = 'panicking'; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Identifier for the operation list builder to use. |
||
| 61 | * |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected $operationListBuilder = 'standard'; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The vault priority is used for cases where some data has to be read from a vault and a decision has to be made which vault to use. |
||
| 68 | * |
||
| 69 | * @var int |
||
| 70 | */ |
||
| 71 | protected $priority = 0; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Map with additional component-specific settings. |
||
| 75 | * |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $settings = []; |
||
| 79 | |||
| 80 | public function __construct(Configuration $configuration) |
||
| 85 | |||
| 86 | public function getConfiguration(): Configuration |
||
| 90 | |||
| 91 | public function getTitle(): string |
||
| 95 | |||
| 96 | public function setTitle(string $title): VaultConfiguration |
||
| 102 | |||
| 103 | public function getVaultLayout(): string |
||
| 107 | |||
| 108 | public function setVaultLayout(string $vaultLayout): VaultConfiguration |
||
| 114 | |||
| 115 | public function getAdapter(): string |
||
| 119 | |||
| 120 | public function setAdapter(string $adapter): VaultConfiguration |
||
| 126 | |||
| 127 | public function getLockAdapter(): string |
||
| 131 | |||
| 132 | public function setLockAdapter(string $lockAdapter): VaultConfiguration |
||
| 138 | |||
| 139 | public function getIndexMerger(): string |
||
| 143 | |||
| 144 | public function setIndexMerger(string $indexMerger): VaultConfiguration |
||
| 150 | |||
| 151 | public function getConflictHandler(): string |
||
| 155 | |||
| 156 | public function setConflictHandler(string $conflictHandler): VaultConfiguration |
||
| 162 | |||
| 163 | public function getOperationListBuilder(): string |
||
| 167 | |||
| 168 | public function setOperationListBuilder(string $operationListBuilder): VaultConfiguration |
||
| 174 | |||
| 175 | public function getPriority(): int |
||
| 179 | |||
| 180 | public function setPriority(int $priority): VaultConfiguration |
||
| 186 | |||
| 187 | public function getSettings(): array |
||
| 191 | |||
| 192 | public function hasSetting(string $name): bool |
||
| 196 | |||
| 197 | public function getSetting(string $name) |
||
| 201 | |||
| 202 | public function setSettings(array $settings): VaultConfiguration |
||
| 208 | |||
| 209 | public function setSetting(string $name, $value): VaultConfiguration |
||
| 215 | |||
| 216 | /** |
||
| 217 | * {@inheritdoc} |
||
| 218 | */ |
||
| 219 | public function exchangeArray(array $array) |
||
| 220 | { |
||
| 221 | View Code Duplication | if ($diff = array_diff(array_keys($array), array_keys($this->getArrayCopy()))) |
|
|
|
|||
| 222 | { |
||
| 223 | throw new \InvalidArgumentException("Invalid index(es): " . implode(',', $diff)); |
||
| 224 | } |
||
| 225 | |||
| 226 | foreach ($array as $key => $value) |
||
| 227 | { |
||
| 228 | // using setter to prevent skipping validation |
||
| 229 | call_user_func([$this, 'set' . ucfirst($key)], $value); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * {@inheritdoc} |
||
| 235 | */ |
||
| 236 | public function getArrayCopy() |
||
| 244 | |||
| 245 | public static function loadValidatorMetadata(ClassMetadata $metadata) |
||
| 254 | } |
||
| 255 |
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.