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 |
||
| 33 | class SettingsController extends Controller { |
||
| 34 | |||
| 35 | private $userId; |
||
| 36 | private $appConfig; |
||
| 37 | private $navigationManager; |
||
| 38 | private $util; |
||
| 39 | |||
| 40 | 7 | public function __construct($appName, IRequest $request, ConfigService $appConfig, INavigationManager $urlGenerator, Util $util, $userId) { |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Admin: render admin page |
||
| 50 | * FIXME: Move to dedicated class |
||
| 51 | * |
||
| 52 | * @return TemplateResponse |
||
| 53 | */ |
||
| 54 | 1 | public function adminIndex() { |
|
| 69 | |||
| 70 | 1 | public function personalIndex() { |
|
| 71 | // Private API call |
||
| 72 | 1 | $navigation = $this->navigationManager->getAll(); |
|
| 73 | 1 | $order = json_decode($this->appConfig->getUserValue('order', $this->userId)); |
|
| 74 | 1 | View Code Duplication | if($order === null){ |
| 75 | $order = json_decode($this->appConfig->getAppValue('order')); |
||
| 76 | if($order === null) $order = array(); |
||
| 77 | } |
||
| 78 | 1 | $nav = $this->util->matchOrder($navigation, $order); |
|
| 79 | 1 | $hidden = json_decode($this->appConfig->getUserValue('hidden',$this->userId)); |
|
| 80 | 1 | View Code Duplication | if($hidden === null){ |
| 81 | $hidden = json_decode($this->appConfig->getAppValue('hidden')); |
||
| 82 | if($hidden === null) $hidden = array(); |
||
| 83 | } |
||
| 84 | 1 | return new TemplateResponse( |
|
| 85 | 1 | $this->appName, |
|
| 86 | 1 | 'admin', |
|
| 87 | 1 | ["nav" => $nav, 'type' => 'personal', 'hidden' => $hidden], |
|
| 88 | 1 | 'blank' |
|
| 89 | ); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Admin: save default order |
||
| 94 | * |
||
| 95 | * @param $order |
||
| 96 | * @return array response |
||
| 97 | */ |
||
| 98 | 2 | public function saveDefaultOrder($order) { |
|
| 99 | 2 | if (!is_null($order)) { |
|
| 100 | 2 | $this->appConfig->setAppValue('order', $order); |
|
| 101 | } |
||
| 102 | 2 | return array('status' => 'success', 'order' => $order); |
|
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Return order for current user |
||
| 107 | * |
||
| 108 | * @NoAdminRequired |
||
| 109 | * @return array response |
||
| 110 | */ |
||
| 111 | 1 | public function getOrder() { |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Save order for current user |
||
| 119 | * |
||
| 120 | * @NoAdminRequired |
||
| 121 | * @param $order string |
||
| 122 | * @return array response |
||
| 123 | */ |
||
| 124 | 2 | View Code Duplication | public function savePersonal($order) { |
| 133 | |||
| 134 | /** |
||
| 135 | * Save hidden for current user |
||
| 136 | * |
||
| 137 | * @NoAdminRequired |
||
| 138 | * @param $hidden string |
||
| 139 | * @return array response |
||
| 140 | */ |
||
| 141 | View Code Duplication | public function savePersonalHidden($hidden) { |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Admin: save default hidden |
||
| 153 | * |
||
| 154 | * @param $hidden |
||
| 155 | * @return array response |
||
| 156 | */ |
||
| 157 | public function saveDefaultHidden($hidden) { |
||
| 163 | |||
| 164 | } |
||
| 165 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.