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 = array('xmin' => 0.0, 'ymin' => 0.0, 'xmax' => 0.0, 'ymax' => 0.0); |
||
43 | private $fileLength = 0; |
||
44 | public $shapeType = 0; |
||
45 | |||
46 | public $records = array(); |
||
47 | |||
48 | /** |
||
49 | * Checks whether dbase manipuations are supported. |
||
50 | * |
||
51 | * @return bool |
||
52 | */ |
||
53 | 21 | public static function supports_dbase() |
|
57 | |||
58 | /** |
||
59 | * @param int $shapeType |
||
60 | */ |
||
61 | 24 | public function __construct($shapeType, $boundingBox = array('xmin' => 0.0, 'ymin' => 0.0, 'xmax' => 0.0, 'ymax' => 0.0), $FileName = null) |
|
68 | |||
69 | /** |
||
70 | * Loads shapefile and dbase (if supported). |
||
71 | * |
||
72 | * @param string $FileName File mask to load (eg. example.*) |
||
73 | */ |
||
74 | 22 | public function loadFromFile($FileName) |
|
105 | |||
106 | /** |
||
107 | * Saves shapefile. |
||
108 | * |
||
109 | * @param string|null $FileName Name of file, otherwise existing is used |
||
110 | */ |
||
111 | 13 | public function saveToFile($FileName = null) |
|
127 | |||
128 | /** |
||
129 | * Generates filename with given extension. |
||
130 | * |
||
131 | * @param string $extension Extension to use (including dot) |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | 23 | private function _getFilename($extension) |
|
139 | |||
140 | /** |
||
141 | * Updates bounding box based on SHPData. |
||
142 | * |
||
143 | * @param string $type Type of box |
||
144 | * @param array $data ShapeRecord SHPData |
||
145 | */ |
||
146 | 12 | private function updateBBox($type, $data) |
|
158 | |||
159 | /** |
||
160 | * Adds record to shape file. |
||
161 | * |
||
162 | * @param ShapeRecord $record |
||
163 | * |
||
164 | * @return int Number of added record |
||
165 | */ |
||
166 | 12 | public function addRecord($record) |
|
189 | |||
190 | /** |
||
191 | * Deletes record from shapefile. |
||
192 | * |
||
193 | * @param int $index |
||
194 | */ |
||
195 | public function deleteRecord($index) |
||
207 | |||
208 | /** |
||
209 | * Returns array defining fields in DBF file. |
||
210 | * |
||
211 | * @return array see setDBFHeader for more information |
||
212 | */ |
||
213 | public function getDBFHeader() |
||
217 | |||
218 | /** |
||
219 | * Changes array defining fields in DBF file, used in dbase_create call. |
||
220 | * |
||
221 | * @param array $header An array of arrays, each array describing the |
||
222 | * format of one field of the database. Each |
||
223 | * field consists of a name, a character indicating |
||
224 | * the field type, and optionally, a length, |
||
225 | * a precision and a nullable flag. |
||
226 | */ |
||
227 | 12 | public function setDBFHeader($header) |
|
236 | |||
237 | /** |
||
238 | * Lookups value in the DBF file and returs index. |
||
239 | * |
||
240 | * @param string $field Field to match |
||
241 | * @param mixed $value Value to match |
||
242 | * |
||
243 | * @return int |
||
244 | */ |
||
245 | 1 | public function getIndexFromDBFData($field, $value) |
|
257 | |||
258 | /** |
||
259 | * Loads DBF metadata. |
||
260 | */ |
||
261 | private function _loadDBFHeader() |
||
297 | |||
298 | /** |
||
299 | * Deletes record from the DBF file. |
||
300 | * |
||
301 | * @param int $index |
||
302 | */ |
||
303 | private function _deleteRecordFromDBF($index) |
||
309 | |||
310 | /** |
||
311 | * Loads SHP file metadata. |
||
312 | * |
||
313 | * @return bool |
||
314 | */ |
||
315 | 20 | private function _loadHeaders() |
|
349 | |||
350 | /** |
||
351 | * Saves bounding box record, possibly using 0 instead of not set values. |
||
352 | * |
||
353 | * @param file $file File object |
||
354 | * @param string $type Bounding box dimension (eg. xmax, mmin...) |
||
355 | */ |
||
356 | 13 | private function _saveBBoxRecord($file, $type) |
|
362 | |||
363 | /** |
||
364 | * Saves bounding box to a file. |
||
365 | * |
||
366 | * @param file $file File object |
||
367 | */ |
||
368 | 13 | private function _saveBBox($file) |
|
379 | |||
380 | /** |
||
381 | * Saves SHP and SHX file metadata. |
||
382 | */ |
||
383 | 13 | private function _saveHeaders() |
|
397 | |||
398 | /** |
||
399 | * Loads records from SHP file (and DBF). |
||
400 | * |
||
401 | * @return bool |
||
402 | */ |
||
403 | 19 | private function _loadRecords() |
|
423 | |||
424 | /** |
||
425 | * Saves records to SHP and SHX files. |
||
426 | */ |
||
427 | 13 | private function _saveRecords() |
|
442 | |||
443 | /** |
||
444 | * Generic interface to open files. |
||
445 | * |
||
446 | * @param bool $toWrite Whether file should be opened for writing |
||
447 | * @param string $extension File extension |
||
448 | * @param string $name Verbose file name to report errors |
||
449 | * |
||
450 | * @return file|false File handle |
||
451 | */ |
||
452 | 23 | private function _openFile($toWrite, $extension, $name) |
|
464 | |||
465 | /** |
||
466 | * Opens SHP file. |
||
467 | * |
||
468 | * @param bool $toWrite Whether file should be opened for writing |
||
469 | * |
||
470 | * @return bool |
||
471 | */ |
||
472 | 23 | private function _openSHPFile($toWrite = false) |
|
481 | |||
482 | /** |
||
483 | * Closes SHP file. |
||
484 | */ |
||
485 | 21 | private function _closeSHPFile() |
|
492 | |||
493 | /** |
||
494 | * Opens SHX file. |
||
495 | * |
||
496 | * @param bool $toWrite Whether file should be opened for writing |
||
497 | * |
||
498 | * @return bool |
||
499 | */ |
||
500 | 13 | private function _openSHXFile($toWrite = false) |
|
509 | |||
510 | /** |
||
511 | * Closes SHX file. |
||
512 | */ |
||
513 | 13 | private function _closeSHXFile() |
|
520 | |||
521 | /** |
||
522 | * Creates DBF file. |
||
523 | * |
||
524 | * @return bool |
||
525 | */ |
||
526 | 13 | private function _createDBFFile() |
|
550 | |||
551 | /** |
||
552 | * Loads DBF file if supported. |
||
553 | * |
||
554 | * @return bool |
||
555 | */ |
||
556 | 20 | private function _openDBFFile() |
|
579 | |||
580 | /** |
||
581 | * Closes DBF file. |
||
582 | */ |
||
583 | 21 | private function _closeDBFFile() |
|
590 | |||
591 | /** |
||
592 | * Sets error message. |
||
593 | * |
||
594 | * @param string $error |
||
595 | */ |
||
596 | 3 | public function setError($error) |
|
600 | |||
601 | /** |
||
602 | * Reads given number of bytes from SHP file. |
||
603 | * |
||
604 | * @param int $bytes |
||
605 | * |
||
606 | * @return string |
||
607 | */ |
||
608 | 20 | public function readSHP($bytes) |
|
612 | |||
613 | /** |
||
614 | * Checks whether file is at EOF. |
||
615 | * |
||
616 | * @return bool |
||
617 | */ |
||
618 | 19 | public function eofSHP() |
|
622 | |||
623 | /** |
||
624 | * Returns shape name. |
||
625 | * |
||
626 | * @return string |
||
627 | */ |
||
628 | 1 | public function getShapeName() |
|
632 | |||
633 | /** |
||
634 | * Check whether file contains measure data. |
||
635 | * |
||
636 | * For some reason this is distinguished by zero bouding box in the |
||
637 | * specification. |
||
638 | * |
||
639 | * @return bool |
||
640 | */ |
||
641 | 8 | public function hasMeasure() |
|
645 | } |
||
646 |
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.