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 Contour 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 Contour, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Contour |
||
27 | { |
||
28 | private $dataPoints = array(); |
||
29 | private $nbrCols = 0; |
||
30 | private $nbrRows = 0; |
||
31 | private $horizEdges = array(); |
||
|
|||
32 | private $vertEdges = array(); |
||
33 | private $isobarValues = array(); |
||
34 | private $stack = null; |
||
35 | private $isobarCoord = array(); |
||
36 | private $nbrIsobars = 10; |
||
37 | private $isobarColors = array(); |
||
38 | private $invert = true; |
||
39 | private $highcontrast = false; |
||
40 | private $highcontrastbw = false; |
||
41 | |||
42 | /** |
||
43 | * Create a new contour level "algorithm machine". |
||
44 | * @param $aMatrix The values to find the contour from |
||
45 | * @param $aIsobars Mixed. If integer it determines the number of isobars to be used. The levels are determined |
||
46 | * automatically as equdistance between the min and max value of the matrice. |
||
47 | * If $aIsobars is an array then this is interpretated as an array of values to be used as isobars in the |
||
48 | * contour plot. |
||
49 | * @return an instance of the contour algorithm |
||
50 | */ |
||
51 | public function __construct($aMatrix, $aIsobars = 10, $aColors = null) |
||
87 | |||
88 | /** |
||
89 | * Flip the plot around the Y-coordinate. This has the same affect as flipping the input |
||
90 | * data matrice |
||
91 | * |
||
92 | * @param $aFlg If true the the vertice in input data matrice position (0,0) corresponds to the top left |
||
93 | * corner of teh plot otherwise it will correspond to the bottom left corner (a horizontal flip) |
||
94 | */ |
||
95 | public function SetInvert($aFlg = true) |
||
99 | |||
100 | /** |
||
101 | * Find the min and max values in the data matrice |
||
102 | * |
||
103 | * @return array(min_value,max_value) |
||
104 | */ |
||
105 | public function getMinMaxVal() |
||
120 | |||
121 | /** |
||
122 | * Reset the two matrices that keeps track on where the isobars crosses the |
||
123 | * horizontal and vertical edges |
||
124 | */ |
||
125 | public function resetEdgeMatrices() |
||
135 | |||
136 | /** |
||
137 | * Determine if the specified isobar crosses the horizontal edge specified by its row and column |
||
138 | * |
||
139 | * @param $aRow Row index of edge to be checked |
||
140 | * @param $aCol Col index of edge to be checked |
||
141 | * @param $aIsobar Isobar value |
||
142 | * @return true if the isobar is crossing this edge |
||
143 | */ |
||
144 | View Code Duplication | public function isobarHCrossing($aRow, $aCol, $aIsobar) |
|
160 | |||
161 | /** |
||
162 | * Determine if the specified isobar crosses the vertical edge specified by its row and column |
||
163 | * |
||
164 | * @param $aRow Row index of edge to be checked |
||
165 | * @param $aCol Col index of edge to be checked |
||
166 | * @param $aIsobar Isobar value |
||
167 | * @return true if the isobar is crossing this edge |
||
168 | */ |
||
169 | View Code Duplication | public function isobarVCrossing($aRow, $aCol, $aIsobar) |
|
185 | |||
186 | /** |
||
187 | * Determine all edges, horizontal and vertical that the specified isobar crosses. The crossings |
||
188 | * are recorded in the two edge matrices. |
||
189 | * |
||
190 | * @param $aIsobar The value of the isobar to be checked |
||
191 | */ |
||
192 | public function determineIsobarEdgeCrossings($aIsobar) |
||
211 | |||
212 | /** |
||
213 | * Return the normalized coordinates for the crossing of the specified edge with the specified |
||
214 | * isobar- The crossing is simpy detrmined with a linear interpolation between the two vertices |
||
215 | * on each side of the edge and the value of the isobar |
||
216 | * |
||
217 | * @param $aRow Row of edge |
||
218 | * @param $aCol Column of edge |
||
219 | * @param $aEdgeDir Determine if this is a horizontal or vertical edge |
||
220 | * @param $ib The isobar value |
||
221 | * @return unknown_type |
||
222 | */ |
||
223 | public function getCrossingCoord($aRow, $aCol, $aEdgeDir, $aIsobarVal) |
||
250 | |||
251 | /** |
||
252 | * In order to avoid all kinds of unpleasent extra checks and complex boundary |
||
253 | * controls for the degenerated case where the contour levels exactly crosses |
||
254 | * one of the vertices we add a very small delta (0.1%) to the data point value. |
||
255 | * This has no visible affect but it makes the code sooooo much cleaner. |
||
256 | * |
||
257 | */ |
||
258 | public function adjustDataPointValues() |
||
272 | |||
273 | /** |
||
274 | * @param $aFlg |
||
275 | * @param $aBW |
||
276 | * @return unknown_type |
||
277 | */ |
||
278 | public function UseHighContrastColor($aFlg = true, $aBW = false) |
||
283 | |||
284 | /** |
||
285 | * Calculate suitable colors for each defined isobar |
||
286 | * |
||
287 | */ |
||
288 | public function CalculateColors() |
||
312 | |||
313 | /** |
||
314 | * This is where the main work is done. For each isobar the crossing of the edges are determined |
||
315 | * and then each cell is analyzed to find the 0, 2 or 4 crossings. Then the normalized coordinate |
||
316 | * for the crossings are determined and pushed on to the isobar stack. When the method is finished |
||
317 | * the $isobarCoord will hold one arrayfor each isobar where all the line segments that makes |
||
318 | * up the contour plot are stored. |
||
319 | * |
||
320 | * @return array( $isobarCoord, $isobarValues, $isobarColors ) |
||
321 | */ |
||
322 | public function getIsobars() |
||
406 | } |
||
407 | |||
409 |
This check marks private properties in classes that are never used. Those properties can be removed.