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 |
||
35 | class CoreContentController extends AbstractFluxController { |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $fluxRecordField = 'content_options'; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $fluxTableName = 'tt_content'; |
||
46 | |||
47 | /** |
||
48 | * @return void |
||
49 | */ |
||
50 | protected function initializeProvider() { |
||
53 | |||
54 | /** |
||
55 | * @return void |
||
56 | */ |
||
57 | protected function initializeViewVariables() { |
||
58 | $row = $this->getRecord(); |
||
59 | $flexFormData = $this->configurationService->convertFlexFormContentToArray($row['pi_flexform']); |
||
60 | $this->settings = RecursiveArrayUtility::merge($this->settings, $flexFormData, FALSE, FALSE); |
||
|
|||
61 | $form = $this->provider->getForm($row); |
||
62 | $generalSettings = $this->configurationService->convertFlexFormContentToArray($row['pi_flexform'], $form); |
||
63 | $contentSettings = $this->configurationService->convertFlexFormContentToArray($row['content_options'], $form); |
||
64 | $this->settings = RecursiveArrayUtility::merge($this->settings, $generalSettings, FALSE, FALSE); |
||
65 | if (FALSE === isset($this->settings['content'])) { |
||
66 | $this->settings['content'] = $contentSettings; |
||
67 | } else { |
||
68 | $this->settings['content'] = RecursiveArrayUtility::merge($this->settings['content'], $contentSettings); |
||
69 | } |
||
70 | parent::initializeViewVariables(); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @return void |
||
75 | */ |
||
76 | public function defaultAction() { |
||
79 | |||
80 | /** |
||
81 | * @return void |
||
82 | */ |
||
83 | public function headerAction() { |
||
86 | |||
87 | /** |
||
88 | * @return void |
||
89 | */ |
||
90 | public function textAction() { |
||
93 | |||
94 | /** |
||
95 | * @return void |
||
96 | */ |
||
97 | public function textpicAction() { |
||
100 | |||
101 | /** |
||
102 | * @return void |
||
103 | */ |
||
104 | public function imageAction() { |
||
107 | |||
108 | /** |
||
109 | * @return void |
||
110 | */ |
||
111 | public function bulletsAction() { |
||
114 | |||
115 | /** |
||
116 | * @return void |
||
117 | */ |
||
118 | public function uploadsAction() { |
||
121 | |||
122 | /** |
||
123 | * @return void |
||
124 | */ |
||
125 | public function tableAction() { |
||
128 | |||
129 | /** |
||
130 | * @return void |
||
131 | */ |
||
132 | public function mediaAction() { |
||
135 | |||
136 | /** |
||
137 | * @return void |
||
138 | */ |
||
139 | public function menuAction() { |
||
140 | $record = $this->getRecord(); |
||
141 | $type = $record[CoreContentProvider::MENUTYPE_FIELDNAME]; |
||
142 | switch ($type) { |
||
143 | View Code Duplication | case CoreContentProvider::MENU_CATEGORIZEDPAGES: |
|
144 | $selected = $record['selected_categories']; |
||
145 | $bindings = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( |
||
146 | 'uid_foreign', |
||
147 | 'sys_category_record_mm', |
||
148 | "fieldname = 'categories' AND tablenames = 'pages' AND uid_local IN (" . $selected . ')', |
||
149 | 'uid_foreign', |
||
150 | 'sorting ASC' |
||
151 | ); |
||
152 | $pageUids = array_map('array_pop', $bindings); |
||
153 | $this->view->assign('pageUids', $pageUids); |
||
154 | break; |
||
155 | View Code Duplication | case CoreContentProvider::MENU_CATEGORIZEDCONTENT: |
|
156 | $selected = $record['selected_categories']; |
||
157 | $bindings = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( |
||
158 | 'uid_foreign', |
||
159 | 'sys_category_record_mm', |
||
160 | "fieldname = 'categories' AND tablenames = 'tt_content' AND uid_local IN (" . $selected . ')', |
||
161 | 'uid_foreign', |
||
162 | 'sorting ASC' |
||
163 | ); |
||
164 | $contentUids = array_map('array_pop', $bindings); |
||
165 | $this->view->assign('contentUids', $contentUids); |
||
166 | break; |
||
167 | case CoreContentProvider::MENU_RELATEDPAGES: |
||
168 | $whereKeywords = $this->getWhereQueryForKeywords($record); |
||
169 | $selectedUids = TRUE === empty($record['pages']) ? $record['uid'] : $record['pages']; |
||
170 | $where = $whereKeywords . ' AND uid NOT IN (' . $selectedUids . ')'; |
||
171 | $bindings = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( |
||
172 | 'uid', |
||
173 | 'pages', |
||
174 | $where, |
||
175 | '', |
||
176 | 'sorting ASC' |
||
177 | ); |
||
178 | $pageUids = array_map('array_pop', $bindings); |
||
179 | $this->view->assign('pageUids', $pageUids); |
||
180 | break; |
||
181 | default: |
||
182 | } |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @return void |
||
187 | */ |
||
188 | public function shortcutAction() { |
||
189 | $record = $this->getRecord(); |
||
190 | $contentUids = array_map(function($index) { |
||
191 | if (0 !== strpos($index, 'tt_content_') && FALSE === MathUtility::canBeInterpretedAsInteger($index)) { |
||
192 | return FALSE; |
||
193 | } |
||
194 | return str_replace('tt_content_', '', $index); |
||
195 | }, GeneralUtility::trimExplode(',', $record['records'])); |
||
196 | |||
197 | $this->view->assign('contentUids', implode(',', array_filter($contentUids))); |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * @return void |
||
202 | */ |
||
203 | public function divAction() { |
||
206 | |||
207 | /** |
||
208 | * @return void |
||
209 | */ |
||
210 | public function htmlAction() { |
||
213 | |||
214 | /** |
||
215 | * @param $record |
||
216 | * @return string |
||
217 | */ |
||
218 | protected function getWhereQueryForKeywords($record) { |
||
243 | |||
244 | } |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.