Completed
Push — master ( 0e466a...c81f6c )
by mw
06:27
created

TemplateBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 2
	public function __construct( array $params ) {
34 2
		$this->params = $params;
35 2
	}
36
37
	/**
38
	 * @since 3.1
39
	 *
40
	 * @param Linker|null|false $linker
41
	 */
42 1
	public function setLinker( $linker ) {
43 1
		$this->linker = $linker;
0 ignored issues
show
Documentation Bug introduced by mwjames
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 1
	}
45
46
	/**
47
	 * @since 3.1
48
	 *
49
	 * @param OutlineTree $tree
50
	 *
51
	 * @return string
52
	 */
53 1
	public function build( $tree ) {
54 1
		$this->tree( $tree );
55
56 1
		return $this->template;
57
	}
58
59 1
	private function tree( $tree, $level = 0 ) {
60
61 1
		if ( $tree->mUnsortedItems !== null ) {
62 1
			foreach ( $tree->mUnsortedItems as $i => $item ) {
63 1
				$this->template .= $this->item( $i, $item );
64
			}
65
		}
66
67 1
		foreach ( $tree->mTree as $key => $node ) {
68 1
			$property = $this->params['outlineproperties'][$level];
69 1
			$class = $this->params['template'] . '-section-' . strtolower( str_replace( ' ', '-', $property ) );
70
71 1
			$this->template .= "<div class='$class'>";
72 1
			$this->template .= $this->open( $this->params['template'] . "-header" );
73 1
			$this->template .= $this->parameter( $property, $key );
74 1
			$this->template .= $this->parameter( "#outlinelevel", $level );
75 1
			$this->template .= $this->parameter( "#itemcount",  $node->leafCount );
76 1
			$this->template .= $this->parameter( "#userparam", $this->params['userparam'] );
77 1
			$this->template .= $this->close();
78 1
			$this->template .= "<div class='" . $this->params['template'] . "-items'>";
79 1
			$this->tree( $node, $level + 1 );
80 1
			$this->template .= "</div>";
81 1
			$this->template .= "</div>";
82
		}
83 1
	}
84
85 1
	private function item( $i, $item ) {
86
87 1
		$first_col = true;
88 1
		$template = '';
89 1
		$linker = $this->params['link'] === 'all' ? $this->linker : null;
90 1
		$itemnumber = 0;
91
92 1
		foreach ( $item->mRow as $resultArray ) {
93
94 1
			$printRequest = $resultArray->getPrintRequest();
95 1
			$val = $printRequest->getText( SMW_OUTPUT_WIKI, null );
96
97 1
			if ( in_array( $val, $this->params['outlineproperties'] ) ) {
98 1
				continue;
99
			}
100
101 1
			while ( ( $dv = $resultArray->getNextDataValue() ) !== false ) {
102 1
				$template .= $this->open( $this->params['template'] . '-item' );
103 1
				$template .= $this->parameter( "#itemsection", $i );
104
105 1
				$template .= $this->parameter( "#itemnumber", $itemnumber );
106 1
				$template .= $this->parameter( "#userparam", $this->params['userparam'] );
107
108 1
				$template .= $this->itemText( $dv, $linker, $printRequest, $first_col );
109 1
				$template .= $this->close();
110
111 1
				$itemnumber++;
112
			}
113
		}
114
115 1
		return "<div class='" . $this->params['template'] . "-item'>" . $template . '</div>';
116
	}
117
118 1
	private function itemText( $dv, $linker, $printRequest, &$first_col ) {
119
120 1
		if ( $first_col && $printRequest->isMode( PrintRequest::PRINT_THIS ) ) {
121 1
			$first_col = false;
122
123 1
			if ( $linker === null && ( $caption = $dv->getDisplayTitle() ) !== '' ) {
124
				$dv->setCaption( $caption );
125
			}
126
127 1
			$text = $dv->getShortText(
128 1
				SMW_OUTPUT_WIKI,
129 1
				$this->params['link'] === 'subject' ? $this->linker : $linker
130
			);
131
132 1
			return $this->parameter( "#itemsubject", $text );
133
		}
134
135 1
		$text = $dv->getShortText(
136 1
			SMW_OUTPUT_WIKI,
137 1
			$linker
138
		);
139
140 1
		return $this->parameter( $printRequest->getLabel(), $text );
141
	}
142
143 1
	private function open( $v ) {
144 1
		return "{{" . $v;
145
	}
146
147 1
	private function parameter( $k, $v ) {
148 1
		return " |$k=$v";
149
	}
150
151 1
	private function close() {
152 1
		return "}}";
153
	}
154
155
}