Completed
Push — master ( c81f6c...1650a0 )
by mw
07:33
created

ListTreeBuilder::tree()   C

Complexity

Conditions 11
Paths 256

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 11.0397

Importance

Changes 0
Metric Value
dl 0
loc 50
ccs 27
cts 29
cp 0.931
rs 5.7833
c 0
b 0
f 0
cc 11
nc 256
nop 2
crap 11.0397

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SRF\Outline;
4
5
use SMW\Query\PrintRequest;
6
use SRF\Outline\OutlineTree;
7
use SMWDataItem as DataItem;
8
9
/**
10
 * @license GNU GPL v2+
11
 * @since 3.1
12
 *
13
 * @author mwjames
14
 */
15
class ListTreeBuilder {
16
17
	/**
18
	 * @var []
19
	 */
20
	private $params = [];
21
22
	/**
23
	 * @var Linker
24
	 */
25
	private $linker;
26
27
	/**
28
	 * @param array $params
29
	 */
30 3
	public function __construct( array $params ) {
31 3
		$this->params = $params;
32 3
	}
33
34
	/**
35
	 * @since 3.1
36
	 *
37
	 * @param Linker|null|false $linker
38
	 */
39 1
	public function setLinker( $linker ) {
40 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...
41 1
	}
42
43
	/**
44
	 * @since 3.1
45
	 *
46
	 * @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...
47
	 *
48
	 * @return string
49
	 */
50 2
	public function build( OutlineTree $outlineTree ) {
51 2
		return $this->tree( $outlineTree );
52
	}
53
54 2
	private function tree( $outline_tree, $level = 0 ) {
55 2
		$text = "";
56
57 2
		if ( !is_null( $outline_tree->items ) ) {
58 2
			$text .= "<ul>\n";
59 2
			foreach ( $outline_tree->items as $item ) {
60 1
				$text .= "<li>{$this->item($item)}</li>\n";
61
			}
62 2
			$text .= "</ul>\n";
63
		}
64
65 2
		if ( $level > 0 ) {
66 1
			$text .= "<ul>\n";
67
		}
68
69 2
		$num_levels = count( $this->params['outlineproperties'] );
70
		// set font size and weight depending on level we're at
71 2
		$font_level = $level;
72
73 2
		if ( $num_levels < 4 ) {
74 2
			$font_level += ( 4 - $num_levels );
75
		}
76
77 2
		if ( $font_level == 0 ) {
78
			$font_size = 'x-large';
79 2
		} elseif ( $font_level == 1 ) {
80
			$font_size = 'large';
81 2
		} elseif ( $font_level == 2 ) {
82 1
			$font_size = 'medium';
83
		} else {
84 2
			$font_size = 'small';
85
		}
86
87 2
		if ( $font_level == 3 ) {
88 2
			$font_weight = 'bold';
89
		} else {
90 1
			$font_weight = 'regular';
91
		}
92
93 2
		foreach ( $outline_tree->tree as $key => $node ) {
94 1
			$text .= "<p style=\"font-size: $font_size; font-weight: $font_weight;\">$key</p>\n";
95 1
			$text .= $this->tree( $node, $level + 1 );
96
		}
97
98 2
		if ( $level > 0 ) {
99 1
			$text .= "</ul>\n";
100
		}
101
102 2
		return $text;
103
	}
104
105 1
	private function item( $item ) {
106 1
		$first_col = true;
107 1
		$found_values = false; // has anything but the first column been printed?
108 1
		$result = "";
109
110 1
		foreach ( $item->row as $resultArray ) {
111
112 1
			$printRequest = $resultArray->getPrintRequest();
113 1
			$val = $printRequest->getText( SMW_OUTPUT_WIKI, null );
114 1
			$first_value = true;
115
116 1
			if ( in_array( $val, $this->params['outlineproperties'] ) ) {
117 1
				continue;
118
			}
119
120 1
			$linker = $this->params['link'] === 'all' ? $this->linker : null;
121
122 1
			if ( $this->params['link'] === 'subject' && $printRequest->isMode( PrintRequest::PRINT_THIS ) ) {
123
				$linker = $this->linker;
124
			}
125
126 1
			while ( ( $dv = $resultArray->getNextDataValue() ) !== false ) {
127
128 1
				if ( !$first_col && !$found_values ) { // first values after first column
129 1
					$result .= ' (';
130 1
					$found_values = true;
131 1
				} elseif ( $found_values || !$first_value ) {
132
					// any value after '(' or non-first values on first column
133
					$result .= ', ';
134
				}
135
136 1
				if ( $first_value ) { // first value in any column, print header
137 1
					$first_value = false;
138 1
					if ( $this->params['showHeaders'] && ( '' != $printRequest->getLabel() ) ) {
139 1
						$result .= $printRequest->getText( SMW_OUTPUT_WIKI, $linker ) . ' ';
140
					}
141
				}
142
143 1
				$dataItem = $dv->getDataItem();
144
145 1
				if ( $linker === null && $dataItem->getDIType() === DataItem::TYPE_WIKIPAGE && ( $caption = $dv->getDisplayTitle() ) !== '' ) {
146
					$dv->setCaption( $caption );
147
				}
148
149 1
				$result .= $dv->getShortText( SMW_OUTPUT_WIKI, $linker );
150
			}
151
152 1
			$first_col = false;
153
		}
154
155 1
		if ( $found_values ) {
156 1
			$result .= ')';
157
		}
158
159 1
		return $result;
160
	}
161
162
}