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 |
||
| 32 | class AppConfigController extends OCSController { |
||
| 33 | |||
| 34 | /** @var IConfig */ |
||
| 35 | protected $config; |
||
| 36 | |||
| 37 | /** @var IAppConfig */ |
||
| 38 | protected $appConfig; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param string $appName |
||
| 42 | * @param IRequest $request |
||
| 43 | * @param IConfig $config |
||
| 44 | * @param IAppConfig $appConfig |
||
| 45 | */ |
||
| 46 | public function __construct($appName, |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @return DataResponse |
||
| 57 | */ |
||
| 58 | public function getApps() { |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param string $app |
||
| 66 | * @return DataResponse |
||
| 67 | */ |
||
| 68 | public function getKeys($app) { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param string $app |
||
| 81 | * @param string $key |
||
| 82 | * @param string $defaultValue |
||
| 83 | * @return DataResponse |
||
| 84 | */ |
||
| 85 | public function getValue($app, $key, $defaultValue = '') { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @PasswordConfirmationRequired |
||
| 98 | * @param string $app |
||
| 99 | * @param string $key |
||
| 100 | * @param string $value |
||
| 101 | * @return DataResponse |
||
| 102 | */ |
||
| 103 | View Code Duplication | public function setValue($app, $key, $value) { |
|
| 114 | |||
| 115 | /** |
||
| 116 | * @PasswordConfirmationRequired |
||
| 117 | * @param string $app |
||
| 118 | * @param string $key |
||
| 119 | * @return DataResponse |
||
| 120 | */ |
||
| 121 | View Code Duplication | public function deleteKey($app, $key) { |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @param string $app |
||
| 135 | * @throws \InvalidArgumentException |
||
| 136 | */ |
||
| 137 | protected function verifyAppId($app) { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param string $app |
||
| 145 | * @param string $key |
||
| 146 | * @throws \InvalidArgumentException |
||
| 147 | */ |
||
| 148 | protected function verifyConfigKey($app, $key) { |
||
| 157 | } |
||
| 158 |
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.