HtmlColumnListRenderer::doFormat()   C
last analyzed

Complexity

Conditions 11
Paths 82

Size

Total Lines 41
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 11

Importance

Changes 0
Metric Value
cc 11
eloc 21
nc 82
nop 4
dl 0
loc 41
ccs 21
cts 21
cp 1
crap 11
rs 5.2653
c 0
b 0
f 0

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 SMW\MediaWiki\Renderer;
4
5
use Html;
6
7
/**
8
 * Simple list formatter to transform an indexed array (e.g. array( 'F' => array( 'Foo', 'Bar' ) )
9
 * into a column divided list.
10
 *
11
 * @license GNU GPL v2+
12
 * @since 2.1
13
 *
14
 * @author mwjames
15
 * @author Markus Krötzsch
16
 */
17
class HtmlColumnListRenderer {
18
19
	/**
20
	 * @var integer
21
	 */
22
	private $numberOfColumns = 1;
23
24
	/**
25
	 * @var array
26
	 */
27
	private $contentsByIndex = array();
28
29
	/**
30
	 * @var integer
31
	 */
32
	private $numRows = 0;
33
34
	/**
35
	 * @var integer
36
	 */
37
	private $numberOfResults = 0;
38
39
	/**
40
	 * @var integer
41
	 */
42
	private $rowsPerColumn = 0;
43
44
	/**
45
	 * @var integer
46
	 */
47
	private $columnWidth = 0;
48
49
	/**
50
	 * @var string
51
	 */
52
	private $listType = 'ul';
53
54
	/**
55
	 * @var string
56
	 */
57
	private $columnListClass = 'smw-columnlist-container';
58
59
	/**
60
	 * @var string
61
	 */
62
	private $columnClass = 'smw-column';
63
64
	/**
65
	 * @var boolean
66
	 */
67
	private $isRTL = false;
68
69
	/**
70
	 * @since 2.2
71
	 *
72
	 * @param string $columnListClass
73
	 *
74
	 * @return HtmlColumnListRenderer
75
	 */
76 2
	public function setColumnListClass( $columnListClass ) {
77 2
		$this->columnListClass = htmlspecialchars( $columnListClass );
78 2
		return $this;
79
	}
80
81
	/**
82
	 * @since 2.2
83
	 *
84
	 * @param string $columnListClass
0 ignored issues
show
Bug introduced by
There is no parameter named $columnListClass. 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...
85
	 *
86
	 * @return HtmlColumnListRenderer
87
	 */
88 2
	public function setColumnClass( $columnClass ) {
89 2
		$this->columnClass = htmlspecialchars( $columnClass );
90 2
		return $this;
91
	}
92
93
	/**
94
	 * @since 2.3
95
	 *
96
	 * @param boolean $isRTL
97
	 */
98 1
	public function setColumnRTLDirectionalityState( $isRTL ) {
99 1
		$this->isRTL = (bool)$isRTL;
100 1
		return $this;
101
	}
102
103
	/**
104
	 * @since 2.1
105
	 *
106
	 * @param integer $numberOfColumns
107
	 *
108
	 * @return HtmlColumnListRenderer
109
	 */
110 8
	public function setNumberOfColumns( $numberOfColumns ) {
111 8
		$this->numberOfColumns = $numberOfColumns;
112 8
		return $this;
113
	}
114
115
	/**
116
	 * @since 2.1
117
	 *
118
	 * @param string $listType
119
	 *
120
	 * @return HtmlColumnListRenderer
121
	 */
122 3
	public function setListType( $listType ) {
123
124 3
		if ( in_array( $listType, array( 'ul', 'ol' ) ) ) {
125 3
			$this->listType = $listType;
126
		}
127
128 3
		return $this;
129
	}
130
131
	/**
132
	 * @since 2.1
133
	 *
134
	 * @param string[] $contentsByNoIndex
135
	 *
136
	 * @return HtmlColumnListRenderer
137
	 */
138 2
	public function addContentsByNoIndex( array $contentsByNoIndex ) {
139
140 2
		$contentsByEmptyIndex[''] = array();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$contentsByEmptyIndex was never initialized. Although not strictly required by PHP, it is generally a good practice to add $contentsByEmptyIndex = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
141
142 2
		foreach ( $contentsByNoIndex as $value ) {
143 2
			$contentsByEmptyIndex[''][] = $value;
144
		}
145
146 2
		return $this->addContentsByIndex( $contentsByEmptyIndex );
0 ignored issues
show
Documentation introduced by
$contentsByEmptyIndex is of type array<string,array,{"":"array"}>, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
147
	}
148
149
	/**
150
	 * @since 2.1
151
	 *
152
	 * @param string[] $contentsByIndex
153
	 *
154
	 * @return HtmlColumnListRenderer
155
	 */
156 9
	public function addContentsByIndex( array $contentsByIndex ) {
157 9
		$this->contentsByIndex = $contentsByIndex;
158 9
		$this->numberOfResults = count( $this->contentsByIndex, COUNT_RECURSIVE ) - count( $this->contentsByIndex );
159 9
		return $this;
160
	}
161
162
	/**
163
	 * @since  2.1
164
	 *
165
	 * @return string
166
	 */
167 9
	public function getHtml() {
168
169 9
		$result = '';
170 9
		$usedColumnCloser = false;
171 9
		$this->numRows = 0;
172
173
		// Class to determine whether we want responsive columns width
174 9
		if ( strpos( $this->columnClass, 'responsive' ) !== false ) {
175 1
			$this->columnWidth = 100;
176 1
			$this->numberOfColumns = 1;
177
		} else {
178 8
			$this->columnWidth = floor( 100 / $this->numberOfColumns );
0 ignored issues
show
Documentation Bug introduced by
The property $columnWidth was declared of type integer, but floor(100 / $this->numberOfColumns) is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
179
		}
180
181 9
		$this->rowsPerColumn = ceil( $this->numberOfResults / $this->numberOfColumns );
0 ignored issues
show
Documentation Bug introduced by
The property $rowsPerColumn was declared of type integer, but ceil($this->numberOfResu...$this->numberOfColumns) is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
182 9
		$listContinuesAbbrev = wfMessage( 'listingcontinuesabbrev' )->text();
183
184 9
		foreach ( $this->contentsByIndex as $key => $resultItems ) {
185
186 9
			if ( $resultItems === array() ) {
187
					continue;
188
			}
189
190 9
			$result .= $this->doFormat(
191
				$key,
192
				$listContinuesAbbrev,
193
				$resultItems,
194
				$usedColumnCloser
195
			);
196
		}
197
198 9
		if ( !$usedColumnCloser ) {
199 9
			$result .= "</{$this->listType}></div> <!-- end column -->";
200
		}
201
202 9
		return Html::rawElement(
203 9
			'div',
204
			array(
205 9
				'class' => $this->columnListClass,
206 9
				'dir'   => $this->isRTL ? 'rtl' : 'ltr'
207
			),
208 9
			$result . "\n" . '<br style="clear: both;"/>'
209
		);
210
	}
211
212 9
	private function doFormat( $key, $listContinuesAbbrev, $resultItems, &$usedColumnCloser ) {
213
214 9
		$result = '';
215 9
		$previousKey = "";
216 9
		$dir = $this->isRTL ? 'rtl' : 'ltr';
217
218 9
		foreach ( $resultItems as $resultItem ) {
219
220 9
			if ( $this->numRows % $this->rowsPerColumn == 0 ) {
221 9
				$result .= "<div class=\"$this->columnClass\" style=\"width:$this->columnWidth%;\" dir=\"$dir\">";
222
223 9
				$numRowsInColumn = $this->numRows + 1;
224
225 9
				if ( $key == $previousKey ) {
226
					// @codingStandardsIgnoreStart phpcs, ignore --sniffs=Generic.Files.LineLength.MaxExceeded
227 5
					$result .= ( $key !== '' ? Html::element( 'div', array( 'class' => 'smw-column-header' ), "$key $listContinuesAbbrev" ) : '' ) . "<{$this->listType} start={$numRowsInColumn}>";
228
					// @codingStandardsIgnoreEnd
229
				}
230
			}
231
232
			// if we're at a new first letter, end
233
			// the last list and start a new one
234 9
			if ( $key != $previousKey ) {
235 7
				$result .= $this->numRows % $this->rowsPerColumn > 0 ? "</{$this->listType}>" : '';
236 7
				$result .= ( $key !== '' ? Html::element( 'div', array( 'class' => 'smw-column-header' ), $key ) : '' ) . "<{$this->listType}>";
237
			}
238
239 9
			$previousKey = $key;
240 9
			$result .= Html::rawElement( 'li', array(), $resultItem );
241 9
			$usedColumnCloser = false;
242
243 9
			if ( ( $this->numRows + 1 ) % $this->rowsPerColumn == 0 && ( $this->numRows + 1 ) < $this->numberOfResults ) {
244 6
				$result .= "</{$this->listType}></div> <!-- end column -->";
245 6
				$usedColumnCloser = true;
246
			}
247
248 9
			$this->numRows++;
249
		}
250
251 9
		return $result;
252
	}
253
254
}
255