Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ShapeRecord 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 ShapeRecord, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class ShapeRecord { |
||
| 34 | private $SHPFile = NULL; |
||
| 35 | private $DBFFile = NULL; |
||
| 36 | |||
| 37 | public $recordNumber = NULL; |
||
| 38 | private $shapeType = NULL; |
||
| 39 | |||
| 40 | public $lastError = ""; |
||
| 41 | |||
| 42 | public $SHPData = array(); |
||
| 43 | public $DBFData = array(); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param integer $shapeType |
||
| 47 | */ |
||
| 48 | public function __construct($shapeType) { |
||
| 51 | |||
| 52 | public function loadFromFile(&$SHPFile, &$DBFFile) { |
||
| 103 | |||
| 104 | public function saveToFile(&$SHPFile, &$DBFFile, $recordNumber) { |
||
| 156 | |||
| 157 | public function updateDBFInfo($header) { |
||
| 166 | |||
| 167 | private function _loadHeaders() { |
||
| 172 | |||
| 173 | private function _saveHeaders() { |
||
| 178 | |||
| 179 | private function _loadPoint() { |
||
| 187 | |||
| 188 | private function _loadPointM() { |
||
| 197 | |||
| 198 | private function _loadPointZ() { |
||
| 208 | |||
| 209 | private function _savePoint($data) { |
||
| 213 | |||
| 214 | private function _savePointM($data) { |
||
| 219 | |||
| 220 | private function _savePointZ($data) { |
||
| 226 | |||
| 227 | private function _saveMeasure($data) { |
||
| 230 | |||
| 231 | private function _saveZCoordinate($data) { |
||
| 234 | |||
| 235 | private function _loadNullRecord() { |
||
| 238 | |||
| 239 | private function _saveNullRecord() { |
||
| 242 | |||
| 243 | private function _loadPointRecord() { |
||
| 246 | |||
| 247 | private function _loadPointMRecord() { |
||
| 250 | |||
| 251 | private function _loadPointZRecord() { |
||
| 254 | |||
| 255 | private function _savePointRecord() { |
||
| 258 | |||
| 259 | private function _savePointMRecord() { |
||
| 262 | |||
| 263 | private function _savePointZRecord() { |
||
| 266 | |||
| 267 | private function _loadMultiPointRecord() { |
||
| 268 | $this->SHPData = array(); |
||
| 269 | $this->SHPData["xmin"] = Util::loadData("d", fread($this->SHPFile, 8)); |
||
| 270 | $this->SHPData["ymin"] = Util::loadData("d", fread($this->SHPFile, 8)); |
||
| 271 | $this->SHPData["xmax"] = Util::loadData("d", fread($this->SHPFile, 8)); |
||
| 272 | $this->SHPData["ymax"] = Util::loadData("d", fread($this->SHPFile, 8)); |
||
| 273 | |||
| 274 | $this->SHPData["numpoints"] = Util::loadData("V", fread($this->SHPFile, 4)); |
||
| 275 | |||
| 276 | for ($i = 0; $i <= $this->SHPData["numpoints"]; $i++) { |
||
|
1 ignored issue
–
show
|
|||
| 277 | $this->SHPData["points"][] = $this->_loadPoint(); |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param string $type |
||
| 283 | */ |
||
| 284 | private function _loadMultiPointMZRecord($type) { |
||
| 285 | |||
| 286 | $this->SHPData[$type."min"] = Util::loadData("d", fread($this->SHPFile, 8)); |
||
| 287 | $this->SHPData[$type."max"] = Util::loadData("d", fread($this->SHPFile, 8)); |
||
| 288 | |||
| 289 | for ($i = 0; $i <= $this->SHPData["numpoints"]; $i++) { |
||
| 290 | $this->SHPData["points"][$i][$type] = Util::loadData("d", fread($this->SHPFile, 8)); |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | private function _loadMultiPointMRecord() { |
||
| 299 | |||
| 300 | private function _loadMultiPointZRecord() { |
||
| 306 | |||
| 307 | private function _saveMultiPointRecord() { |
||
| 308 | fwrite($this->SHPFile, pack("dddd", $this->SHPData["xmin"], $this->SHPData["ymin"], $this->SHPData["xmax"], $this->SHPData["ymax"])); |
||
| 309 | |||
| 310 | fwrite($this->SHPFile, pack("V", $this->SHPData["numpoints"])); |
||
| 311 | |||
| 312 | for ($i = 0; $i <= $this->SHPData["numpoints"]; $i++) { |
||
|
1 ignored issue
–
show
|
|||
| 313 | $this->_savePoint($this->SHPData["points"][$i]); |
||
| 314 | } |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @param string $type |
||
| 319 | */ |
||
| 320 | private function _saveMultiPointMZRecord($type) { |
||
| 321 | |||
| 322 | fwrite($this->SHPFile, pack("dd", $this->SHPData[$type."min"], $this->SHPData[$type."max"])); |
||
| 323 | |||
| 324 | for ($i = 0; $i <= $this->SHPData["numpoints"]; $i++) { |
||
|
1 ignored issue
–
show
|
|||
| 325 | fwrite($this->SHPFile, Util::packDouble($this->SHPData["points"][$type])); |
||
| 326 | } |
||
| 327 | } |
||
| 328 | |||
| 329 | private function _saveMultiPointMRecord() { |
||
| 334 | |||
| 335 | private function _saveMultiPointZRecord() { |
||
| 341 | |||
| 342 | private function _loadPolyLineRecord() { |
||
| 343 | $this->SHPData = array(); |
||
| 344 | $this->SHPData["xmin"] = Util::loadData("d", fread($this->SHPFile, 8)); |
||
| 345 | $this->SHPData["ymin"] = Util::loadData("d", fread($this->SHPFile, 8)); |
||
| 346 | $this->SHPData["xmax"] = Util::loadData("d", fread($this->SHPFile, 8)); |
||
| 347 | $this->SHPData["ymax"] = Util::loadData("d", fread($this->SHPFile, 8)); |
||
| 348 | |||
| 349 | $this->SHPData["numparts"] = Util::loadData("V", fread($this->SHPFile, 4)); |
||
| 350 | $this->SHPData["numpoints"] = Util::loadData("V", fread($this->SHPFile, 4)); |
||
| 351 | |||
| 352 | for ($i = 0; $i < $this->SHPData["numparts"]; $i++) { |
||
|
1 ignored issue
–
show
|
|||
| 353 | $this->SHPData["parts"][$i] = Util::loadData("V", fread($this->SHPFile, 4)); |
||
| 354 | } |
||
| 355 | |||
| 356 | $firstIndex = ftell($this->SHPFile); |
||
| 357 | $readPoints = 0; |
||
| 358 | reset($this->SHPData["parts"]); |
||
| 359 | while (list($partIndex, $partData) = each($this->SHPData["parts"])) { |
||
| 360 | if (!isset($this->SHPData["parts"][$partIndex]["points"]) || !is_array($this->SHPData["parts"][$partIndex]["points"])) { |
||
| 361 | $this->SHPData["parts"][$partIndex] = array(); |
||
| 362 | $this->SHPData["parts"][$partIndex]["points"] = array(); |
||
| 363 | } |
||
| 364 | while (!in_array($readPoints, $this->SHPData["parts"]) && ($readPoints < ($this->SHPData["numpoints"])) && !feof($this->SHPFile)) { |
||
| 365 | $this->SHPData["parts"][$partIndex]["points"][] = $this->_loadPoint(); |
||
| 366 | $readPoints++; |
||
| 367 | } |
||
| 368 | } |
||
| 369 | |||
| 370 | fseek($this->SHPFile, $firstIndex + ($readPoints * 16)); |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param string $type |
||
| 375 | */ |
||
| 376 | private function _loadPolyLineMZRecord($type) { |
||
| 393 | |||
| 394 | private function _loadPolyLineMRecord() { |
||
| 399 | |||
| 400 | private function _loadPolyLineZRecord() { |
||
| 406 | |||
| 407 | private function _savePolyLineRecord() { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @param string $type |
||
| 426 | */ |
||
| 427 | private function _savePolyLineMZRecord($type) { |
||
| 437 | |||
| 438 | private function _savePolyLineMRecord() { |
||
| 443 | |||
| 444 | private function _savePolyLineZRecord() { |
||
| 450 | |||
| 451 | private function _loadPolygonRecord() { |
||
| 454 | |||
| 455 | private function _loadPolygonMRecord() { |
||
| 458 | |||
| 459 | private function _loadPolygonZRecord() { |
||
| 462 | |||
| 463 | private function _savePolygonRecord() { |
||
| 466 | |||
| 467 | private function _savePolygonMRecord() { |
||
| 470 | |||
| 471 | private function _savePolygonZRecord() { |
||
| 474 | |||
| 475 | private function _adjustBBox($point) { |
||
| 476 | // Adjusts bounding box based on point |
||
| 477 | $directions = array('x', 'y', 'z', 'm'); |
||
| 478 | foreach ($directions as $direction) { |
||
| 479 | if (!isset($point[$direction])) { |
||
| 492 | |||
| 493 | public function addPoint($point, $partIndex = 0) { |
||
| 554 | |||
| 555 | public function deletePoint($pointIndex = 0, $partIndex = 0) { |
||
| 608 | |||
| 609 | public function getContentLength() { |
||
| 662 | |||
| 663 | private function _loadDBFData() { |
||
| 667 | |||
| 668 | private function _saveDBFData() { |
||
| 680 | |||
| 681 | /** |
||
| 682 | * @param string $error |
||
| 683 | */ |
||
| 684 | public function setError($error) { |
||
| 688 | } |
||
| 689 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: