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 | 5 | 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 | View Code Duplication | public function adminIndex() { |
66 | |||
67 | 1 | View Code Duplication | public function personalIndex() { |
79 | |||
80 | /** |
||
81 | * Admin: save default order |
||
82 | * |
||
83 | * @param $order |
||
84 | * @return array response |
||
85 | */ |
||
86 | 1 | public function saveDefaultOrder($order) { |
|
87 | 1 | if (!is_null($order)) { |
|
88 | 1 | $this->appConfig->setAppValue('order', $order); |
|
89 | 1 | } |
|
90 | 1 | return array('status' => 'success', 'order' => $order); |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * Return order for current user |
||
95 | * |
||
96 | * @NoAdminRequired |
||
97 | * @return array response |
||
98 | */ |
||
99 | 1 | public function getOrder() { |
|
103 | |||
104 | /** |
||
105 | * Save order for current user |
||
106 | * |
||
107 | * @NoAdminRequired |
||
108 | * @param $order string |
||
109 | * @return array response |
||
110 | */ |
||
111 | 1 | public function savePersonal($order) { |
|
120 | |||
121 | } |
||
122 |
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.