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 |
||
146 | class Locator_Controller extends Page_Controller |
||
147 | { |
||
148 | /** |
||
149 | * @var array |
||
150 | */ |
||
151 | private static $allowed_actions = array( |
||
152 | 'xml', |
||
153 | ); |
||
154 | |||
155 | /** |
||
156 | * @var array |
||
157 | */ |
||
158 | private static $base_filter = []; |
||
159 | |||
160 | /** |
||
161 | * @var array |
||
162 | */ |
||
163 | private static $base_exclude = [ |
||
164 | 'Lat' => 0, |
||
165 | 'Lng' => 0, |
||
166 | ]; |
||
167 | |||
168 | /** |
||
169 | * @var array |
||
170 | */ |
||
171 | private static $base_filter_any = []; |
||
172 | |||
173 | /** |
||
174 | * @var string |
||
175 | */ |
||
176 | private static $list_template_path = 'locator/templates/location-list-description.html'; |
||
177 | |||
178 | /** |
||
179 | * @var string |
||
180 | */ |
||
181 | private static $info_window_template_path = 'locator/templates/infowindow-description.html'; |
||
182 | |||
183 | /** |
||
184 | * @var bool |
||
185 | */ |
||
186 | private static $bootstrapify = true; |
||
187 | |||
188 | /** |
||
189 | * @var int |
||
190 | */ |
||
191 | private static $limit = 50; |
||
192 | |||
193 | /** |
||
194 | * ID of map container |
||
195 | * |
||
196 | * @var string |
||
197 | */ |
||
198 | private static $map_container = 'map'; |
||
199 | |||
200 | /** |
||
201 | * class of location list container |
||
202 | * |
||
203 | * @var string |
||
204 | */ |
||
205 | private static $list_container = 'loc-list'; |
||
206 | |||
207 | /** |
||
208 | * GET variable which, if isset, will trigger storeLocator init and return XML |
||
209 | * |
||
210 | * @var string |
||
211 | */ |
||
212 | private static $query_trigger = 'action_doFilterLocations'; |
||
213 | |||
214 | /** |
||
215 | * @var DataList|ArrayList |
||
216 | */ |
||
217 | protected $locations; |
||
218 | |||
219 | /** |
||
220 | * Set Requirements based on input from CMS |
||
221 | */ |
||
222 | public function init() |
||
223 | { |
||
224 | parent::init(); |
||
225 | |||
226 | // prevent init of map if no query |
||
227 | $request = Controller::curr()->getRequest(); |
||
228 | if ($this->getTrigger($request)) { |
||
229 | // google maps api key |
||
230 | $key = Config::inst()->get('GoogleGeocoding', 'google_api_key'); |
||
231 | |||
232 | $locations = $this->getLocations(); |
||
233 | |||
234 | if ($locations) { |
||
235 | |||
236 | Requirements::css('locator/css/map.css'); |
||
237 | Requirements::javascript('framework/thirdparty/jquery/jquery.js'); |
||
238 | Requirements::javascript('https://maps.google.com/maps/api/js?key=' . $key); |
||
239 | Requirements::javascript('locator/thirdparty/jquery-store-locator-plugin/assets/js/libs/handlebars.min.js'); |
||
240 | Requirements::javascript('locator/thirdparty/jquery-store-locator-plugin/assets/js/plugins/storeLocator/jquery.storelocator.js'); |
||
241 | |||
242 | $featuredInList = ($locations->filter('Featured', true)->count() > 0); |
||
243 | $defaultCoords = $this->getAddressSearchCoords() ? $this->getAddressSearchCoords() : ''; |
||
244 | |||
245 | $featured = $featuredInList |
||
246 | ? 'featuredLocations: true' |
||
247 | : 'featuredLocations: false'; |
||
248 | |||
249 | // map config based on user input in Settings tab |
||
250 | $limit = Config::inst()->get('Locator_Controller', 'limit'); |
||
251 | if ($limit < 1) $limit = -1; |
||
252 | $load = 'fullMapStart: true, storeLimit: ' . $limit . ', maxDistance: true,'; |
||
253 | |||
254 | $listTemplatePath = Config::inst()->get('Locator_Controller', 'list_template_path'); |
||
255 | $infowindowTemplatePath = Config::inst()->get('Locator_Controller', 'info_window_template_path'); |
||
256 | |||
257 | $kilometer = ($this->data()->Unit == 'km') ? "lengthUnit: 'km'" : "lengthUnit: 'm'"; |
||
258 | |||
259 | // pass GET variables to xml action |
||
260 | $vars = $this->request->getVars(); |
||
261 | unset($vars['url']); |
||
262 | $url = ''; |
||
263 | if (count($vars)) { |
||
264 | $url .= '?' . http_build_query($vars); |
||
265 | } |
||
266 | $link = Controller::join_links($this->Link(), 'xml.xml', $url); |
||
267 | |||
268 | // containers |
||
269 | $map_id = Config::inst()->get('Locator_Controller', 'map_container'); |
||
270 | $list_class = Config::inst()->get('Locator_Controller', 'list_container'); |
||
271 | |||
272 | // init map |
||
273 | Requirements::customScript(" |
||
274 | $(function(){ |
||
275 | $('#map-container').storeLocator({ |
||
276 | " . $load . " |
||
277 | dataLocation: '" . $link . "', |
||
278 | listTemplatePath: '" . $listTemplatePath . "', |
||
279 | infowindowTemplatePath: '" . $infowindowTemplatePath . "', |
||
280 | originMarker: true, |
||
281 | " . $featured . ", |
||
282 | slideMap: false, |
||
283 | distanceAlert: -1, |
||
284 | " . $kilometer . ", |
||
285 | " . $defaultCoords . " |
||
286 | mapID: '" . $map_id . "', |
||
287 | locationList: '" . $list_class . "', |
||
288 | mapSettings: { |
||
289 | zoom: 12, |
||
290 | mapTypeId: google.maps.MapTypeId.ROADMAP, |
||
291 | disableDoubleClickZoom: true, |
||
292 | scrollwheel: false, |
||
293 | navigationControl: false, |
||
294 | draggable: false |
||
295 | } |
||
296 | }); |
||
297 | }); |
||
298 | ", "locator_map_init_script"); |
||
299 | } |
||
300 | } |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * @param SS_HTTPRequest $request |
||
305 | * |
||
306 | * @return ViewableData_Customised |
||
307 | */ |
||
308 | View Code Duplication | public function index(SS_HTTPRequest $request) |
|
309 | { |
||
310 | if ($this->getTrigger($request)) { |
||
311 | $locations = $this->getLocations(); |
||
312 | } else { |
||
313 | $locations = ArrayList::create(); |
||
314 | } |
||
315 | |||
316 | return $this->customise(array( |
||
317 | 'Locations' => $locations, |
||
318 | )); |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Return a XML feed of all locations marked "show in locator" |
||
323 | * |
||
324 | * @param SS_HTTPRequest $request |
||
325 | * @return HTMLText |
||
326 | */ |
||
327 | View Code Duplication | public function xml(SS_HTTPRequest $request) |
|
328 | { |
||
329 | if ($this->getTrigger($request)) { |
||
330 | $locations = $this->getLocations(); |
||
331 | } else { |
||
332 | $locations = ArrayList::create(); |
||
333 | } |
||
334 | |||
335 | return $this->customise(array( |
||
336 | 'Locations' => $locations, |
||
337 | ))->renderWith('LocationXML'); |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * @return ArrayList|DataList |
||
342 | */ |
||
343 | public function getLocations() |
||
350 | |||
351 | /** |
||
352 | * @param SS_HTTPRequest|null $request |
||
353 | * @return $this |
||
354 | */ |
||
355 | public function setLocations(SS_HTTPRequest $request = null) |
||
411 | |||
412 | /** |
||
413 | * @param SS_HTTPRequest $request |
||
414 | * @return bool |
||
415 | */ |
||
416 | public function getTrigger(SS_HTTPRequest $request = null) |
||
417 | { |
||
418 | if ($request === null) { |
||
419 | $request = $this->getRequest(); |
||
420 | } |
||
421 | $trigger = $request->getVar(Config::inst()->get('Locator_Controller', 'query_trigger')); |
||
422 | return isset($trigger); |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * @return bool|string |
||
427 | */ |
||
428 | public function getAddressSearchCoords() |
||
440 | |||
441 | /** |
||
442 | * LocationSearch form. |
||
443 | * |
||
444 | * Search form for locations, updates map and results list via AJAX |
||
445 | * |
||
446 | * @return Form |
||
447 | */ |
||
448 | public function LocationSearch() |
||
463 | |||
464 | } |
||
465 |
This check marks private properties in classes that are never used. Those properties can be removed.