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 | public static $NODE_SHAPES = [ |
||
| 19 | 'box', |
||
| 20 | 'box3d', |
||
| 21 | 'circle', |
||
| 22 | 'component', |
||
| 23 | 'diamond', |
||
| 24 | 'doublecircle', |
||
| 25 | 'doubleoctagon', |
||
| 26 | 'egg', |
||
| 27 | 'ellipse', |
||
| 28 | 'folder', |
||
| 29 | 'hexagon', |
||
| 30 | 'house', |
||
| 31 | 'invhouse', |
||
| 32 | 'invtrapezium', |
||
| 33 | 'invtriangle', |
||
| 34 | 'Mcircle', |
||
| 35 | 'Mdiamond', |
||
| 36 | 'Msquare', |
||
| 37 | 'none', |
||
| 38 | 'note', |
||
| 39 | 'octagon', |
||
| 40 | 'parallelogram', |
||
| 41 | 'pentagon ', |
||
| 42 | 'plaintext', |
||
| 43 | 'point', |
||
| 44 | 'polygon', |
||
| 45 | 'rect', |
||
| 46 | 'rectangle', |
||
| 47 | 'septagon', |
||
| 48 | 'square', |
||
| 49 | 'tab', |
||
| 50 | 'trapezium', |
||
| 51 | 'triangle', |
||
| 52 | 'tripleoctagon', |
||
| 53 | ]; |
||
| 54 | |||
| 55 | protected $m_graphName; |
||
| 56 | protected $m_graphLabel; |
||
| 57 | protected $m_graphColor; |
||
| 58 | protected $m_graphLegend; |
||
| 59 | protected $m_graphLink; |
||
| 60 | protected $m_rankdir; |
||
| 61 | protected $m_graphSize; |
||
| 62 | protected $m_labelArray = []; |
||
| 63 | protected $m_graphColors = [ |
||
| 64 | 'black', |
||
| 65 | 'red', |
||
| 66 | 'green', |
||
| 67 | 'blue', |
||
| 68 | 'darkviolet', |
||
| 69 | 'gold', |
||
| 70 | 'deeppink', |
||
| 71 | 'brown', |
||
| 72 | 'bisque', |
||
| 73 | 'darkgreen', |
||
| 74 | 'yellow', |
||
| 75 | 'darkblue', |
||
| 76 | 'magenta', |
||
| 77 | 'steelblue2' ]; |
||
| 78 | protected $m_nameProperty; |
||
| 79 | protected $m_nodeShape; |
||
| 80 | protected $m_parentRelation; |
||
| 81 | protected $m_wordWrapLimit; |
||
| 82 | protected $m_nodeLabel; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * (non-PHPdoc) |
||
| 86 | * @see SMWResultPrinter::handleParameters() |
||
| 87 | */ |
||
| 88 | protected function handleParameters( array $params, $outputmode ) { |
||
| 111 | |||
| 112 | protected function getResultText( SMWQueryResult $res, $outputmode ) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Returns the GV for a single subject. |
||
| 165 | * |
||
| 166 | * @since 1.5.4 |
||
| 167 | * |
||
| 168 | * @param array $row |
||
| 169 | * @param $outputmode |
||
| 170 | * |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | protected function getGVForItem( array /* of SMWResultArray */ |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Returns the GV for a single SMWDataValue. |
||
| 200 | * |
||
| 201 | * @since 1.5.4 |
||
| 202 | * |
||
| 203 | * @param SMWDataValue $object |
||
| 204 | * @param $outputmode |
||
| 205 | * @param boolean $isName Is this the name that should be used for the node? |
||
| 206 | * @param string $name |
||
| 207 | * @param string $labelName |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | protected function getGVForDataValue( SMWDataValue $object, $outputmode, $isName, $name, $labelName ) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Returns the word wrapped version of the provided text. |
||
| 272 | * |
||
| 273 | * @since 1.5.4 |
||
| 274 | * |
||
| 275 | * @param string $text |
||
| 276 | * @param integer $charLimit |
||
| 277 | * |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | protected function getWordWrappedText( $text, $charLimit ) { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * (non-PHPdoc) |
||
| 309 | * @see SMWResultPrinter::getName() |
||
| 310 | */ |
||
| 311 | public function getName() { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @see SMWResultPrinter::getParamDefinitions |
||
| 317 | * |
||
| 318 | * @since 1.8 |
||
| 319 | * |
||
| 320 | * @param $definitions array of IParamDefinition |
||
| 321 | * |
||
| 322 | * @return array of IParamDefinition|array |
||
| 323 | */ |
||
| 324 | public function getParamDefinitions( array $definitions ) { |
||
| 405 | |||
| 406 | } |
||
| 407 |
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: