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) { |
||
| 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 | if ($helper = & plugin_load('helper', 'spatialhelper_sitemap')) { |
||
| 149 | // dbglog($helper, "createSpatialSitemap loaded helper."); |
||
| 150 | |||
| 151 | $kml = $helper->createKMLSitemap($this->getConf('media_kml')); |
||
| 152 | $rss = $helper->createGeoRSSSitemap($this->getConf('media_georss')); |
||
| 153 | |||
| 154 | if (!empty ($this->getConf('sitemap_namespaces'))) { |
||
| 155 | $namespaces = array_map('trim', explode("\n", $this->getConf('sitemap_namespaces'))); |
||
| 156 | foreach ($namespaces as $namespace) { |
||
| 157 | $kmlN = $helper->createKMLSitemap($namespace . $this->getConf('media_kml')); |
||
| 158 | $rssN = $helper->createGeoRSSSitemap($namespace . $this->getConf('media_georss')); |
||
| 159 | dbglog($kmlN && $rssN, "handle_sitemap_generate_after, created KML / GeoRSS sitemap in $namespace, succes: "); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | return $kml && $rss; |
||
| 163 | } else { |
||
| 164 | dbglog($helper, "createSpatialSitemap NOT loaded helper."); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * trap findnearby action. |
||
| 170 | * This addional handler is required as described at: https://www.dokuwiki.org/devel:event:tpl_act_unknown |
||
| 171 | * |
||
| 172 | * @param Doku_Event $event |
||
| 173 | * event object by reference |
||
| 174 | * @param mixed $param |
||
| 175 | * not used |
||
| 176 | */ |
||
| 177 | public function handle_action_act_preprocess(Doku_Event $event, $param) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * handle findnearby action. |
||
| 186 | * |
||
| 187 | * @param Doku_Event $event |
||
| 188 | * event object by reference |
||
| 189 | * @param mixed $param |
||
| 190 | * associative array with keys |
||
| 191 | * 'format'=> HTML | JSON |
||
| 192 | */ |
||
| 193 | public function _findnearby(Doku_Event & $event, $param) { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Print seachresults as HTML lists. |
||
| 228 | * |
||
| 229 | * @param array $searchresults |
||
| 230 | */ |
||
| 231 | private function printJSON($searchresults) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Print seachresults as HTML lists. |
||
| 240 | * |
||
| 241 | * @param array $searchresults |
||
| 242 | * @param boolean $showMedia |
||
| 243 | */ |
||
| 244 | private function printHTML($searchresults, $showMedia = true) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * add media to spatial index. |
||
| 303 | * |
||
| 304 | * @param Doku_Event $event |
||
| 305 | * event object by reference |
||
| 306 | * @param unknown $param |
||
| 307 | */ |
||
| 308 | public function _handle_media_uploaded(Doku_Event & $event, $param) { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * removes the media from the index. |
||
| 332 | */ |
||
| 333 | public function _handle_media_deleted(Doku_Event & $event, $param) { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * add a link to the spatial sitemap files in the header. |
||
| 350 | * |
||
| 351 | * @param Doku_Event $event |
||
| 352 | * the DokuWiki event. $event->data is a two-dimensional |
||
| 353 | * array of all meta headers. The keys are meta, link and script. |
||
| 354 | * @param unknown_type $param |
||
| 355 | * |
||
| 356 | * @see http://www.dokuwiki.org/devel:event:tpl_metaheader_output |
||
| 357 | */ |
||
| 358 | public function handle_metaheader_output(Doku_Event $event, $param) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Calculate a new coordinate based on start, distance and bearing |
||
| 376 | * |
||
| 377 | * @param $start array |
||
| 378 | * - start coordinate as decimal lat/lon pair |
||
| 379 | * @param $dist float |
||
| 380 | * - distance in kilometers |
||
| 381 | * @param $brng float |
||
| 382 | * - bearing in degrees (compass direction) |
||
| 383 | */ |
||
| 384 | private function _geo_destination($start, $dist, $brng) { |
||
| 406 | } |
||
| 407 |
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.