Complex classes like SRFGraph 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 SRFGraph, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class SRFGraph extends SMWResultPrinter { |
||
| 17 | |||
| 18 | const NODELABEL_DISPLAYTITLE = 'displaytitle'; |
||
| 19 | |||
| 20 | public static $NODE_LABELS = [ |
||
| 21 | self::NODELABEL_DISPLAYTITLE, |
||
| 22 | ]; |
||
| 23 | |||
| 24 | public static $NODE_SHAPES = [ |
||
| 25 | 'box', |
||
| 26 | 'box3d', |
||
| 27 | 'circle', |
||
| 28 | 'component', |
||
| 29 | 'diamond', |
||
| 30 | 'doublecircle', |
||
| 31 | 'doubleoctagon', |
||
| 32 | 'egg', |
||
| 33 | 'ellipse', |
||
| 34 | 'folder', |
||
| 35 | 'hexagon', |
||
| 36 | 'house', |
||
| 37 | 'invhouse', |
||
| 38 | 'invtrapezium', |
||
| 39 | 'invtriangle', |
||
| 40 | 'Mcircle', |
||
| 41 | 'Mdiamond', |
||
| 42 | 'Msquare', |
||
| 43 | 'none', |
||
| 44 | 'note', |
||
| 45 | 'octagon', |
||
| 46 | 'parallelogram', |
||
| 47 | 'pentagon ', |
||
| 48 | 'plaintext', |
||
| 49 | 'point', |
||
| 50 | 'polygon', |
||
| 51 | 'rect', |
||
| 52 | 'rectangle', |
||
| 53 | 'septagon', |
||
| 54 | 'square', |
||
| 55 | 'tab', |
||
| 56 | 'trapezium', |
||
| 57 | 'triangle', |
||
| 58 | 'tripleoctagon', |
||
| 59 | ]; |
||
| 60 | |||
| 61 | protected $m_graphName; |
||
| 62 | protected $m_graphLabel; |
||
| 63 | protected $m_graphColor; |
||
| 64 | protected $m_graphLegend; |
||
| 65 | protected $m_graphLink; |
||
| 66 | protected $m_rankdir; |
||
| 67 | protected $m_graphSize; |
||
| 68 | protected $m_labelArray = []; |
||
| 69 | protected $m_graphColors = [ |
||
| 70 | 'black', |
||
| 71 | 'red', |
||
| 72 | 'green', |
||
| 73 | 'blue', |
||
| 74 | 'darkviolet', |
||
| 75 | 'gold', |
||
| 76 | 'deeppink', |
||
| 77 | 'brown', |
||
| 78 | 'bisque', |
||
| 79 | 'darkgreen', |
||
| 80 | 'yellow', |
||
| 81 | 'darkblue', |
||
| 82 | 'magenta', |
||
| 83 | 'steelblue2' ]; |
||
| 84 | protected $m_nameProperty; |
||
| 85 | protected $m_nodeShape; |
||
| 86 | protected $m_parentRelation; |
||
| 87 | protected $m_wordWrapLimit; |
||
| 88 | protected $m_nodeLabel; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * (non-PHPdoc) |
||
| 92 | * @see SMWResultPrinter::handleParameters() |
||
| 93 | */ |
||
| 94 | protected function handleParameters( array $params, $outputmode ) { |
||
| 117 | |||
| 118 | protected function getResultText( SMWQueryResult $res, $outputmode ) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Returns the GV for a single subject. |
||
| 171 | * |
||
| 172 | * @since 1.5.4 |
||
| 173 | * |
||
| 174 | * @param array $row |
||
| 175 | * @param $outputmode |
||
| 176 | * |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | protected function getGVForItem( array /* of SMWResultArray */ |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Returns the GV for a single SMWDataValue. |
||
| 206 | * |
||
| 207 | * @since 1.5.4 |
||
| 208 | * |
||
| 209 | * @param SMWDataValue $object |
||
| 210 | * @param $outputmode |
||
| 211 | * @param boolean $isName Is this the name that should be used for the node? |
||
| 212 | * @param string $name |
||
| 213 | * @param string $labelName |
||
| 214 | * |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | protected function getGVForDataValue( SMWDataValue $object, $outputmode, $isName, $name, $labelName ) { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Returns the word wrapped version of the provided text. |
||
| 278 | * |
||
| 279 | * @since 1.5.4 |
||
| 280 | * |
||
| 281 | * @param string $text |
||
| 282 | * @param integer $charLimit |
||
| 283 | * |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | protected function getWordWrappedText( $text, $charLimit ) { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * (non-PHPdoc) |
||
| 315 | * @see SMWResultPrinter::getName() |
||
| 316 | */ |
||
| 317 | public function getName() { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @see SMWResultPrinter::getParamDefinitions |
||
| 323 | * |
||
| 324 | * @since 1.8 |
||
| 325 | * |
||
| 326 | * @param $definitions array of IParamDefinition |
||
| 327 | * |
||
| 328 | * @return array of IParamDefinition|array |
||
| 329 | */ |
||
| 330 | public function getParamDefinitions( array $definitions ) { |
||
| 411 | |||
| 412 | } |
||
| 413 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: