TreeNodePrinter   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.26%

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 3
dl 0
loc 186
ccs 71
cts 73
cp 0.9726
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A visit() 0 18 2
A getTextForNode() 0 22 4
A getTextForRowNoTemplate() 0 29 5
A getTextForRowWithTemplate() 0 15 2
A getValuesTextForCell() 0 14 2
A getParamNameForCell() 0 17 4
A getLabelForCell() 0 20 5
1
<?php
2
3
namespace SRF\Formats\Tree;
4
5
use Tree\Node\NodeInterface;
6
use Tree\Visitor\Visitor;
7
8
class TreeNodePrinter implements Visitor {
9
10
	private $depth = 0;
11
	private $rowNumber = 0;
12
	private $configuration = null;
13
	/**
14
	 * @var TreeResultPrinter
15
	 */
16
	private $resultPrinter = null;
17
	private $columnLabels = [];
18
19 5
	public function __construct( TreeResultPrinter $resultPrinter, $configuration ) {
20 5
		$this->configuration = $configuration;
21 5
		$this->resultPrinter = $resultPrinter;
22 5
	}
23
24 5
	public function visit( NodeInterface $node ) {
25
26 5
		$nodeTexts = [ $this->getTextForNode( $node ) ];
0 ignored issues
show
Compatibility introduced by Stephan Gambke
$node of type object<Tree\Node\NodeInterface> is not a sub-type of object<SRF\Formats\Tree\TreeNode>. It seems like you assume a concrete implementation of the interface Tree\Node\NodeInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
27
28 5
		$this->depth++;
29 5
		$this->rowNumber++;
30
31 5
		foreach ( $node->getChildren() as $child ) {
32 5
			$nodeTexts = array_merge(
33 5
				$nodeTexts,
34 5
				$child->accept( $this )
35
			);
36
		}
37
38 5
		$this->depth--;
39
40 5
		return $nodeTexts;
41
	}
42
43 5
	protected function getTextForNode( TreeNode $node ) {
44
45
		/** @var \SMWResultArray[]|null $row */
46 5
		$row = $node->getValue();
47
48 5
		if ( $row === null ) {
49 5
			return '';
50
		}
51
52 5
		$textForNode = str_repeat( ( $this->configuration['format'] === 'oltree' ) ? '#' : '*', $this->depth );
53
54 5
		if ( $this->configuration['template'] === '' ) {
55
			// build simple list
56 5
			$textForNode .= $this->getTextForRowNoTemplate( $row );
57
		} else {
58
			// build template code
59 3
			$textForNode .= $this->getTextForRowWithTemplate( $row );
60
61
		}
62
63 5
		return $textForNode;
64
	}
65
66
	/**
67
	 * @param \SMWResultArray[] $row
68
	 *
69
	 * @return string
70
	 */
71 5
	protected function getTextForRowNoTemplate( $row ) {
72
73 5
		$cellTexts = [];
74 5
		foreach ( $row as $columnNumber => $cell ) {
75
76 5
			$valuesText = $this->getValuesTextForCell( $cell, $columnNumber );
77
78 5
			if ( $valuesText === '' ) {
79 1
				continue;
80
			}
81
82 5
			$labelText = $this->getLabelForCell( $cell, $columnNumber );
83
84 5
			$cellTexts[] = $labelText . $valuesText;
85
		}
86
87 5
		if ( count( $cellTexts ) > 0 ) {
88 5
			$result = array_shift( $cellTexts );
89
90 5
			if ( count( $cellTexts ) > 0 ) {
91 5
				$result .= ' (' . join( $this->configuration['sep'], $cellTexts ) . ')';
92
			}
93
94
		} else {
95
			$result = '';
96
		}
97
98 5
		return $result;
99
	}
100
101
	/**
102
	 * @param \SMWResultArray[] $row
103
	 *
104
	 * @return string
105
	 */
106 3
	protected function getTextForRowWithTemplate( $row ) {
107
108 3
		$templateParams = [];
109 3
		foreach ( $row as $columnNumber => $cell ) {
110
111 3
			$valuesText = $this->getValuesTextForCell( $cell, $columnNumber );
112 3
			$paramName = $this->getParamNameForCell( $cell, $columnNumber );
113
114 3
			$templateParams[] = "$paramName=$valuesText ";
115
		}
116
117 3
		$templateParams[] = "#=$this->rowNumber ";
118
119 3
		return $this->resultPrinter->getTemplateCall( $this->configuration['template'], $templateParams );
120
	}
121
122
	/**
123
	 * @param \SMWResultArray $cell
124
	 * @param int $columnNumber
125
	 *
126
	 * @return string
127
	 */
128 5
	protected function getValuesTextForCell( \SMWResultArray $cell, $columnNumber ) {
129
130 5
		$cell->reset();
131 5
		$linker = $this->resultPrinter->getLinkerForColumn( $columnNumber );
132
133 5
		$valueTexts = [];
134
135 5
		while ( ( $text = $cell->getNextText( SMW_OUTPUT_WIKI, $linker ) ) !== false ) {
136 5
			$valueTexts[] = $text;
137
		}
138
139 5
		$valuesText = join( $this->configuration['sep'], $valueTexts );
140 5
		return $valuesText;
141
	}
142
143
	/**
144
	 * @param \SMWResultArray $cell
145
	 * @param int $columnNumber
146
	 *
147
	 * @return string
148
	 */
149 3
	protected function getParamNameForCell( $cell, $columnNumber ) {
150
151 3
		if ( !array_key_exists( $columnNumber, $this->columnLabels ) ) {
152
153 3
			$label = $cell->getPrintRequest()->getLabel();
154
155 3
			if ( $this->configuration[ 'named args' ] === true || ( $label === '' ) ) {
156 3
				$paramName = $columnNumber + 1;
157
			} else {
158 1
				$paramName = $label;
159
			}
160
161 3
			$this->columnLabels[$columnNumber] = $paramName;
162
		}
163
164 3
		return $this->columnLabels[$columnNumber];
165
	}
166
167
	/**
168
	 * @param \SMWResultArray $cell
169
	 *
170
	 * @return string
171
	 */
172 5
	protected function getLabelForCell( $cell, $columnNumber ) {
173
174 5
		if ( !array_key_exists( $columnNumber, $this->columnLabels ) ) {
175
176 5
			if ( $this->configuration['headers'] === 'hide' || $cell->getPrintRequest()->getLabel() === '' ) {
177 5
				$labelText = '';
178 1
			} elseif ( $this->configuration['headers'] === 'plain' ) {
179
				$labelText = $cell->getPrintRequest()->getText( SMW_OUTPUT_WIKI ) . ': ';
180
			} else { // $this->configuration[ 'headers' ] === 'link'
181 1
				$labelText = $cell->getPrintRequest()->getText(
182 1
						SMW_OUTPUT_WIKI,
183 1
						$this->resultPrinter->getLinker()
184 1
					) . ': ';
185
			}
186
187 5
			$this->columnLabels[$columnNumber] = $labelText;
188
		}
189
190 5
		return $this->columnLabels[$columnNumber];
191
	}
192
193
}
194