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 |
||
24 | class ShapeRecord { |
||
25 | private $SHPFile = null; |
||
26 | private $DBFFile = null; |
||
27 | private $ShapeFile = null; |
||
28 | |||
29 | public $recordNumber = null; |
||
30 | private $shapeType = null; |
||
31 | |||
32 | public $lastError = ''; |
||
33 | |||
34 | public $SHPData = array(); |
||
35 | public $DBFData = array(); |
||
36 | |||
37 | static $shape_names = array( |
||
|
|||
38 | 0 => 'Null Shape', |
||
39 | 1 => 'Point', |
||
40 | 3 => 'PolyLine', |
||
41 | 5 => 'Polygon', |
||
42 | 8 => 'MultiPoint', |
||
43 | 11 => 'PointZ', |
||
44 | 13 => 'PolyLineZ', |
||
45 | 15 => 'PolygonZ', |
||
46 | 18 => 'MultiPointZ', |
||
47 | 21 => 'PointM', |
||
48 | 23 => 'PolyLineM', |
||
49 | 25 => 'PolygonM', |
||
50 | 28 => 'MultiPointM', |
||
51 | 31 => 'MultiPatch', |
||
52 | ); |
||
53 | |||
54 | /** |
||
55 | * @param integer $shapeType |
||
56 | */ |
||
57 | public function __construct($shapeType) { |
||
58 | $this->shapeType = $shapeType; |
||
59 | } |
||
60 | |||
61 | public function loadFromFile(&$ShapeFile, &$SHPFile, &$DBFFile) { |
||
62 | $this->ShapeFile = $ShapeFile; |
||
63 | $this->SHPFile = $SHPFile; |
||
64 | $this->DBFFile = $DBFFile; |
||
65 | $this->_loadHeaders(); |
||
66 | |||
67 | switch ($this->shapeType) { |
||
68 | case 0: |
||
69 | $this->_loadNullRecord(); |
||
70 | break; |
||
71 | case 1: |
||
72 | $this->_loadPointRecord(); |
||
73 | break; |
||
74 | case 21: |
||
75 | $this->_loadPointMRecord(); |
||
76 | break; |
||
77 | case 11: |
||
78 | $this->_loadPointZRecord(); |
||
79 | break; |
||
80 | case 3: |
||
81 | $this->_loadPolyLineRecord(); |
||
82 | break; |
||
83 | case 23: |
||
84 | $this->_loadPolyLineMRecord(); |
||
85 | break; |
||
86 | case 13: |
||
87 | $this->_loadPolyLineZRecord(); |
||
88 | break; |
||
89 | case 5: |
||
90 | $this->_loadPolygonRecord(); |
||
91 | break; |
||
92 | case 25: |
||
93 | $this->_loadPolygonMRecord(); |
||
94 | break; |
||
95 | case 15: |
||
96 | $this->_loadPolygonZRecord(); |
||
97 | break; |
||
98 | case 8: |
||
99 | $this->_loadMultiPointRecord(); |
||
100 | break; |
||
101 | case 28: |
||
102 | $this->_loadMultiPointMRecord(); |
||
103 | break; |
||
104 | case 18: |
||
105 | $this->_loadMultiPointZRecord(); |
||
106 | break; |
||
107 | default: |
||
108 | $this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType)); |
||
109 | break; |
||
110 | } |
||
111 | if (ShapeFile::supports_dbase() && isset($this->DBFFile)) { |
||
112 | $this->_loadDBFData(); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | public function saveToFile(&$SHPFile, &$DBFFile, $recordNumber) { |
||
117 | $this->SHPFile = $SHPFile; |
||
118 | $this->DBFFile = $DBFFile; |
||
119 | $this->recordNumber = $recordNumber; |
||
120 | $this->_saveHeaders(); |
||
121 | |||
122 | switch ($this->shapeType) { |
||
123 | case 0: |
||
124 | // Nothing to save |
||
125 | break; |
||
126 | case 1: |
||
127 | $this->_savePointRecord(); |
||
128 | break; |
||
129 | case 21: |
||
130 | $this->_savePointMRecord(); |
||
131 | break; |
||
132 | case 11: |
||
133 | $this->_savePointZRecord(); |
||
134 | break; |
||
135 | case 3: |
||
136 | $this->_savePolyLineRecord(); |
||
137 | break; |
||
138 | case 23: |
||
139 | $this->_savePolyLineMRecord(); |
||
140 | break; |
||
141 | case 13: |
||
142 | $this->_savePolyLineZRecord(); |
||
143 | break; |
||
144 | case 5: |
||
145 | $this->_savePolygonRecord(); |
||
146 | break; |
||
147 | case 25: |
||
148 | $this->_savePolygonMRecord(); |
||
149 | break; |
||
150 | case 15: |
||
151 | $this->_savePolygonZRecord(); |
||
152 | break; |
||
153 | case 8: |
||
154 | $this->_saveMultiPointRecord(); |
||
155 | break; |
||
156 | case 28: |
||
157 | $this->_saveMultiPointMRecord(); |
||
158 | break; |
||
159 | case 18: |
||
160 | $this->_saveMultiPointZRecord(); |
||
161 | break; |
||
162 | default: |
||
163 | $this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType)); |
||
164 | break; |
||
165 | } |
||
166 | $this->_saveDBFData(); |
||
167 | } |
||
168 | |||
169 | public function updateDBFInfo($header) { |
||
170 | $tmp = $this->DBFData; |
||
171 | unset($this->DBFData); |
||
172 | $this->DBFData = array(); |
||
173 | foreach ($header as $value) { |
||
174 | $this->DBFData[$value[0]] = (isset($tmp[$value[0]])) ? $tmp[$value[0]] : ''; |
||
175 | } |
||
176 | } |
||
177 | |||
178 | private function _loadHeaders() { |
||
179 | $this->recordNumber = Util::loadData('N', $this->ShapeFile->readSHP(4)); |
||
180 | // We read the length of the record |
||
181 | Util::loadData('N', $this->ShapeFile->readSHP(4)); |
||
182 | $this->shapeType = Util::loadData('V', $this->ShapeFile->readSHP(4)); |
||
183 | } |
||
184 | |||
185 | private function _saveHeaders() { |
||
186 | fwrite($this->SHPFile, pack('N', $this->recordNumber)); |
||
187 | fwrite($this->SHPFile, pack('N', $this->getContentLength())); |
||
188 | fwrite($this->SHPFile, pack('V', $this->shapeType)); |
||
189 | } |
||
190 | |||
191 | private function _loadPoint() { |
||
192 | $data = array(); |
||
193 | |||
194 | $data['x'] = Util::loadData('d', $this->ShapeFile->readSHP(8)); |
||
195 | $data['y'] = Util::loadData('d', $this->ShapeFile->readSHP(8)); |
||
196 | |||
197 | return $data; |
||
198 | } |
||
199 | |||
200 | private function _loadPointM() { |
||
201 | $data = array(); |
||
202 | |||
203 | $data['x'] = Util::loadData('d', $this->ShapeFile->readSHP(8)); |
||
204 | $data['y'] = Util::loadData('d', $this->ShapeFile->readSHP(8)); |
||
205 | $data['m'] = Util::loadData('d', $this->ShapeFile->readSHP(8)); |
||
206 | |||
207 | return $data; |
||
208 | } |
||
209 | |||
210 | private function _loadPointZ() { |
||
211 | $data = array(); |
||
212 | |||
213 | $data['x'] = Util::loadData('d', $this->ShapeFile->readSHP(8)); |
||
214 | $data['y'] = Util::loadData('d', $this->ShapeFile->readSHP(8)); |
||
215 | $data['z'] = Util::loadData('d', $this->ShapeFile->readSHP(8)); |
||
216 | $data['m'] = Util::loadData('d', $this->ShapeFile->readSHP(8)); |
||
217 | |||
218 | return $data; |
||
219 | } |
||
220 | |||
221 | private function _savePoint($data) { |
||
222 | fwrite($this->SHPFile, Util::packDouble($data['x'])); |
||
223 | fwrite($this->SHPFile, Util::packDouble($data['y'])); |
||
224 | } |
||
225 | |||
226 | private function _savePointM($data) { |
||
227 | fwrite($this->SHPFile, Util::packDouble($data['x'])); |
||
228 | fwrite($this->SHPFile, Util::packDouble($data['y'])); |
||
229 | fwrite($this->SHPFile, Util::packDouble($data['m'])); |
||
230 | } |
||
231 | |||
232 | private function _savePointZ($data) { |
||
233 | fwrite($this->SHPFile, Util::packDouble($data['x'])); |
||
234 | fwrite($this->SHPFile, Util::packDouble($data['y'])); |
||
235 | fwrite($this->SHPFile, Util::packDouble($data['z'])); |
||
236 | fwrite($this->SHPFile, Util::packDouble($data['m'])); |
||
237 | } |
||
238 | |||
239 | private function _loadNullRecord() { |
||
240 | $this->SHPData = array(); |
||
241 | } |
||
242 | |||
243 | private function _loadPointRecord() { |
||
244 | $this->SHPData = $this->_loadPoint(); |
||
245 | } |
||
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', $this->ShapeFile->readSHP(8)); |
||
270 | $this->SHPData['ymin'] = Util::loadData('d', $this->ShapeFile->readSHP(8)); |
||
271 | $this->SHPData['xmax'] = Util::loadData('d', $this->ShapeFile->readSHP(8)); |
||
272 | $this->SHPData['ymax'] = Util::loadData('d', $this->ShapeFile->readSHP(8)); |
||
273 | |||
274 | $this->SHPData['numpoints'] = Util::loadData('V', $this->ShapeFile->readSHP(4)); |
||
275 | |||
276 | View Code Duplication | for ($i = 0; $i <= $this->SHPData['numpoints']; $i++) { |
|
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', $this->ShapeFile->readSHP(8)); |
||
287 | $this->SHPData[$type.'max'] = Util::loadData('d', $this->ShapeFile->readSHP(8)); |
||
288 | |||
289 | View Code Duplication | for ($i = 0; $i <= $this->SHPData['numpoints']; $i++) { |
|
1 ignored issue
–
show
|
|||
290 | $this->SHPData['points'][$i][$type] = Util::loadData('d', $this->ShapeFile->readSHP(8)); |
||
291 | } |
||
292 | } |
||
293 | |||
294 | private function _loadMultiPointMRecord() { |
||
299 | |||
300 | private function _loadMultiPointZRecord() { |
||
306 | |||
307 | private function _saveMultiPointRecord() { |
||
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++) { |
||
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', $this->ShapeFile->readSHP(8)); |
||
345 | $this->SHPData['ymin'] = Util::loadData('d', $this->ShapeFile->readSHP(8)); |
||
346 | $this->SHPData['xmax'] = Util::loadData('d', $this->ShapeFile->readSHP(8)); |
||
347 | $this->SHPData['ymax'] = Util::loadData('d', $this->ShapeFile->readSHP(8)); |
||
348 | |||
349 | $this->SHPData['numparts'] = Util::loadData('V', $this->ShapeFile->readSHP(4)); |
||
350 | $this->SHPData['numpoints'] = Util::loadData('V', $this->ShapeFile->readSHP(4)); |
||
351 | |||
352 | View Code Duplication | for ($i = 0; $i < $this->SHPData['numparts']; $i++) { |
|
353 | $this->SHPData['parts'][$i] = Util::loadData('V', $this->ShapeFile->readSHP(4)); |
||
354 | } |
||
355 | |||
356 | $firstIndex = ftell($this->SHPFile); |
||
357 | $readPoints = 0; |
||
358 | foreach ($this->SHPData['parts'] as $partIndex => $partData) { |
||
371 | |||
372 | /** |
||
373 | * @param string $type |
||
374 | */ |
||
375 | private function _loadPolyLineMZRecord($type) { |
||
391 | |||
392 | private function _loadPolyLineMRecord() { |
||
397 | |||
398 | private function _loadPolyLineZRecord() { |
||
404 | |||
405 | private function _savePolyLineRecord() { |
||
420 | |||
421 | /** |
||
422 | * @param string $type |
||
423 | */ |
||
424 | private function _savePolyLineMZRecord($type) { |
||
433 | |||
434 | private function _savePolyLineMRecord() { |
||
439 | |||
440 | private function _savePolyLineZRecord() { |
||
446 | |||
447 | private function _loadPolygonRecord() { |
||
450 | |||
451 | private function _loadPolygonMRecord() { |
||
454 | |||
455 | private function _loadPolygonZRecord() { |
||
458 | |||
459 | private function _savePolygonRecord() { |
||
462 | |||
463 | private function _savePolygonMRecord() { |
||
466 | |||
467 | private function _savePolygonZRecord() { |
||
470 | |||
471 | private function _adjustBBox($point) { |
||
488 | |||
489 | public function addPoint($point, $partIndex = 0) { |
||
550 | |||
551 | public function deletePoint($pointIndex = 0, $partIndex = 0) { |
||
606 | |||
607 | public function getContentLength() { |
||
663 | |||
664 | private function _loadDBFData() { |
||
668 | |||
669 | private function _saveDBFData() { |
||
681 | |||
682 | /** |
||
683 | * @param string $error |
||
684 | */ |
||
685 | public function setError($error) { |
||
689 | |||
690 | /** |
||
691 | * Returns shape name |
||
692 | * |
||
693 | * @return string |
||
694 | */ |
||
695 | public function getShapeName() |
||
702 | } |
||
703 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.