Completed
Push — master ( 0b1d89...ca460c )
by Stephan
05:55 queued 02:40
created

TreeNodePrinter   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 93.06%

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 5
dl 0
loc 180
ccs 67
cts 72
cp 0.9306
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getTextForNode() 0 22 4
B getTextForRowNoTemplate() 0 29 5
A getTextForRowWithTemplate() 0 15 2
A getValuesTextForCell() 0 14 2
B getParamNameForCell() 0 19 5
B getLabelForCell() 0 17 5
A visit() 0 18 2
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
$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
	 * @return string
69
	 */
70 5
	protected function getTextForRowNoTemplate( $row ) {
71
72 5
		$cellTexts = [];
73 5
		foreach ( $row as $columnNumber => $cell ) {
74
75 5
			$valuesText = $this->getValuesTextForCell( $cell, $columnNumber );
76
77 5
			if ( $valuesText === '' ) {
78 1
				continue;
79
			}
80
81 5
			$labelText = $this->getLabelForCell( $cell, $columnNumber );
82
83 5
			$cellTexts[] = $labelText . $valuesText;
84
		}
85
86 5
		if ( count( $cellTexts ) > 0 ) {
87 5
			$result = array_shift( $cellTexts );
88
89 5
			if ( count( $cellTexts ) > 0 ) {
90 5
				$result .= ' (' . join( $this->configuration[ 'sep' ], $cellTexts ) . ')';
91
			}
92
93
		} else {
94
			$result = '';
95
		}
96
97 5
		return $result;
98
	}
99
100
	/**
101
	 * @param \SMWResultArray[] $row
102
	 * @return string
103
	 */
104 3
	protected function getTextForRowWithTemplate( $row ) {
105
106 3
		$templateParams = [];
107 3
		foreach ( $row as $columnNumber => $cell ) {
108
109 3
			$valuesText = $this->getValuesTextForCell( $cell, $columnNumber );
110 3
			$paramName = $this->getParamNameForCell( $cell, $columnNumber );
111
112 3
			$templateParams[] = "$paramName=$valuesText ";
113
		}
114
115 3
		$templateParams[] = "#=$this->rowNumber ";
116
117 3
		return $this->resultPrinter->getTemplateCall( $this->configuration[ 'template' ], $templateParams );
118
	}
119
120
	/**
121
	 * @param \SMWResultArray $cell
122
	 * @param int $columnNumber
123
	 * @return string
124
	 */
125 5
	protected function getValuesTextForCell( \SMWResultArray $cell, $columnNumber ) {
126
127 5
		$cell->reset();
128 5
		$linker = $this->resultPrinter->getLinkerForColumn( $columnNumber );
129
130 5
		$valueTexts = [];
131
132 5
		while ( ( $text = $cell->getNextText( SMW_OUTPUT_WIKI, $linker ) ) !== false ) {
133 5
			$valueTexts[] = $text;
134
		}
135
136 5
		$valuesText = join( $this->configuration[ 'sep' ], $valueTexts );
137 5
		return $valuesText;
138
	}
139
140
	/**
141
	 * @param \SMWResultArray $cell
142
	 * @param int $columnNumber
143
	 * @return string
144
	 */
145 3
	protected function getParamNameForCell( $cell, $columnNumber ) {
146
147 3
		if ( !array_key_exists( $columnNumber, $this->columnLabels ) ) {
148
149 3
			$label = $cell->getPrintRequest()->getLabel();
150
151 3
			if ( $this->configuration[ 'template arguments' ] === 'numbered' || ( $label === '' ) ) {
152 3
				$paramName = $columnNumber + 1;
153
			} elseif ( $this->configuration[ 'template arguments' ] === 'legacy' ) {
154
				$paramName = '?' . $label;
155
			} else { // $this->configuration[ 'template arguments' ] === 'named'
156
				$paramName = $label;
157
			}
158
159 3
			$this->columnLabels[ $columnNumber ] = $paramName;
160
		}
161
162 3
		return $this->columnLabels[ $columnNumber ];
163
	}
164
165
	/**
166
	 * @param \SMWResultArray $cell
167
	 * @return string
168
	 */
169 5
	protected function getLabelForCell( $cell, $columnNumber ) {
170
171 5
		if ( !array_key_exists( $columnNumber, $this->columnLabels ) ) {
172
173 5
			if ( $this->configuration[ 'headers' ] === 'hide' || $cell->getPrintRequest()->getLabel() === '' ) {
174 5
				$labelText = '';
175 1
			} elseif ( $this->configuration[ 'headers' ] === 'plain' ) {
176
				$labelText = $cell->getPrintRequest()->getText( SMW_OUTPUT_WIKI ) . ': ';
177
			} else { // $this->configuration[ 'headers' ] === 'link'
178 1
				$labelText = $cell->getPrintRequest()->getText( SMW_OUTPUT_WIKI, $this->resultPrinter->getLinker() ) . ': ';
179
			}
180
181 5
			$this->columnLabels[ $columnNumber ] = $labelText;
182
		}
183
184 5
		return $this->columnLabels[ $columnNumber ];
185
	}
186
187
}
188