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.96; |
||
| 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) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Create new level of quadrants from the previous bounds, the intersected found timezones and the previous zone ID |
||
| 108 | * @param $zoneId |
||
| 109 | * @param $intersectedZones |
||
| 110 | * @param $quadrantBounds |
||
| 111 | * @return array |
||
| 112 | */ |
||
| 113 | protected function getNextQuadrants($zoneId, $intersectedZones, $quadrantBounds) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Select timezones to find the intersections |
||
| 164 | * @param $previousTimezone |
||
| 165 | * @return array |
||
| 166 | */ |
||
| 167 | protected function selectTimeZonesToInspect($previousTimezone) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Update the lookup table |
||
| 182 | * @param $zoneResult |
||
| 183 | * @param $curQuadrantId |
||
| 184 | */ |
||
| 185 | protected function updateLookup($zoneResult, $curQuadrantId) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Get intersection features from current quadrant and each intersected timezone areas |
||
| 197 | * @param $intersectionResult |
||
| 198 | * @param $curQuadrant |
||
| 199 | * @return array |
||
| 200 | */ |
||
| 201 | protected function getIntersectionFeatures($intersectionResult, $curQuadrant) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Find the associated zones to the current quadrants and the next quadrants to be evaluated |
||
| 223 | * @param $intersectionResult |
||
| 224 | * @param $curQuadrant |
||
| 225 | * @param $lastLevelFlag |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | protected function getAssociatedZonesAndNextQuadrants($intersectionResult, $curQuadrant, $lastLevelFlag) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Check if the current indexing iteration should be carry on or not |
||
| 263 | * @param $curLevel |
||
| 264 | * @param $numQuadrants |
||
| 265 | * @return bool |
||
| 266 | */ |
||
| 267 | protected function validIndexingPercentage($curLevel, $numQuadrants) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Index current quadrants and get the new ones |
||
| 278 | * @param $lastLevelFlag |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | protected function indexQuadrants($lastLevelFlag) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Main function that run all index processing |
||
| 295 | */ |
||
| 296 | protected function generateIndexes() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Create the directory tree |
||
| 317 | * @param $path |
||
| 318 | */ |
||
| 319 | protected function createDirectoryTree($path) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Create json file from timezone features |
||
| 334 | * @param $features |
||
| 335 | * @param $path |
||
| 336 | * @return bool|int |
||
| 337 | */ |
||
| 338 | protected function writeGeoFeaturesToJson($features, $path) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Build tree array to be save in a json file later |
||
| 351 | * @return array |
||
| 352 | */ |
||
| 353 | protected function buildTree() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Write the quadrant tree in a json file |
||
| 364 | * @return bool|int |
||
| 365 | */ |
||
| 366 | protected function writeQuadrantTreeJson() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Set the input data source |
||
| 380 | * @param $path |
||
| 381 | * @throws ErrorException |
||
| 382 | */ |
||
| 383 | public function setGeoDataSource($path) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Main public function that starts all processing |
||
| 394 | */ |
||
| 395 | public function createQuadrantTreeData() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Find the intersected timezones and the next quadrant to be evaluated |
||
| 407 | * @param $lastLevelFlag |
||
| 408 | * @param $curQuadrant |
||
| 409 | * @return array |
||
| 410 | */ |
||
| 411 | protected function findTimezonesAndNextQuadrants($lastLevelFlag, $curQuadrant) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Add level to the lookup table where the quadrant tree is being defined |
||
| 425 | * @param $zoneResult |
||
| 426 | * @param $levelPath |
||
| 427 | * @return mixed |
||
| 428 | */ |
||
| 429 | protected function addLevelToLookup($zoneResult, $levelPath) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Remove level from the lookup table where the quadrant tree is being defined |
||
| 440 | * @param $levelPath |
||
| 441 | */ |
||
| 442 | protected function removeLevelFromLookup($levelPath) |
||
| 455 | } |
||
| 456 |