Conditions | 11 |
Paths | 194 |
Total Lines | 93 |
Code Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
81 | public function init() |
||
82 | { |
||
83 | parent::init(); |
||
84 | // google maps api key |
||
85 | $key = Config::inst()->get(GoogleGeocoder::class, 'geocoder_api_key'); |
||
86 | Requirements::javascript('https://maps.google.com/maps/api/js?key=' . $key); |
||
87 | |||
88 | // prevent init of map if no query |
||
89 | $request = Controller::curr()->getRequest(); |
||
90 | |||
91 | if ($this->getTrigger($request)) { |
||
92 | $locations = $this->getLocations(); |
||
93 | |||
94 | if ($locations) { |
||
95 | $featuredInList = ($locations->filter('Featured', true)->count() > 0); |
||
96 | $defaultCoords = $this->getAddressSearchCoords() ? |
||
97 | $this->getAddressSearchCoords() : |
||
98 | new ArrayData([ |
||
99 | "Lat" => 0, |
||
100 | "Lng" => 0, |
||
101 | ]); |
||
102 | |||
103 | $featured = $featuredInList |
||
104 | ? 'featuredLocations: true' |
||
105 | : 'featuredLocations: false'; |
||
106 | |||
107 | // map config based on user input in Settings tab |
||
108 | $limit = Config::inst()->get(LocatorController::class, 'limit'); |
||
109 | if ($limit < 1) { |
||
110 | $limit = -1; |
||
111 | } |
||
112 | $load = 'fullMapStart: true, storeLimit: ' . $limit . ', maxDistance: true,'; |
||
113 | |||
114 | $listTemplatePath = $this->getListTemplate(); |
||
115 | $infowindowTemplatePath = $this->getInfoWindowTemplate(); |
||
116 | |||
117 | $kilometer = ($this->data()->Unit == 'km') ? "lengthUnit: 'km'" : "lengthUnit: 'm'"; |
||
118 | |||
119 | // pass GET variables to xml action |
||
120 | $vars = $this->request->getVars(); |
||
121 | unset($vars['url']); |
||
122 | $url = ''; |
||
123 | if (count($vars)) { |
||
124 | $url .= '?' . http_build_query($vars); |
||
125 | } |
||
126 | $link = Controller::join_links($this->Link(), 'xml.xml', $url); |
||
127 | |||
128 | // containers |
||
129 | $map_id = Config::inst()->get(LocatorController::class, 'map_container'); |
||
130 | $list_class = Config::inst()->get(LocatorController::class, 'list_container'); |
||
131 | |||
132 | $mapStyle = ''; |
||
133 | if ($stylePath = $this->getMapStyleJSONPath()) { |
||
134 | if ($style = file_get_contents($stylePath)) { |
||
135 | $mapStyle = "styles: {$style},"; |
||
136 | } |
||
137 | }; |
||
138 | |||
139 | $markerImage = ''; |
||
140 | if ($imagePath = $this->getMarkerIcon()) { |
||
141 | $markerImage = "markerImg: '{$imagePath}',"; |
||
142 | } |
||
143 | |||
144 | // init map |
||
145 | Requirements::customScript(" |
||
146 | $(function(){ |
||
147 | $('#map-container').storeLocator({ |
||
148 | {$load} |
||
149 | dataLocation: '{$link}', |
||
150 | listTemplatePath: '{$listTemplatePath}', |
||
151 | infowindowTemplatePath: '{$infowindowTemplatePath}', |
||
152 | {$markerImage} |
||
153 | originMarker: true, |
||
154 | {$featured}, |
||
155 | slideMap: false, |
||
156 | distanceAlert: -1, |
||
157 | {$kilometer}, |
||
158 | defaultLat: {$defaultCoords->getField("Lat")}, |
||
159 | defaultLng: {$defaultCoords->getField("Lng")}, |
||
160 | mapID: '{$map_id}', |
||
161 | locationList: '{$list_class}', |
||
162 | mapSettings: { |
||
163 | {$mapStyle} |
||
164 | zoom: 12, |
||
165 | mapTypeId: google.maps.MapTypeId.ROADMAP, |
||
166 | disableDoubleClickZoom: true, |
||
167 | scrollwheel: false, |
||
168 | navigationControl: false, |
||
169 | draggable: false |
||
170 | } |
||
171 | }); |
||
172 | }); |
||
173 | ", 'jquery-locator'); |
||
174 | } |
||
365 |