Complex classes like Indexer 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 Indexer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class Indexer extends Tree |
||
| 8 | { |
||
| 9 | const TARGET_INDEX_PERCENT = 0.91; |
||
| 10 | const DEFAULT_ZONE_RESULT = -1; |
||
| 11 | const LEVEL_DELIMITER_SYMBOL = "."; |
||
| 12 | const TOTAL_LEVELS = 4; |
||
| 13 | |||
| 14 | protected $dataSourcePath = null; |
||
| 15 | protected $dataSource; |
||
| 16 | protected $timezones = array(); |
||
| 17 | protected $lookup = array(); |
||
| 18 | protected $currentQuadrants = array(); |
||
| 19 | |||
| 20 | |||
| 21 | public function __construct($dataDirectory = null, $dataSourcePath = null) |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Initialize the current quadrants attribute for the first indexing iteration |
||
| 29 | */ |
||
| 30 | protected function initCurrentQuadrants() |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Read the new timezones json to be indexed |
||
| 54 | */ |
||
| 55 | protected function readDataSource() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Save timezones values from the reference file (timezones json) to timezones array attribute |
||
| 67 | */ |
||
| 68 | protected function setTimezonesArray() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Find the timezones that intersect with or are within the quadrant polygon |
||
| 77 | * @param $timezonesToInspect |
||
| 78 | * @param $quadrantBounds |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | protected function whichTimeZonesIntersect($timezonesToInspect, $quadrantBounds) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Create new level of quadrants from the previous bounds, the intersected found timezones and the previous zone ID |
||
| 106 | * @param $zoneId |
||
| 107 | * @param $intersectedZones |
||
| 108 | * @param $quadrantBounds |
||
| 109 | * @return array |
||
| 110 | */ |
||
| 111 | protected function getNextQuadrants($zoneId, $intersectedZones, $quadrantBounds) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Select timezones to find the intersections |
||
| 162 | * @param $previousTimezone |
||
| 163 | * @return array |
||
| 164 | */ |
||
| 165 | protected function selectTimeZonesToInspect($previousTimezone) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Update the lookup table |
||
| 180 | * @param $zoneResult |
||
| 181 | * @param $curQuadrantId |
||
| 182 | */ |
||
| 183 | protected function updateLookup($zoneResult, $curQuadrantId) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Get intersection features from current quadrant and each intersected timezone areas |
||
| 195 | * @param $intersectionResult |
||
| 196 | * @param $curQuadrant |
||
| 197 | * @return array |
||
| 198 | */ |
||
| 199 | protected function getIntersectionFeatures($intersectionResult, $curQuadrant) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Find the associated zones to the current quadrants and the next quadrants to be evaluated |
||
| 220 | * @param $intersectionResult |
||
| 221 | * @param $curQuadrant |
||
| 222 | * @param $lastLevelFlag |
||
| 223 | * @return array |
||
| 224 | */ |
||
| 225 | protected function getAssociatedZonesAndNextQuadrants($intersectionResult, $curQuadrant, $lastLevelFlag) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Check if the current indexing iteration should be carry on or not |
||
| 260 | * @param $curLevel |
||
| 261 | * @param $numQuadrants |
||
| 262 | * @return bool |
||
| 263 | */ |
||
| 264 | protected function validIndexingPercentage($curLevel, $numQuadrants) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Index current quadrants and get the new ones |
||
| 275 | * @param $lastLevelFlag |
||
| 276 | * @return array |
||
| 277 | */ |
||
| 278 | protected function indexQuadrants($lastLevelFlag) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Main function that run all index processing |
||
| 292 | */ |
||
| 293 | protected function generateIndexes() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Create the directory tree |
||
| 314 | * @param $path |
||
| 315 | */ |
||
| 316 | protected function createDirectoryTree($path) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Create json file from timezone features |
||
| 331 | * @param $features |
||
| 332 | * @param $path |
||
| 333 | * @return bool|int |
||
| 334 | */ |
||
| 335 | protected function writeGeoFeaturesToJson($features, $path) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Build tree array to be save in a json file later |
||
| 348 | * @return array |
||
| 349 | */ |
||
| 350 | protected function buildTree() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Write the quadrant tree in a json file |
||
| 361 | * @return bool|int |
||
| 362 | */ |
||
| 363 | protected function writeQuadrantTreeJson() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Set the input data source |
||
| 377 | * @param $path |
||
| 378 | * @throws ErrorException |
||
| 379 | */ |
||
| 380 | public function setGeoDataSource($path) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Main public function that starts all processing |
||
| 391 | */ |
||
| 392 | public function createQuadrantTreeData() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Find the intersected timezones and the next quadrant to be evaluated |
||
| 404 | * @param $lastLevelFlag |
||
| 405 | * @param $curQuadrant |
||
| 406 | * @return array |
||
| 407 | */ |
||
| 408 | protected function findTimezonesAndNextQuadrants($lastLevelFlag, $curQuadrant) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Add level to the lookup table where the quadrant tree is being defined |
||
| 422 | * @param $zoneResult |
||
| 423 | * @param $levelPath |
||
| 424 | * @return mixed |
||
| 425 | */ |
||
| 426 | protected function addLevelToLookup($zoneResult, $levelPath) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Remove level from the lookup table where the quadrant tree is being defined |
||
| 437 | * @param $levelPath |
||
| 438 | */ |
||
| 439 | protected function removeLevelFromLookup($levelPath) |
||
| 452 | } |
||
| 453 |