Complex classes like GraphPrinter 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 GraphPrinter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class GraphPrinter extends SMWResultPrinter { |
||
| 24 | |||
| 25 | const NODELABEL_DISPLAYTITLE = 'displaytitle'; |
||
| 26 | |||
| 27 | public static $NODE_LABELS = [ |
||
| 28 | self::NODELABEL_DISPLAYTITLE, |
||
| 29 | ]; |
||
| 30 | |||
| 31 | public static $NODE_SHAPES = [ |
||
| 32 | 'box', |
||
| 33 | 'box3d', |
||
| 34 | 'circle', |
||
| 35 | 'component', |
||
| 36 | 'diamond', |
||
| 37 | 'doublecircle', |
||
| 38 | 'doubleoctagon', |
||
| 39 | 'egg', |
||
| 40 | 'ellipse', |
||
| 41 | 'folder', |
||
| 42 | 'hexagon', |
||
| 43 | 'house', |
||
| 44 | 'invhouse', |
||
| 45 | 'invtrapezium', |
||
| 46 | 'invtriangle', |
||
| 47 | 'Mcircle', |
||
| 48 | 'Mdiamond', |
||
| 49 | 'Msquare', |
||
| 50 | 'none', |
||
| 51 | 'note', |
||
| 52 | 'octagon', |
||
| 53 | 'parallelogram', |
||
| 54 | 'pentagon ', |
||
| 55 | 'plaintext', |
||
| 56 | 'point', |
||
| 57 | 'polygon', |
||
| 58 | 'rect', |
||
| 59 | 'rectangle', |
||
| 60 | 'septagon', |
||
| 61 | 'square', |
||
| 62 | 'tab', |
||
| 63 | 'trapezium', |
||
| 64 | 'triangle', |
||
| 65 | 'tripleoctagon', |
||
| 66 | ]; |
||
| 67 | |||
| 68 | protected $m_graphName; |
||
| 69 | protected $m_graphLabel; |
||
| 70 | protected $m_graphColor; |
||
| 71 | protected $m_graphLegend; |
||
| 72 | protected $m_graphLink; |
||
| 73 | protected $m_rankdir; |
||
| 74 | protected $m_graphSize; |
||
| 75 | protected $m_labelArray = []; |
||
| 76 | protected $m_graphColors = [ |
||
| 77 | 'black', |
||
| 78 | 'red', |
||
| 79 | 'green', |
||
| 80 | 'blue', |
||
| 81 | 'darkviolet', |
||
| 82 | 'gold', |
||
| 83 | 'deeppink', |
||
| 84 | 'brown', |
||
| 85 | 'bisque', |
||
| 86 | 'darkgreen', |
||
| 87 | 'yellow', |
||
| 88 | 'darkblue', |
||
| 89 | 'magenta', |
||
| 90 | 'steelblue2' ]; |
||
| 91 | protected $m_nameProperty; |
||
| 92 | protected $m_nodeShape; |
||
| 93 | protected $m_parentRelation; |
||
| 94 | protected $m_wordWrapLimit; |
||
| 95 | protected $m_nodeLabel; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * (non-PHPdoc) |
||
| 99 | * @see SMWResultPrinter::handleParameters() |
||
| 100 | */ |
||
| 101 | protected function handleParameters( array $params, $outputmode ) { |
||
| 124 | |||
| 125 | protected function getResultText( SMWQueryResult $res, $outputmode ) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Returns the GV for a single subject. |
||
| 178 | * |
||
| 179 | * @since 1.5.4 |
||
| 180 | * |
||
| 181 | * @param array $row |
||
| 182 | * @param $outputmode |
||
| 183 | * |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | protected function getGVForItem( array /* of SMWResultArray */ |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Returns the GV for a single SMWDataValue. |
||
| 213 | * |
||
| 214 | * @since 1.5.4 |
||
| 215 | * |
||
| 216 | * @param SMWDataValue $object |
||
| 217 | * @param $outputmode |
||
| 218 | * @param boolean $isName Is this the name that should be used for the node? |
||
| 219 | * @param string $name |
||
| 220 | * @param string $labelName |
||
| 221 | * |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | protected function getGVForDataValue( SMWDataValue $object, $outputmode, $isName, $name, $labelName ) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Returns the word wrapped version of the provided text. |
||
| 285 | * |
||
| 286 | * @since 1.5.4 |
||
| 287 | * |
||
| 288 | * @param string $text |
||
| 289 | * @param integer $charLimit |
||
| 290 | * |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | protected function getWordWrappedText( $text, $charLimit ) { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * (non-PHPdoc) |
||
| 322 | * @see SMWResultPrinter::getName() |
||
| 323 | */ |
||
| 324 | public function getName() { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @see SMWResultPrinter::getParamDefinitions |
||
| 330 | * |
||
| 331 | * @since 1.8 |
||
| 332 | * |
||
| 333 | * @param $definitions array of IParamDefinition |
||
| 334 | * |
||
| 335 | * @return array of IParamDefinition|array |
||
| 336 | */ |
||
| 337 | public function getParamDefinitions( array $definitions ) { |
||
| 418 | |||
| 419 | } |
||
| 420 |
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: