Completed
Push — master ( ba4f70...8cc42f )
by Jeroen De
05:41
created

TemplateBuilder::itemRaw()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
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
use SRF\Outline\OutlineTree;
7
8
/**
9
 * @license GNU GPL v2+
10
 * @since 3.1
11
 *
12
 * @author mwjames
13
 */
14
class TemplateBuilder {
15
16
	/**
17
	 * @var []
18
	 */
19
	private $params = [];
20
21
	/**
22
	 * @var Linker
23
	 */
24
	private $linker;
25
26
	/**
27
	 * @var string
28
	 */
29
	private $template = '';
30
31
	/**
32
	 * @param array $params
33
	 */
34 3
	public function __construct( array $params ) {
35 3
		$this->params = $params;
36 3
	}
37
38
	/**
39
	 * @since 3.1
40
	 *
41
	 * @param Linker|null|false $linker
42
	 */
43 1
	public function setLinker( $linker ) {
44 1
		$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...
45 1
	}
46
47
	/**
48
	 * @since 3.1
49
	 *
50
	 * @param OutlineTree $tree
0 ignored issues
show
Bug introduced by
There is no parameter named $tree. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
51
	 *
52
	 * @return string
53
	 */
54 2
	public function build( OutlineTree $outlineTree ) {
55 2
		$this->tree( $outlineTree );
56
57 2
		return $this->getIntroTemplate() . $this->template . $this->getOutroTemplate();
58
	}
59
60 2
	private function tree( $outlineTree, $level = 0 ) {
61
62 2
		if ( $outlineTree->items !== null ) {
63 2
			foreach ( $outlineTree->items as $i => $item ) {
64 1
				$this->template .= $this->item( $i, $item );
65
			}
66
		}
67
68 2
		foreach ( $outlineTree->tree as $key => $node ) {
69 1
			$property = $this->params['outlineproperties'][$level];
70 1
			$class = $this->params['template'] . '-section-' . strtolower( str_replace( ' ', '-', $property ) );
71
72 1
			$this->template .= "<div class='$class'>";
73 1
			$this->template .= $this->open( $this->params['template'] . "-header" );
74 1
			$this->template .= $this->parameter( $property, $key );
75 1
			$this->template .= $this->parameter( "#outlinelevel", $level );
76 1
			$this->template .= $this->parameter( "#itemcount",  $node->leafCount );
77 1
			$this->template .= $this->parameter( "#userparam", $this->params['userparam'] );
78 1
			$this->template .= $this->close();
79 1
			$this->template .= "<div class='" . $this->params['template'] . "-items'>";
80 1
			$this->tree( $node, $level + 1 );
81 1
			$this->template .= "</div>";
82 1
			$this->template .= "</div>";
83
		}
84 2
	}
85
86 1
	private function item( $i, $item ) {
87
88 1
		$first_col = true;
89 1
		$template = '';
90 1
		$linker = $this->params['link'] === 'all' ? $this->linker : null;
91 1
		$itemnumber = 0;
92
93 1
		foreach ( $item->row as $resultArray ) {
94
95 1
			$printRequest = $resultArray->getPrintRequest();
96 1
			$val = $printRequest->getText( SMW_OUTPUT_WIKI, null );
97
98 1
			if ( in_array( $val, $this->params['outlineproperties'] ) ) {
99 1
				continue;
100
			}
101
102 1
			while ( ( $dv = $resultArray->getNextDataValue() ) !== false ) {
103 1
				$template .= $this->open( $this->params['template'] . '-item' );
104 1
				$template .= $this->parameter( "#itemsection", $i );
105
106 1
				$template .= $this->parameter( "#itemnumber", $itemnumber );
107 1
				$template .= $this->parameter( "#userparam", $this->params['userparam'] );
108
109 1
				$template .= $this->itemRaw($dv);
110
111 1
				$template .= $this->itemText( $dv, $linker, $printRequest, $first_col );
112 1
				$template .= $this->close();
113
114 1
				$itemnumber++;
115
			}
116
		}
117
118 1
		return "<div class='" . $this->params['template'] . "-item'>" . $template . '</div>';
119
	}
120
121 1
	private function itemText( $dv, $linker, $printRequest, &$first_col ) {
122
123 1
		if ( $first_col && $printRequest->isMode( PrintRequest::PRINT_THIS ) ) {
124 1
			$first_col = false;
125
126 1
			if ( $linker === null && ( $caption = $dv->getDisplayTitle() ) !== '' ) {
127
				$dv->setCaption( $caption );
128
			}
129
130 1
			$text = $dv->getShortText(
131 1
				SMW_OUTPUT_WIKI,
132 1
				$this->params['link'] === 'subject' ? $this->linker : $linker
133
			);
134
135 1
			return $this->parameter( "#itemsubject", $text );
136
		}
137
138 1
		$text = $dv->getShortText(
139 1
			SMW_OUTPUT_WIKI,
140
			$linker
141
		);
142
143 1
		return $this->parameter( $printRequest->getLabel(), $text );
144
	}
145
146 1
	private function itemRaw( $dv ){
147 1
		$rawText = $dv->getShortText( SMW_OUTPUT_WIKI );
148
149 1
		return $this->parameter( "#itemsubjectraw", $rawText );
150
	}
151
152 2
	private function open( $v ) {
153 2
		return "{{" . $v;
154
	}
155
156 1
	private function parameter( $k, $v ) {
157 1
		return " |$k=$v";
158
	}
159
160 2
	private function close() {
161 2
		return "}}";
162
	}
163
164 2
	function getIntroTemplate(): string {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
165 2
		if ( $this->params['introtemplate'] === '' ) {
166 1
			return "";
167
		}
168 2
		return $this->open( $this->params['introtemplate'] ) . $this->close();
169
	}
170
171 2
	function getOutroTemplate(): string {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
172 2
		if ( $this->params['outrotemplate'] === '' ) {
173 1
			return "";
174
		}
175 2
		return $this->open( $this->params['outrotemplate'] ) . $this->close();
176
	}
177
178
}
179