Completed
Push — master ( 195f4f...8c0d0a )
by Jeroen De
05:54
created

TemplateBuilder::getIntroTemplate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
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->itemText( $dv, $linker, $printRequest, $first_col );
110 1
				$template .= $this->close();
111
112 1
				$itemnumber++;
113
			}
114
		}
115
116 1
		return "<div class='" . $this->params['template'] . "-item'>" . $template . '</div>';
117
	}
118
119 1
	private function itemText( $dv, $linker, $printRequest, &$first_col ) {
120
121 1
		if ( $first_col && $printRequest->isMode( PrintRequest::PRINT_THIS ) ) {
122 1
			$first_col = false;
123
124 1
			if ( $linker === null && ( $caption = $dv->getDisplayTitle() ) !== '' ) {
125
				$dv->setCaption( $caption );
126
			}
127
128 1
			$text = $dv->getShortText(
129 1
				SMW_OUTPUT_WIKI,
130 1
				$this->params['link'] === 'subject' ? $this->linker : $linker
131
			);
132
133 1
			return $this->parameter( "#itemsubject", $text );
134
		}
135
136 1
		$text = $dv->getShortText(
137 1
			SMW_OUTPUT_WIKI,
138
			$linker
139
		);
140
141 1
		return $this->parameter( $printRequest->getLabel(), $text );
142
	}
143
144 2
	private function open( $v ) {
145 2
		return "{{" . $v;
146
	}
147
148 1
	private function parameter( $k, $v ) {
149 1
		return " |$k=$v";
150
	}
151
152 2
	private function close() {
153 2
		return "}}";
154
	}
155
156 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...
157 2
		if ( $this->params['introtemplate'] ) {
158 2
			return $this->open( $this->params['introtemplate'] ) . $this->close();
159
		}
160 1
		return "";
161
	}
162
163 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...
164 2
		if ( $this->params['outrotemplate'] ) {
165 2
			return $this->open( $this->params['outrotemplate'] ) . $this->close();
166
		}
167 1
		return "";
168
	}
169
170
}