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 Plot 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 Plot, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Plot |
||
10 | { |
||
11 | public $numpoints = 0; |
||
12 | public $value; |
||
13 | public $legend = ''; |
||
14 | public $coords = array(); |
||
15 | public $color = 'black'; |
||
16 | public $hidelegend = false; |
||
17 | public $line_weight = 1; |
||
18 | public $csimtargets = array(); |
||
19 | public $csimwintargets = array(); // Array of targets for CSIM |
||
20 | public $csimareas = ''; // Resultant CSIM area tags |
||
21 | public $csimalts = null; // ALT:s for corresponding target |
||
22 | public $legendcsimtarget = ''; |
||
23 | public $legendcsimwintarget = ''; |
||
24 | public $legendcsimalt = ''; |
||
25 | protected $weight = 1; |
||
26 | protected $center = false; |
||
27 | |||
28 | protected $inputValues; |
||
29 | protected $isRunningClear = false; |
||
30 | |||
31 | public function __construct($aDatay, $aDatax = false) |
||
56 | |||
57 | // Stroke the plot |
||
58 | // "virtual" function which must be implemented by |
||
59 | // the subclasses |
||
60 | public function Stroke($aImg, $aXScale, $aYScale) |
||
64 | |||
65 | public function HideLegend($f = true) |
||
69 | |||
70 | public function DoLegend($graph) |
||
76 | |||
77 | public function StrokeDataValue($img, $aVal, $x, $y) |
||
81 | |||
82 | // Set href targets for CSIM |
||
83 | public function SetCSIMTargets($aTargets, $aAlts = '', $aWinTargets = '') |
||
89 | |||
90 | // Get all created areas |
||
91 | public function GetCSIMareas() |
||
95 | |||
96 | // "Virtual" function which gets called before any scale |
||
97 | // or axis are stroked used to do any plot specific adjustment |
||
98 | public function PreStrokeAdjust($aGraph) |
||
105 | |||
106 | // Virtual function to the the concrete plot class to make any changes to the graph |
||
107 | // and scale before the stroke process begins |
||
108 | public function PreScaleSetup($aGraph) |
||
112 | |||
113 | // Get minimum values in plot |
||
114 | View Code Duplication | public function Min() |
|
144 | |||
145 | // Get maximum value in plot |
||
146 | View Code Duplication | public function Max() |
|
177 | |||
178 | public function SetColor($aColor) |
||
182 | |||
183 | View Code Duplication | public function SetLegend($aLegend, $aCSIM = '', $aCSIMAlt = '', $aCSIMWinTarget = '') |
|
190 | |||
191 | public function SetWeight($aWeight) |
||
195 | |||
196 | public function SetLineWeight($aWeight = 1) |
||
200 | |||
201 | public function SetCenter($aCenter = true) |
||
205 | |||
206 | // This method gets called by Graph class to plot anything that should go |
||
207 | // into the margin after the margin color has been set. |
||
208 | public function StrokeMargin($aImg) |
||
212 | |||
213 | // Framework function the chance for each plot class to set a legend |
||
214 | public function Legend($aGraph) |
||
220 | |||
221 | public function Clear() |
||
227 | } // Class |
||
228 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..