Complex classes like action_plugin_spatialhelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use action_plugin_spatialhelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class action_plugin_spatialhelper extends DokuWiki_Action_Plugin { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Register for events. |
||
| 38 | * |
||
| 39 | * @param Doku_Event_Handler $controller |
||
| 40 | * DokuWiki's event controller object. Also available as global $EVENT_HANDLER |
||
| 41 | */ |
||
| 42 | public function register(Doku_Event_Handler $controller) { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Update the spatial index for the page. |
||
| 73 | * |
||
| 74 | * @param Doku_Event $event |
||
| 75 | * event object by reference |
||
| 76 | * @param object $param |
||
| 77 | * the parameters passed to register_hook when this handler was registered |
||
| 78 | */ |
||
| 79 | public function handle_indexer_page_add(Doku_Event $event, $param) { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Update the spatial index, removing the page. |
||
| 90 | * |
||
| 91 | * @param Doku_Event $event |
||
| 92 | * event object by reference |
||
| 93 | * @param object $param |
||
| 94 | * the parameters passed to register_hook when this handler was registered |
||
| 95 | */ |
||
| 96 | public function _removeFromIndex(Doku_Event & $event, $param) { |
||
| 97 | // event data: |
||
| 98 | // $data[0] – The raw arguments for io_saveFile as an array. Do not change file path. |
||
| 99 | // $data[0][0] – the file path. |
||
| 100 | // $data[0][1] – the content to be saved, and may be modified. |
||
| 101 | // $data[1] – ns: The colon separated namespace path minus the trailing page name. (false if root ns) |
||
| 102 | // $data[2] – page_name: The wiki page name. |
||
| 103 | // $data[3] – rev: The page revision, false for current wiki pages. |
||
| 104 | |||
| 105 | dbglog($event->data, "Event data in _removeFromIndex."); |
||
| 106 | if (@file_exists($event->data [0] [0])) { |
||
| 107 | // file not new |
||
| 108 | if (!$event->data [0] [1]) { |
||
| 109 | // file is empty, page is being deleted |
||
| 110 | if (empty ($event->data [1])) { |
||
| 111 | // root namespace |
||
| 112 | $id = $event->data [2]; |
||
| 113 | } else { |
||
| 114 | $id = $event->data [1] . ":" . $event->data [2]; |
||
| 115 | } |
||
| 116 | $indexer = & plugin_load('helper', 'spatialhelper_index'); |
||
| 117 | if ($indexer) { |
||
| 118 | $indexer->deleteFromIndex($id); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Add a new SitemapItem object that points to the KML of public geocoded pages. |
||
| 126 | * |
||
| 127 | * @param Doku_Event $event |
||
| 128 | * @param unknown $param |
||
| 129 | */ |
||
| 130 | public function handle_sitemap_generate_before(Doku_Event $event, $param) { |
||
| 131 | $path = mediaFN($this->getConf('media_kml')); |
||
| 132 | $lastmod = @filemtime($path); |
||
| 133 | $event->data ['items'] [] = new SitemapItem(ml($this->getConf('media_kml'), '', true, '&', true), $lastmod); |
||
| 134 | dbglog($event->data ['items'], "Added a new SitemapItem object that points to the KML of public geocoded pages."); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Create a spatial sitemap or attach the geo/kml map to the sitemap. |
||
| 139 | * |
||
| 140 | * @param Doku_Event $event |
||
| 141 | * event object by reference, not used |
||
| 142 | * @param mixed $param |
||
| 143 | * parameter array, not used |
||
| 144 | */ |
||
| 145 | public function handle_sitemap_generate_after(Doku_Event $event, $param) { |
||
| 146 | // $event→data['items']: Array of SitemapItem instances, the array of sitemap items that already contains all public pages of the wiki |
||
| 147 | // $event→data['sitemap']: The path of the file the sitemap will be saved to. |
||
| 148 | |||
| 149 | dbglog($event->data['items'], "createSpatialSitemap loading helper"); |
||
| 150 | if ($helper = & plugin_load('helper', 'spatialhelper_sitemap')) { |
||
| 151 | dbglog($helper, "createSpatialSitemap loaded helper."); |
||
| 152 | |||
| 153 | $kml = $helper->createKMLSitemap($this->getConf('media_kml')); |
||
| 154 | $rss = $helper->createGeoRSSSitemap($this->getConf('media_georss')); |
||
| 155 | |||
| 156 | return $kml && $rss; |
||
| 157 | } else { |
||
| 158 | dbglog($helper, "createSpatialSitemap NOT loaded helper."); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * trap findnearby action. |
||
| 164 | * This addional handler is required as described at: https://www.dokuwiki.org/devel:event:tpl_act_unknown |
||
| 165 | * |
||
| 166 | * @param Doku_Event $event |
||
| 167 | * event object by reference |
||
| 168 | * @param mixed $param |
||
| 169 | * not used |
||
| 170 | */ |
||
| 171 | public function handle_action_act_preprocess(Doku_Event $event, $param) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * handle findnearby action. |
||
| 180 | * |
||
| 181 | * @param Doku_Event $event |
||
| 182 | * event object by reference |
||
| 183 | * @param mixed $param |
||
| 184 | * associative array with keys |
||
| 185 | * 'format'=> HTML | JSON |
||
| 186 | */ |
||
| 187 | public function _findnearby(Doku_Event & $event, $param) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Print seachresults as HTML lists. |
||
| 222 | * |
||
| 223 | * @param array $searchresults |
||
| 224 | */ |
||
| 225 | private function printJSON($searchresults) { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Print seachresults as HTML lists. |
||
| 234 | * |
||
| 235 | * @param array $searchresults |
||
| 236 | * @param boolean $showMedia |
||
| 237 | */ |
||
| 238 | private function printHTML($searchresults, $showMedia = true) { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * add media to spatial index. |
||
| 297 | * |
||
| 298 | * @param Doku_Event $event |
||
| 299 | * event object by reference |
||
| 300 | * @param unknown $param |
||
| 301 | */ |
||
| 302 | public function _handle_media_uploaded(Doku_Event & $event, $param) { |
||
| 303 | // data[0] temporary file name (read from $_FILES) |
||
| 304 | // data[1] file name of the file being uploaded |
||
| 305 | // data[2] future directory id of the file being uploaded |
||
| 306 | // data[3] the mime type of the file being uploaded |
||
| 307 | // data[4] true if the uploaded file exists already |
||
| 308 | // data[5] (since 2011-02-06) the PHP function used to move the file to the correct location |
||
| 309 | |||
| 310 | dbglog($event->data, "_handle_media_uploaded::event data"); |
||
| 311 | |||
| 312 | // check the list of mimetypes |
||
| 313 | // if it's a supported type call appropriate index function |
||
| 314 | if (substr_compare($event->data [3], 'image/jpeg', 0)) { |
||
| 315 | $indexer = plugin_load('helper', 'spatialhelper_index'); |
||
| 316 | if ($indexer) { |
||
| 317 | $indexer->indexImage($event->data [2], $event->data [1]); |
||
| 318 | } |
||
| 319 | } |
||
| 320 | // TODO add image/tiff |
||
| 321 | // TODO kml, gpx, geojson... |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * removes the media from the index. |
||
| 326 | */ |
||
| 327 | public function _handle_media_deleted(Doku_Event & $event, $param) { |
||
| 328 | // data['id'] ID data['unl'] unlink return code |
||
| 329 | // data['del'] Namespace directory unlink return code |
||
| 330 | // data['name'] file name data['path'] full path to the file |
||
| 331 | // data['size'] file size |
||
| 332 | |||
| 333 | dbglog($event->data, "_handle_media_deleted::event data"); |
||
| 334 | |||
| 335 | // remove the media id from the index |
||
| 336 | $indexer = & plugin_load('helper', 'spatialhelper_index'); |
||
| 337 | if ($indexer) { |
||
| 338 | $indexer->deleteFromIndex('media__' . $event->data ['id']); |
||
| 339 | } |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * add a link to the spatial sitemap files in the header. |
||
| 344 | * |
||
| 345 | * @param Doku_Event $event |
||
| 346 | * the DokuWiki event. $event->data is a two-dimensional |
||
| 347 | * array of all meta headers. The keys are meta, link and script. |
||
| 348 | * @param unknown_type $param |
||
| 349 | * |
||
| 350 | * @see http://www.dokuwiki.org/devel:event:tpl_metaheader_output |
||
| 351 | */ |
||
| 352 | public function handle_metaheader_output(Doku_Event $event, $param) { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Calculate a new coordinate based on start, distance and bearing |
||
| 370 | * |
||
| 371 | * @param $start array |
||
| 372 | * - start coordinate as decimal lat/lon pair |
||
| 373 | * @param $dist float |
||
| 374 | * - distance in kilometers |
||
| 375 | * @param $brng float |
||
| 376 | * - bearing in degrees (compass direction) |
||
| 377 | */ |
||
| 378 | private function _geo_destination($start, $dist, $brng) { |
||
| 400 | } |
||
| 401 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.