Conditions | 18 |
Paths | 640 |
Total Lines | 98 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
67 | public function buildGraph($nodes){ |
||
68 | $this->add( "digraph " . $this->options->graphName . " {" ); |
||
69 | |||
70 | // set fontsize and fontname of graph, nodes and edges |
||
71 | $this->add( "graph [fontsize=10, fontname=\"Verdana\"]\n" ); |
||
72 | $this->add( "node [fontsize=10, fontname=\"Verdana\"];\n" ); |
||
73 | $this->add( "edge [fontsize=10, fontname=\"Verdana\"];\n" ); |
||
74 | |||
75 | // choose graphsize, nodeshapes and rank direction |
||
76 | if ( $this->options->graphSize != '' ) { |
||
77 | $this->add("size=\"" . $this->options->graphSize . "\";"); |
||
78 | } |
||
79 | |||
80 | if ( $this->options->nodeShape != '' ) { |
||
81 | $this->add( "node [shape=" . $this->options->nodeShape . "];" ); |
||
82 | } |
||
83 | |||
84 | $this->add( "rankdir=" . $this->options->rankDir . ";" ); |
||
85 | |||
86 | /** @var \SRF\GraphNode $node */ |
||
87 | foreach ( $nodes as $node ) { |
||
88 | |||
89 | // take "displaytitle" as node-label if it is set |
||
90 | if ( $this->options->nodeLabel === GraphPrinter::NODELABEL_DISPLAYTITLE) { |
||
91 | $objectDisplayTitle = $node->getLabel(); |
||
92 | if ( !empty( $objectDisplayTitle )) { |
||
93 | $nodeLabel = $this->getWordWrappedText( $objectDisplayTitle, |
||
94 | $this->options->wordWrapLimit ); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Add nodes to the graph |
||
100 | * |
||
101 | * @var \SRF\Graph\GraphNode $node |
||
102 | */ |
||
103 | $this->add( "\"" . $node->getID() . "\"" ); |
||
104 | |||
105 | if ( $this->options->enableGraphLink ) { |
||
106 | |||
107 | $nodeLinkURL = "[[" . $node->getID() . "]]"; |
||
108 | |||
109 | if( $nodeLabel === '' ) { |
||
110 | $this->add( " [URL = \"$nodeLinkURL\"]" ); |
||
111 | } else { |
||
112 | $this->add( " [URL = \"$nodeLinkURL\", label = \"$nodeLabel\"]" ); |
||
|
|||
113 | } |
||
114 | } |
||
115 | $this->add( "; "); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Add edges to the graph |
||
120 | * |
||
121 | * @var \SRF\Graph\GraphNode $node |
||
122 | */ |
||
123 | foreach ( $nodes as $node ) { |
||
124 | |||
125 | if ( count( $node->getParentNode() ) > 0 ) { |
||
126 | |||
127 | foreach ( $node->getParentNode() as $parentNode ) { |
||
128 | |||
129 | // handle parent/child switch (parentRelation) |
||
130 | $this->add( $this->options->parentRelation ? " \"" . $parentNode['object'] |
||
131 | . "\" -> \"" . $node->getID() . "\"" |
||
132 | : " \"" . $node->getID() . "\" -> \"" . $parentNode['object'] . "\" " ); |
||
133 | |||
134 | if ( $this->options->showGraphLabel || $this->options->showGraphColor ) { |
||
135 | $this->add( ' [' ); |
||
136 | |||
137 | // add legend item only if missing |
||
138 | if ( array_search( $parentNode['predicate'], $this->legendItem, true ) === false ) { |
||
139 | $this->legendItem[] = $parentNode['predicate']; |
||
140 | } |
||
141 | |||
142 | // assign color |
||
143 | $color = $this->graphColors[array_search( $parentNode['predicate'], $this->legendItem, true )]; |
||
144 | |||
145 | // show arrow label (graphLabel is misleading but kept for compatibility reasons) |
||
146 | if ( $this->options->showGraphLabel ) { |
||
147 | $this->add( "label=\"" . $parentNode['predicate'] . "\"" ); |
||
148 | if ( $this->options->showGraphColor ) { |
||
149 | $this->add( ",fontcolor=$color," ); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | // colorize arrow |
||
154 | if ( $this->options->showGraphColor ) { |
||
155 | $this->add( "color=$color" ); |
||
156 | } |
||
157 | $this->add( "]" ); |
||
158 | } |
||
159 | $this->add( ";" ); |
||
160 | } |
||
161 | } |
||
162 | } |
||
163 | $this->add( "}" ); |
||
164 | } |
||
165 | |||
227 | } |
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: