Completed
Push — master ( ab2e37...f0a9c0 )
by Henry
13:19 queued 11:13
created

Comment::render()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 29
cts 29
cp 1
rs 8.9818
c 0
b 0
f 0
cc 4
nc 3
nop 0
crap 4

How to fix   Long Method   

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
namespace Redaxscript\Navigation;
3
4
use Redaxscript\Html;
5
use Redaxscript\Model;
6
use Redaxscript\Module;
7
use Redaxscript\Validator;
8
9
/**
10
 * children class to create the comment navigation
11
 *
12
 * @since 3.3.0
13
 *
14
 * @package Redaxscript
15
 * @category Navigation
16
 * @author Henry Ruhs
17
 */
18
19
class Comment extends NavigationAbstract
20
{
21
	/**
22
	 * options of the navigation
23
	 *
24
	 * @var array
25
	 */
26
27
	protected $_optionArray =
28
	[
29
		'className' =>
30
		[
31
			'list' => 'rs-list-comments'
32
		],
33
		'orderColumn' => 'rank'
34
	];
35
36
	/**
37
	 * render the view
38
	 *
39
	 * @since 3.3.0
40
	 *
41
	 * @return string
42
	 */
43
44 2
	public function render() : string
45
	{
46 2
		$output = Module\Hook::trigger('navigationCommentStart');
47 2
		$outputItem = null;
48 2
		$commentModel = new Model\Comment();
49 2
		$accessValidator = new Validator\Access();
50
51
		/* html element */
52
53 2
		$element = new Html\Element();
54
		$listElement = $element
55 2
			->copy()
56 2
			->init('ul',
57
			[
58 2
				'class' => $this->_optionArray['className']['list']
59
			]);
60 2
		$itemElement = $element->copy()->init('li');
61 2
		$linkElement = $element->copy()->init('a');
62 2
		$textElement = $element->copy()->init('span');
63
64
		/* query comments */
65
66
		$comments = $commentModel
67 2
			->query()
68 2
			->whereLanguageIs($this->_registry->get('language'))
69 2
			->where('status', 1)
70 2
			->orderBySetting($this->_optionArray['orderColumn'])
71 2
			->limit($this->_optionArray['limit'])
72 2
			->findMany();
73
74
		/* collect item output */
75
76 2
		foreach ($comments as $value)
77
		{
78 2
			if ($accessValidator->validate($value->access, $this->_registry->get('myGroups')))
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $this->_registry->get('myGroups') targeting Redaxscript\Registry::get() can also be of type array; however, Redaxscript\Validator\Access::validate() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
79
			{
80
				$outputItem .= $itemElement
81 2
					->copy()
82 2
					->html($linkElement
83 2
						->copy()
84 2
						->attr(
85
						[
86 2
							'href' => $this->_registry->get('parameterRoute') . $commentModel->getRouteById($value->id)
87
						])
88 2
						->text($value->author . $this->_language->get('colon') . ' ' . $value->text)
89
					);
90
			}
91
		}
92
93
		/* collect output */
94
95 2
		$output .= $listElement->html($outputItem ? : $itemElement->html($textElement->text($this->_language->get('comment_no'))));
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_language->get('comment_no') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by redaxmedia
It seems like $outputItem ?: $itemElem...ge->get('comment_no'))) can also be of type object<Redaxscript\Html\Element>; however, Redaxscript\Html\HtmlAbstract::html() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
96 2
		$output .= Module\Hook::trigger('navigationCommentEnd');
97 2
		return $output;
98
	}
99
}
100