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 |
||
| 36 | class AdminController extends Controller { |
||
| 37 | /** @var IJobList */ |
||
| 38 | private $jobList; |
||
| 39 | /** @var ISecureRandom */ |
||
| 40 | private $secureRandom; |
||
| 41 | /** @var IConfig */ |
||
| 42 | private $config; |
||
| 43 | /** @var ITimeFactory */ |
||
| 44 | private $timeFactory; |
||
| 45 | /** @var UpdateChecker */ |
||
| 46 | private $updateChecker; |
||
| 47 | /** @var IL10N */ |
||
| 48 | private $l10n; |
||
| 49 | /** @var IDateTimeFormatter */ |
||
| 50 | private $dateTimeFormatter; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param string $appName |
||
| 54 | * @param IRequest $request |
||
| 55 | * @param IJobList $jobList |
||
| 56 | * @param ISecureRandom $secureRandom |
||
| 57 | * @param IConfig $config |
||
| 58 | * @param ITimeFactory $timeFactory |
||
| 59 | * @param IL10N $l10n |
||
| 60 | * @param UpdateChecker $updateChecker |
||
| 61 | * @param IDateTimeFormatter $dateTimeFormatter |
||
| 62 | */ |
||
| 63 | View Code Duplication | public function __construct($appName, |
|
| 81 | |||
| 82 | /** |
||
| 83 | * @return TemplateResponse |
||
| 84 | */ |
||
| 85 | public function displayPanel() { |
||
| 86 | $lastUpdateCheck = $this->dateTimeFormatter->formatDateTime( |
||
| 87 | $this->config->getAppValue('core', 'lastupdatedat') |
||
| 88 | ); |
||
| 89 | |||
| 90 | $channels = [ |
||
| 91 | 'daily', |
||
| 92 | 'beta', |
||
| 93 | 'stable', |
||
| 94 | 'production', |
||
| 95 | ]; |
||
| 96 | $currentChannel = \OCP\Util::getChannel(); |
||
| 97 | |||
| 98 | // Remove the currently used channel from the channels list |
||
| 99 | if(($key = array_search($currentChannel, $channels)) !== false) { |
||
| 100 | unset($channels[$key]); |
||
| 101 | } |
||
| 102 | $updateState = $this->updateChecker->getUpdateState(); |
||
| 103 | $params = [ |
||
| 104 | 'isNewVersionAvailable' => ($updateState === []) ? false : true, |
||
| 105 | 'lastChecked' => $lastUpdateCheck, |
||
| 106 | 'currentChannel' => $currentChannel, |
||
| 107 | 'channels' => $channels, |
||
| 108 | 'newVersionString' => ($updateState === []) ? '' : $updateState['updateVersion'], |
||
| 109 | ]; |
||
| 110 | |||
| 111 | return new TemplateResponse($this->appName, 'admin', $params, ''); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @UseSession |
||
| 116 | * |
||
| 117 | * @param string $channel |
||
| 118 | * @return DataResponse |
||
| 119 | */ |
||
| 120 | public function setChannel($channel) { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @return DataResponse |
||
| 128 | */ |
||
| 129 | public function createCredentials() { |
||
| 140 | } |
||
| 141 |
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.