Completed
Push — outline ( 46d920 )
by mw
16:15
created

TemplateBuilder::tree()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 4
nc 5
nop 2
1
<?php
2
3
namespace SRF\Outline;
4
5
use SMW\Query\PrintRequest;
6
7
/**
8
 * @license GNU GPL v2+
9
 * @since 3.1
10
 *
11
 * @author mwjames
12
 */
13
class TemplateBuilder {
14
15
	/**
16
	 * @var []
17
	 */
18
	private $params = [];
19
20
	/**
21
	 * @var Linker
22
	 */
23
	private $linker;
24
25
	/**
26
	 * @var string
27
	 */
28
	private $template = '';
29
30
	/**
31
	 * @param array $params
32
	 */
33
	public function __construct( array $params ) {
34
		$this->params = $params;
35
	}
36
37
	/**
38
	 * @since 3.1
39
	 *
40
	 * @param Linker|null|false $linker
41
	 */
42
	public function setLinker( $linker ) {
43
		$this->linker = $linker;
0 ignored issues
show
Documentation Bug introduced by
It seems like $linker can also be of type false. However, the property $linker is declared as type object<SRF\Outline\Linker>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
44
	}
45
46
	/**
47
	 * @since 3.1
48
	 *
49
	 * @param OutlineTree $tree
50
	 *
51
	 * @return string
52
	 */
53
	public function build( $tree ) {
54
		$this->tree( $tree );
55
56
		return $this->template;
57
	}
58
59
	private function tree( $tree, $level = 0 ) {
60
61
		if ( $tree->mUnsortedItems !== null ) {
62
			foreach ( $tree->mUnsortedItems as $i => $item ) {
63
				$this->template .= $this->item( $i, $item );
64
			}
65
		}
66
67
		foreach ( $tree->mTree as $key => $node ) {
68
			$property = $this->params['outlineproperties'][$level];
69
			$class = $this->params['template'] . '-section-' . strtolower( str_replace( ' ', '-', $property ) );
70
71
			$this->template .= "<div class='$class'>";
72
			$this->template .= $this->open( $this->params['template'] . "-header" );
73
			$this->template .= $this->parameter( $property, $key );
74
			$this->template .= $this->parameter( "#outlinelevel", $level );
75
			$this->template .= $this->parameter( "#itemcount",  $node->leafCount );
76
			$this->template .= $this->parameter( "#userparam", $this->params['userparam'] );
77
			$this->template .= $this->close();
78
			$this->template .= "<div class='" . $this->params['template'] . "-items'>";
79
			$this->tree( $node, $level + 1 );
80
			$this->template .= "</div>";
81
			$this->template .= "</div>";
82
		}
83
	}
84
85
	private function item( $i, $item ) {
86
87
		$first_col = true;
88
		$template = '';
89
		$linker = $this->params['link'] === 'all' ? $this->linker : null;
90
		$itemnumber = 0;
91
92
		foreach ( $item->mRow as $resultArray ) {
93
94
			$printRequest = $resultArray->getPrintRequest();
95
			$val = $printRequest->getText( SMW_OUTPUT_WIKI, null );
96
97
			if ( in_array( $val, $this->params['outlineproperties'] ) ) {
98
				continue;
99
			}
100
101
			while ( ( $dv = $resultArray->getNextDataValue() ) !== false ) {
102
				$template .= $this->open( $this->params['template'] . '-item' );
103
				$template .= $this->parameter( "#itemsection", $i );
104
105
				$template .= $this->parameter( "#itemnumber", $itemnumber );
106
				$template .= $this->parameter( "#userparam", $this->params['userparam'] );
107
108
				$template .= $this->itemText( $dv, $linker, $printRequest, $first_col );
109
				$template .= $this->close();
110
111
				$itemnumber++;
112
			}
113
		}
114
115
		return "<div class='" . $this->params['template'] . "-item'>" . $template . '</div>';
116
	}
117
118
	private function itemText( $dv, $linker, $printRequest, &$first_col ) {
119
120
		if ( $first_col && $printRequest->isMode( PrintRequest::PRINT_THIS ) ) {
121
			$first_col = false;
122
123
			if ( $linker === null && ( $caption = $dv->getDisplayTitle() ) !== '' ) {
124
				$dv->setCaption( $caption );
125
			}
126
127
			$text = $dv->getShortText(
128
				SMW_OUTPUT_WIKI,
129
				$this->params['link'] === 'subject' ? $this->linker : $linker
130
			);
131
132
			return $this->parameter( "#itemsubject", $text );
133
		}
134
135
		$text = $dv->getShortText(
136
			SMW_OUTPUT_WIKI,
137
			$linker
138
		);
139
140
		return $this->parameter( $printRequest->getLabel(), $text );
141
	}
142
143
	private function open( $v ) {
144
		return "{{" . $v;
145
	}
146
147
	private function parameter( $k, $v ) {
148
		return " |$k=$v";
149
	}
150
151
	private function close() {
152
		return "}}";
153
	}
154
155
}