Completed
Pull Request — master (#538)
by
unknown
19:59
created

GraphPrinter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 3
dl 0
loc 216
rs 10
c 0
b 0
f 0
ccs 0
cts 89
cp 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A handleParameters() 0 15 1
A getResultText() 0 26 4
A processResultRow() 0 20 4
B getParamDefinitions() 0 75 1
1
<?php
2
3
namespace SRF\Graph;
4
5
use SMW\ResultPrinter;
6
use SMWQueryResult;
7
use SMWWikiPageValue;
8
use GraphViz;
9
use Html;
10
11
/**
12
 * SMW result printer for graphs using graphViz.
13
 * In order to use this printer you need to have both
14
 * the graphViz library installed on your system and
15
 * have the graphViz MediaWiki extension installed.
16
 *
17
 * @file SRF_Graph.php
18
 * @ingroup SemanticResultFormats
19
 *
20
 * @licence GNU GPL v2+
21
 * @author Frank Dengler
22
 * @author Jeroen De Dauw < [email protected] >
23
 * @author Sebastian Schmid
24
 */
25
class GraphPrinter extends ResultPrinter {
26
27
	const NODELABEL_DISPLAYTITLE = 'displaytitle';
28
	public static $NODE_LABELS = [
29
		self::NODELABEL_DISPLAYTITLE,
30
	];
31
32
	public static $NODE_SHAPES = [
33
		'box',
34
		'box3d',
35
		'circle',
36
		'component',
37
		'diamond',
38
		'doublecircle',
39
		'doubleoctagon',
40
		'egg',
41
		'ellipse',
42
		'folder',
43
		'hexagon',
44
		'house',
45
		'invhouse',
46
		'invtrapezium',
47
		'invtriangle',
48
		'Mcircle',
49
		'Mdiamond',
50
		'Msquare',
51
		'none',
52
		'note',
53
		'octagon',
54
		'parallelogram',
55
		'pentagon ',
56
		'plaintext',
57
		'point',
58
		'polygon',
59
		'rect',
60
		'rectangle',
61
		'septagon',
62
		'square',
63
		'tab',
64
		'trapezium',
65
		'triangle',
66
		'tripleoctagon',
67
	];
68
	private $nodes = [];
69
	private $options;
70
71
	public function getName() {
72
		return $this->msg( 'srf-printername-graph' )->text();
73
	}
74
75
	/**
76
	 * @see SMWResultPrinter::handleParameters()
77
	 */
78
	protected function handleParameters( array $params, $outputmode ) {
79
		parent::handleParameters( $params, $outputmode );
80
81
		$this->options->graphName = trim( $params['graphname'] );
82
		$this->options->graphSize = trim( $params['graphsize'] );
83
		$this->options->nodeShape = trim( $params['nodeshape'] );
84
		$this->options->nodeLabel = trim( $params['nodelabel'] );
85
		$this->options->rankDir = strtoupper( trim( $params['arrowdirection'] ) );
86
		$this->options->wordWrapLimit = trim( $params['wordwraplimit'] );
87
		$this->options->parentRelation = strtolower( trim( $params['relation'] ) ) == 'parent';
88
		$this->options->enableGraphLink = trim($params['graphlink']);
89
		$this->options->showGraphLabel = trim($params['graphlabel']);
90
		$this->options->showGraphColor = trim($params['graphcolor']);
91
		$this->options->showGraphLegend = trim( $params['graphlegend'] );
92
	}
93
94
	/**
95
	 * @param SMWQueryResult $res
96
	 * @param $outputmode
97
	 *
98
	 * @return string
99
	 */
100
	protected function getResultText( SMWQueryResult $res, $outputmode ) {
101
		if ( !class_exists( 'GraphViz' )
102
			&& !class_exists( '\\MediaWiki\\Extension\\GraphViz\\GraphViz' )
103
		) {
104
			wfWarn( 'The SRF Graph printer needs the GraphViz extension to be installed.' );
105
			return '';
106
		}
107
108
		// iterate query result and create SRF\GraphNodes
109
		while ( $row = $res->getNext() ) {
110
			$this->processResultRow( $row );
111
		}
112
113
		// use GraphFormater to build the graph
114
		$graphFormatter = new GraphFormatter( $this->options );
115
		$graphFormatter->buildGraph( $this->nodes );
116
117
		// Calls graphvizParserHook function from MediaWiki GraphViz extension
118
		$result = $GLOBALS['wgParser']->recursiveTagParse( "<graphviz>" . $graphFormatter->getGraph
119
				() . "</graphviz>" );
120
121
		// append legend
122
		$result .= $graphFormatter->getGraphLegend();
123
124
		return $result;
125
	}
126
127
	/**
128
	 * Process a result row and create SRF\GraphNodes
129
	 *
130
	 * @since 3.1
131
	 *
132
	 * @param array $row
133
	 *
134
	 */
135
	protected function processResultRow( array /* of SMWResultArray */ $row ) {
136
137
		// loop through all row fields
138
		foreach ( $row as $i => $resultArray ) {
139
140
			// loop through all values of a multivalue field
141
			while ( ( /* SMWWikiPageValue */
142
				$object = $resultArray->getNextDataValue() ) !== false ) {
143
144
				// create SRF\GraphNode for column 0
145
				if ( $i == 0 ) {
146
					$node = new GraphNode( $object->getShortWikiText() );
147
					$node->setLabel( $object->getDisplayTitle() );
148
					$this->nodes[] = $node;
149
				} else {
150
					$node->addParentNode( $resultArray->getPrintRequest()->getLabel(), $object->getShortWikiText() );
0 ignored issues
show
Bug introduced by
The variable $node does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
151
				}
152
			}
153
		}
154
	}
155
156
	/**
157
	 * @see SMWResultPrinter::getParamDefinitions
158
	 *
159
	 * @since 1.8
160
	 *
161
	 * @param $definitions array of IParamDefinition
162
	 *
163
	 * @return array of IParamDefinition|array
164
	 */
165
	public function getParamDefinitions( array $definitions ) {
166
		$params = parent::getParamDefinitions( $definitions );
167
168
		$params['graphname'] = [
169
			'default' => 'QueryResult',
170
			'message' => 'srf-paramdesc-graphname',
171
		];
172
173
		$params['graphsize'] = [
174
			'type' => 'string',
175
			'default' => '',
176
			'message' => 'srf-paramdesc-graphsize',
177
			'manipulatedefault' => false,
178
		];
179
180
		$params['graphlegend'] = [
181
			'type' => 'boolean',
182
			'default' => false,
183
			'message' => 'srf-paramdesc-graphlegend',
184
		];
185
186
		$params['graphlabel'] = [
187
			'type' => 'boolean',
188
			'default' => false,
189
			'message' => 'srf-paramdesc-graphlabel',
190
		];
191
192
		$params['graphlink'] = [
193
			'type' => 'boolean',
194
			'default' => false,
195
			'message' => 'srf-paramdesc-graphlink',
196
		];
197
198
		$params['graphcolor'] = [
199
			'type' => 'boolean',
200
			'default' => false,
201
			'message' => 'srf-paramdesc-graphcolor',
202
		];
203
204
		$params['arrowdirection'] = [
205
			'aliases' => 'rankdir',
206
			'default' => 'LR',
207
			'message' => 'srf-paramdesc-rankdir',
208
			'values'  => [ 'LR', 'RL', 'TB', 'BT' ],
209
		];
210
211
		$params['nodeshape'] = [
212
			'default' => false,
213
			'message' => 'srf-paramdesc-graph-nodeshape',
214
			'manipulatedefault' => false,
215
			'values' => self::$NODE_SHAPES,
216
		];
217
218
		$params['relation'] = [
219
			'default' => 'child',
220
			'message' => 'srf-paramdesc-graph-relation',
221
			'manipulatedefault' => false,
222
			'values' => [ 'parent', 'child' ],
223
		];
224
225
		$params['wordwraplimit'] = [
226
			'type' => 'integer',
227
			'default' => 25,
228
			'message' => 'srf-paramdesc-graph-wwl',
229
			'manipulatedefault' => false,
230
		];
231
232
		$params['nodelabel'] = [
233
			'default' => '',
234
			'message' => 'srf-paramdesc-nodelabel',
235
			'values' => self::$NODE_LABELS,
236
		];
237
238
		return $params;
239
	}
240
}
241