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 |
||
152 | class Locator_Controller extends Page_Controller |
||
153 | { |
||
154 | /** |
||
155 | * @var array |
||
156 | */ |
||
157 | private static $allowed_actions = array( |
||
158 | 'xml', |
||
159 | ); |
||
160 | |||
161 | /** |
||
162 | * @var array |
||
163 | */ |
||
164 | private static $base_filter = [ |
||
165 | 'ShowInLocator' => true, |
||
166 | ]; |
||
167 | |||
168 | /** |
||
169 | * @var array |
||
170 | */ |
||
171 | private static $base_exclude = [ |
||
172 | 'Lat' => 0, |
||
173 | 'Lng' => 0, |
||
174 | ]; |
||
175 | |||
176 | /** |
||
177 | * @var array |
||
178 | */ |
||
179 | private static $base_filter_any = []; |
||
180 | |||
181 | /** |
||
182 | * @var string |
||
183 | */ |
||
184 | private static $list_template_path = 'locator/templates/location-list-description.html'; |
||
185 | |||
186 | /** |
||
187 | * @var string |
||
188 | */ |
||
189 | private static $info_window_template_path = 'locator/templates/infowindow-description.html'; |
||
190 | |||
191 | /** |
||
192 | * @var DataList|ArrayList |
||
193 | */ |
||
194 | private $locations; |
||
195 | |||
196 | /** |
||
197 | * Set Requirements based on input from CMS |
||
198 | */ |
||
199 | public function init() |
||
200 | { |
||
201 | parent::init(); |
||
202 | $themeDir = SSViewer::get_theme_folder(); |
||
203 | // google maps api key |
||
204 | $key = Config::inst()->get('GoogleGeocoding', 'google_api_key'); |
||
205 | $locations = $this->getLocations(); |
||
206 | |||
207 | if ($locations) { |
||
208 | Requirements::javascript('framework/thirdparty/jquery/jquery.js'); |
||
209 | Requirements::javascript('locator/thirdparty/jQuery-Store-Locator-Plugin-2.6.2/libs/handlebars/handlebars-v4.0.5.js'); |
||
210 | Requirements::javascript('https://maps.googleapis.com/maps/api/js?key=' . $key); |
||
211 | Requirements::javascript('locator/thirdparty/jQuery-Store-Locator-Plugin-2.6.2/src/js/jquery.storelocator.js'); |
||
212 | } |
||
213 | Requirements::css('locator/css/map.css'); |
||
214 | |||
215 | $featuredInList = ($locations->filter('Featured', true)->count() > 0); |
||
216 | $featured = $featuredInList |
||
217 | ? 'featuredLocations: true' |
||
218 | : 'featuredLocations: false'; |
||
219 | // map config based on user input in Settings tab |
||
220 | // AutoGeocode or Full Map |
||
221 | if ($this->data()->AutoGeocode) { |
||
222 | $load = $featuredInList |
||
223 | ? 'autoGeocode: false, fullMapStart: true, storeLimit: 1000, maxDistance: true,' |
||
224 | : 'autoGeocode: true, fullMapStart: false,'; |
||
225 | } else { |
||
226 | $load = 'autoGeocode: false, fullMapStart: true, storeLimit: 1000, maxDistance: true,'; |
||
227 | } |
||
228 | /*$load = ($this->data()->AutoGeocode) ? |
||
229 | 'autoGeocode: true, fullMapStart: false,' : |
||
230 | 'autoGeocode: false, fullMapStart: true, storeLimit: 1000, maxDistance: true,';*/ |
||
231 | $base = Director::baseFolder(); |
||
232 | $themePath = $base . '/' . $themeDir; |
||
233 | $listTemplatePath = (file_exists($themePath . '/templates/location-list-description.html')) ? |
||
234 | $themeDir . '/templates/location-list-description.html' : |
||
235 | 'locator/templates/location-list-description.html'; |
||
236 | $infowindowTemplatePath = (file_exists($themePath . '/templates/infowindow-description.html')) ? |
||
237 | $themeDir . '/templates/infowindow-description.html' : |
||
238 | 'locator/templates/infowindow-description.html'; |
||
239 | // in page or modal |
||
240 | $modal = ($this->data()->ModalWindow) ? 'modalWindow: true' : 'modalWindow: false'; |
||
241 | $kilometer = ($this->data()->Unit == 'km') ? 'lengthUnit: "km"' : 'lengthUnit: "m"'; |
||
242 | // pass GET variables to xml action |
||
243 | $vars = $this->request->getVars(); |
||
244 | unset($vars['url']); |
||
245 | unset($vars['action_index']); |
||
246 | $url = ''; |
||
247 | if (count($vars)) { |
||
248 | $url .= '?' . http_build_query($vars); |
||
249 | } |
||
250 | $link = $this->AbsoluteLink() . 'xml.xml' . $url; |
||
251 | |||
252 | // init map |
||
253 | if ($locations) { |
||
254 | Requirements::customScript(" |
||
255 | $(function() { |
||
256 | $('#bh-sl-map-container').storeLocator({ |
||
257 | " . $load . " |
||
258 | dataLocation: '" . $link . "', |
||
259 | listTemplatePath: '" . $listTemplatePath . "', |
||
260 | infowindowTemplatePath: '" . $infowindowTemplatePath . "', |
||
261 | originMarker: true, |
||
262 | " . $modal . ', |
||
263 | ' . $featured . ", |
||
264 | slideMap: false, |
||
265 | zoomLevel: 0, |
||
266 | noForm: true, |
||
267 | inputID: 'Form_LocationSearch_Address', |
||
268 | categoryID: 'Form_LocationSearch_category', |
||
269 | distanceAlert: -1, |
||
270 | " . $kilometer . " |
||
271 | }); |
||
272 | }); |
||
273 | "); |
||
274 | } |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * @param SS_HTTPRequest $request |
||
279 | * |
||
280 | * @return ViewableData_Customised |
||
281 | */ |
||
282 | View Code Duplication | public function index(SS_HTTPRequest $request) |
|
294 | |||
295 | /** |
||
296 | * Return a XML feed of all locations marked "show in locator" |
||
297 | * |
||
298 | * @param SS_HTTPRequest $request |
||
299 | * @return HTMLText |
||
300 | */ |
||
301 | View Code Duplication | public function xml(SS_HTTPRequest $request) |
|
313 | |||
314 | /** |
||
315 | * @return ArrayList|DataList |
||
316 | */ |
||
317 | public function getLocations() |
||
324 | |||
325 | /** |
||
326 | * @param SS_HTTPRequest|null $request |
||
327 | * @return $this |
||
328 | */ |
||
329 | public function setLocations(SS_HTTPRequest $request = null) |
||
359 | |||
360 | /** |
||
361 | * @return bool|string |
||
362 | */ |
||
363 | public function getAddressSearchCoords() |
||
375 | |||
376 | /** |
||
377 | * LocationSearch form. |
||
378 | * |
||
379 | * Search form for locations, updates map and results list via AJAX |
||
380 | * |
||
381 | * @return Form |
||
382 | */ |
||
383 | public function LocationSearch() |
||
397 | |||
398 | } |
||
399 |
This check marks private properties in classes that are never used. Those properties can be removed.