Complex classes like ShapeFile 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 ShapeFile, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class ShapeFile |
||
| 29 | { |
||
| 30 | const MAGIC = 0x270a; |
||
| 31 | |||
| 32 | public $FileName; |
||
| 33 | |||
| 34 | private $SHPFile = null; |
||
| 35 | private $SHXFile = null; |
||
| 36 | private $DBFFile = null; |
||
| 37 | |||
| 38 | private $DBFHeader; |
||
| 39 | |||
| 40 | public $lastError = ''; |
||
| 41 | |||
| 42 | public $boundingBox = [ |
||
| 43 | 'xmin' => 0.0, |
||
| 44 | 'ymin' => 0.0, |
||
| 45 | 'xmax' => 0.0, |
||
| 46 | 'ymax' => 0.0, |
||
| 47 | ]; |
||
| 48 | private $fileLength = 0; |
||
| 49 | public $shapeType = 0; |
||
| 50 | |||
| 51 | public $records = []; |
||
| 52 | |||
| 53 | 21 | /** |
|
| 54 | * Checks whether dbase manipuations are supported. |
||
| 55 | 21 | * |
|
| 56 | * @return bool |
||
| 57 | */ |
||
| 58 | public static function supportsDbase() |
||
| 62 | |||
| 63 | 24 | /** |
|
| 64 | * @param int $shapeType File shape type, should be same as all records |
||
| 65 | 24 | * @param array $boundingBox File bounding box |
|
| 66 | 24 | * @param null|mixed $FileName File name |
|
| 67 | 24 | */ |
|
| 68 | 24 | public function __construct( |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Loads shapefile and dbase (if supported). |
||
| 86 | 22 | * |
|
| 87 | 20 | * @param string $FileName File mask to load (eg. example.*) |
|
| 88 | 1 | */ |
|
| 89 | 1 | public function loadFromFile($FileName) |
|
| 120 | 13 | ||
| 121 | 13 | /** |
|
| 122 | 13 | * Saves shapefile. |
|
| 123 | 13 | * |
|
| 124 | 13 | * @param string|null $FileName Name of file, otherwise existing is used |
|
| 125 | 13 | */ |
|
| 126 | public function saveToFile($FileName = null) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Generates filename with given extension. |
||
| 145 | * |
||
| 146 | * @param string $extension Extension to use (including dot) |
||
| 147 | * |
||
| 148 | 12 | * @return string |
|
| 149 | */ |
||
| 150 | 12 | private function _getFilename($extension) |
|
| 154 | 12 | ||
| 155 | 12 | /** |
|
| 156 | 12 | * Updates bounding box based on SHPData. |
|
| 157 | 12 | * |
|
| 158 | 12 | * @param string $type Type of box |
|
| 159 | 12 | * @param array $data ShapeRecord SHPData |
|
| 160 | */ |
||
| 161 | private function updateBBox($type, $data) |
||
| 173 | |||
| 174 | 12 | /** |
|
| 175 | 12 | * Adds record to shape file. |
|
| 176 | 12 | * |
|
| 177 | * @param ShapeRecord $record |
||
| 178 | 12 | * |
|
| 179 | 12 | * @return int Number of added record |
|
| 180 | */ |
||
| 181 | 12 | public function addRecord($record) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Deletes record from shapefile. |
||
| 207 | * |
||
| 208 | * @param int $index |
||
| 209 | */ |
||
| 210 | public function deleteRecord($index) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Returns array defining fields in DBF file. |
||
| 225 | * |
||
| 226 | * @return array see setDBFHeader for more information |
||
| 227 | */ |
||
| 228 | public function getDBFHeader() |
||
| 232 | |||
| 233 | 12 | /** |
|
| 234 | 12 | * Changes array defining fields in DBF file, used in dbase_create call. |
|
| 235 | * |
||
| 236 | * @param array $header An array of arrays, each array describing the |
||
| 237 | 12 | * format of one field of the database. Each |
|
| 238 | * field consists of a name, a character indicating |
||
| 239 | * the field type, and optionally, a length, |
||
| 240 | * a precision and a nullable flag. |
||
| 241 | */ |
||
| 242 | public function setDBFHeader($header) |
||
| 251 | |||
| 252 | 1 | /** |
|
| 253 | * Lookups value in the DBF file and returs index. |
||
| 254 | * |
||
| 255 | 1 | * @param string $field Field to match |
|
| 256 | * @param mixed $value Value to match |
||
| 257 | 1 | * |
|
| 258 | * @return int |
||
| 259 | */ |
||
| 260 | public function getIndexFromDBFData($field, $value) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Loads DBF metadata. |
||
| 275 | */ |
||
| 276 | private function _loadDBFHeader() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Deletes record from the DBF file. |
||
| 315 | * |
||
| 316 | * @param int $index |
||
| 317 | 20 | */ |
|
| 318 | private function _deleteRecordFromDBF($index) |
||
| 324 | |||
| 325 | /** |
||
| 326 | 19 | * Loads SHP file metadata. |
|
| 327 | * |
||
| 328 | 19 | * @return bool |
|
| 329 | */ |
||
| 330 | private function _loadHeaders() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Saves bounding box record, possibly using 0 instead of not set values. |
||
| 367 | * |
||
| 368 | * @param file $file File object |
||
| 369 | * @param string $type Bounding box dimension (eg. xmax, mmin...) |
||
| 370 | 13 | */ |
|
| 371 | private function _saveBBoxRecord($file, $type) |
||
| 377 | 13 | ||
| 378 | 13 | /** |
|
| 379 | 13 | * Saves bounding box to a file. |
|
| 380 | 13 | * |
|
| 381 | * @param file $file File object |
||
| 382 | */ |
||
| 383 | private function _saveBBox($file) |
||
| 394 | 13 | ||
| 395 | 13 | /** |
|
| 396 | 13 | * Saves SHP and SHX file metadata. |
|
| 397 | 13 | */ |
|
| 398 | 13 | private function _saveHeaders() |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Loads records from SHP file (and DBF). |
||
| 415 | * |
||
| 416 | 19 | * @return bool |
|
| 417 | 19 | */ |
|
| 418 | private function _loadRecords() |
||
| 438 | 12 | ||
| 439 | 12 | /** |
|
| 440 | 12 | * Saves records to SHP and SHX files. |
|
| 441 | 12 | */ |
|
| 442 | 12 | private function _saveRecords() |
|
| 457 | 23 | ||
| 458 | 23 | /** |
|
| 459 | 2 | * Generic interface to open files. |
|
| 460 | * |
||
| 461 | 2 | * @param bool $toWrite Whether file should be opened for writing |
|
| 462 | * @param string $extension File extension |
||
| 463 | * @param string $name Verbose file name to report errors |
||
| 464 | 21 | * |
|
| 465 | * @return file|false File handle |
||
| 466 | */ |
||
| 467 | private function _openFile($toWrite, $extension, $name) |
||
| 479 | |||
| 480 | /** |
||
| 481 | 21 | * Opens SHP file. |
|
| 482 | * |
||
| 483 | * @param bool $toWrite Whether file should be opened for writing |
||
| 484 | * |
||
| 485 | * @return bool |
||
| 486 | */ |
||
| 487 | 21 | private function _openSHPFile($toWrite = false) |
|
| 496 | |||
| 497 | /** |
||
| 498 | * Closes SHP file. |
||
| 499 | */ |
||
| 500 | private function _closeSHPFile() |
||
| 507 | |||
| 508 | /** |
||
| 509 | 13 | * Opens SHX file. |
|
| 510 | * |
||
| 511 | * @param bool $toWrite Whether file should be opened for writing |
||
| 512 | * |
||
| 513 | * @return bool |
||
| 514 | */ |
||
| 515 | 13 | private function _openSHXFile($toWrite = false) |
|
| 524 | |||
| 525 | /** |
||
| 526 | * Closes SHX file. |
||
| 527 | */ |
||
| 528 | 13 | private function _closeSHXFile() |
|
| 535 | |||
| 536 | /** |
||
| 537 | * Creates DBF file. |
||
| 538 | * |
||
| 539 | * @return bool |
||
| 540 | */ |
||
| 541 | private function _createDBFFile() |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Loads DBF file if supported. |
||
| 568 | * |
||
| 569 | * @return bool |
||
| 570 | */ |
||
| 571 | private function _openDBFFile() |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Closes DBF file. |
||
| 597 | */ |
||
| 598 | 3 | private function _closeDBFFile() |
|
| 605 | |||
| 606 | /** |
||
| 607 | * Sets error message. |
||
| 608 | * |
||
| 609 | * @param string $error |
||
| 610 | 20 | */ |
|
| 611 | public function setError($error) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Reads given number of bytes from SHP file. |
||
| 618 | * |
||
| 619 | * @param int $bytes |
||
| 620 | 19 | * |
|
| 621 | * @return string |
||
| 622 | 19 | */ |
|
| 623 | public function readSHP($bytes) |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Checks whether file is at EOF. |
||
| 630 | 1 | * |
|
| 631 | * @return bool |
||
| 632 | 1 | */ |
|
| 633 | public function eofSHP() |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Returns shape name. |
||
| 640 | * |
||
| 641 | * @return string |
||
| 642 | */ |
||
| 643 | 8 | public function getShapeName() |
|
| 647 | |||
| 648 | /** |
||
| 649 | * Check whether file contains measure data. |
||
| 650 | * |
||
| 651 | * For some reason this is distinguished by zero bounding box in the |
||
| 652 | * specification. |
||
| 653 | * |
||
| 654 | * @return bool |
||
| 655 | */ |
||
| 656 | public function hasMeasure() |
||
| 660 | } |
||
| 661 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.