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 |
||
45 | class CoreContentProvider extends ContentProvider implements ProviderInterface { |
||
46 | |||
47 | const MODE_RECORD = 'record'; |
||
48 | const MODE_PRESELECT = 'preselect'; |
||
49 | const CTYPE_MENU = 'menu'; |
||
50 | const CTYPE_TABLE = 'table'; |
||
51 | const CTYPE_FIELDNAME = 'CType'; |
||
52 | const MENUTYPE_FIELDNAME = 'menu_type'; |
||
53 | const MENU_SELECTEDPAGES = 0; |
||
54 | const MENU_SUBPAGESOFSELECTEDPAGES = 1; |
||
55 | const MENU_SUBPAGESOFSELECTEDPAGESWITHABSTRACT = 4; |
||
56 | const MENU_SUBPAGESOFSELECTEDPAGESWITHSECTIONS = 7; |
||
57 | const MENU_SITEMAP = 2; |
||
58 | const MENU_SITEMAPSOFSELECTEDPAGES = 8; |
||
59 | const MENU_SECTIONINDEX = 3; |
||
60 | const MENU_RECENTLYUPDATED = 5; |
||
61 | const MENU_RELATEDPAGES = 6; |
||
62 | const MENU_CATEGORIZEDPAGES = 'categorized_pages'; |
||
63 | const MENU_CATEGORIZEDCONTENT = 'categorized_content'; |
||
64 | const THEAD_NONE = 'none'; |
||
65 | const THEAD_TOP = 'top'; |
||
66 | const THEAD_LEFT = 'left'; |
||
67 | |||
68 | /** |
||
69 | * @var string |
||
70 | */ |
||
71 | protected $extensionKey = 'DCNGmbH.MooxCore'; |
||
72 | |||
73 | /** |
||
74 | * @var integer |
||
75 | */ |
||
76 | protected $priority = 0; |
||
77 | |||
78 | /** |
||
79 | * @var string |
||
80 | */ |
||
81 | protected $tableName = 'tt_content'; |
||
82 | |||
83 | /** |
||
84 | * @var string |
||
85 | */ |
||
86 | protected $fieldName = 'content_options'; |
||
87 | |||
88 | /** |
||
89 | * Filled with an integer-or-string -> Fluid section name |
||
90 | * map which maps machine names of menu types to human |
||
91 | * readable values that are sensible as Fluid section names. |
||
92 | * When type is selected in menu element, corresponding |
||
93 | * section gets rendered. |
||
94 | * |
||
95 | * @var array |
||
96 | */ |
||
97 | protected $menuTypeToSectionNameMap = array( |
||
98 | self::MENU_SELECTEDPAGES => 'SelectedPages', |
||
99 | self::MENU_SUBPAGESOFSELECTEDPAGES => 'SubPagesOfSelectedPages', |
||
100 | self::MENU_SUBPAGESOFSELECTEDPAGESWITHABSTRACT => 'SubPagesOfSelectedPagesWithAbstract', |
||
101 | self::MENU_SUBPAGESOFSELECTEDPAGESWITHSECTIONS => 'SubPagesOfSelectedPagesWithSections', |
||
102 | self::MENU_SITEMAP => 'SiteMap', |
||
103 | self::MENU_SITEMAPSOFSELECTEDPAGES => 'SiteMapsOfSelectedPages', |
||
104 | self::MENU_SECTIONINDEX => 'SectionIndex', |
||
105 | self::MENU_RECENTLYUPDATED => 'RecentlyUpdated', |
||
106 | self::MENU_RELATEDPAGES => 'RelatedPages', |
||
107 | self::MENU_CATEGORIZEDPAGES => 'CategorizedPages', |
||
108 | self::MENU_CATEGORIZEDCONTENT => 'CategorizedContent' |
||
109 | ); |
||
110 | |||
111 | /** |
||
112 | * @var ConfigurationService |
||
113 | */ |
||
114 | protected $contentConfigurationService; |
||
115 | |||
116 | /** |
||
117 | * @param ConfigurationService $contentConfigurationService |
||
118 | * @return void |
||
119 | */ |
||
120 | public function injectContentConfigurationService(ConfigurationService $contentConfigurationService) { |
||
121 | $this->contentConfigurationService = $contentConfigurationService; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @return void |
||
126 | */ |
||
127 | public function initializeObject() { |
||
128 | $typoScript = $this->contentConfigurationService->getAllTypoScript(); |
||
129 | $settings = $typoScript['plugin']['tx_mooxcore']['settings']; |
||
130 | $this->templateVariables['settings'] = $settings; |
||
131 | $this->templatePathAndFilename = PathUtility::translatePath($settings['defaults']['template']); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Note: This Provider will -always- trigger on any tt_content record |
||
136 | * but has the lowest possible (0) priority, ensuring that any |
||
137 | * Provider which wants to take over, can do so. |
||
138 | * |
||
139 | * @param array $row |
||
140 | * @param string $table |
||
141 | * @param string $field |
||
142 | * @param string $extensionKey |
||
143 | * @return boolean |
||
144 | */ |
||
145 | public function trigger(array $row, $table, $field, $extensionKey = NULL) { |
||
148 | |||
149 | /** |
||
150 | * @param array $row |
||
151 | * @return Form |
||
152 | */ |
||
153 | public function getForm(array $row) { |
||
154 | if (self::CTYPE_MENU === $row[self::CTYPE_FIELDNAME]) { |
||
155 | // addtional menu variables |
||
156 | $menuType = $row[self::MENUTYPE_FIELDNAME]; |
||
157 | $partialTemplateName = $this->menuTypeToSectionNameMap[$menuType]; |
||
158 | $this->templateVariables['menuPartialTemplateName'] = $partialTemplateName; |
||
159 | $this->templateVariables['pageUids'] = GeneralUtility::trimExplode(',', $row['pages']); |
||
160 | } |
||
161 | if (self::CTYPE_TABLE == $row[self::CTYPE_FIELDNAME]) { |
||
162 | $this->templateVariables['tableHeadPositions'] = array( |
||
163 | self::THEAD_NONE => $this->translateLabel('tableHead.none', 'moox_core'), |
||
|
|||
164 | self::THEAD_TOP => $this->translateLabel('tableHead.top', 'moox_core'), |
||
165 | self::THEAD_LEFT => $this->translateLabel('tableHead.left', 'moox_core'), |
||
166 | ); |
||
167 | } |
||
168 | return parent::getForm($row); |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @param array $row |
||
173 | * @return string|NULL |
||
174 | */ |
||
175 | public function getExtensionKey(array $row) { |
||
176 | $extensionKey = $this->extensionKey; |
||
177 | if (FALSE === empty($row['content_variant'])) { |
||
178 | $extensionKey = $row['content_variant']; |
||
179 | } |
||
180 | return ExtensionNamingUtility::getExtensionKey($extensionKey); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @param array $row |
||
185 | * @return string |
||
186 | */ |
||
187 | public function getControllerExtensionKeyFromRecord(array $row) { |
||
190 | |||
191 | /** |
||
192 | * @param array $row |
||
193 | * @return string |
||
194 | */ |
||
195 | public function getTemplatePathAndFilename(array $row) { |
||
196 | $extensionKey = $this->getExtensionKey($row); |
||
197 | $variant = $this->getVariant($row); |
||
198 | $version = $this->getVersion($row); |
||
199 | $registeredTypes = (array) $GLOBALS['TYPO3_CONF_VARS']['DCNGmbH.MooxCore']['types']; |
||
200 | $templateName = TRUE === in_array($row['CType'], $registeredTypes) ? $row['CType'] : 'default'; |
||
201 | $template = $this->contentConfigurationService->resolveTemplateFileForVariant($extensionKey, $templateName, $variant, $version); |
||
202 | return $template; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @param array $row |
||
207 | * @return string |
||
208 | */ |
||
209 | View Code Duplication | protected function getVariant(array $row) { |
|
210 | $defaults = $this->contentConfigurationService->getDefaults(); |
||
211 | if (self::MODE_RECORD !== $defaults['mode'] && TRUE === empty($row['content_variant'])) { |
||
212 | return $defaults['variant']; |
||
213 | } |
||
214 | return $row['content_variant']; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * @param array $row |
||
219 | * @return string |
||
220 | */ |
||
221 | View Code Duplication | protected function getVersion(array $row) { |
|
222 | $defaults = $this->contentConfigurationService->getDefaults(); |
||
223 | if (self::MODE_RECORD !== $defaults['mode'] && TRUE === empty($row['content_version'])) { |
||
224 | return $defaults['version']; |
||
225 | } |
||
226 | return $row['content_version']; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * @param array $row |
||
231 | * @return string |
||
232 | */ |
||
233 | public function getControllerActionFromRecord(array $row) { |
||
236 | |||
237 | /** |
||
238 | * @param string $operation |
||
239 | * @param integer $id |
||
240 | * @param array $row |
||
241 | * @param DataHandler $reference |
||
242 | * @param array $removals Allows overridden methods to pass an additional array of field names to remove from the stored Flux value |
||
243 | * @return void |
||
244 | */ |
||
245 | public function postProcessRecord($operation, $id, array &$row, DataHandler $reference, array $removals = array()) { |
||
257 | |||
258 | /** |
||
259 | * @param array $row |
||
260 | * @return array |
||
261 | */ |
||
262 | public function getTemplatePaths(array $row) { |
||
276 | |||
277 | /** |
||
278 | * @param string $key |
||
279 | * @return string|NULL |
||
280 | */ |
||
281 | protected function translateLabel($key) { |
||
284 | |||
285 | } |
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.