Completed
Push — master ( f598b1...764fcb )
by Henry
117:58
created

Comment::render()   C

Complexity

Conditions 9
Paths 13

Size

Total Lines 86

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 0
loc 86
ccs 0
cts 44
cp 0
rs 6.7498
c 0
b 0
f 0
cc 9
nc 13
nop 1
crap 90

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\View;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Html;
6
use Redaxscript\Model;
7
use Redaxscript\Module;
8
use Redaxscript\Validator;
9
10
/**
11
 * children class to create the comment
12
 *
13
 * @since 4.0.0
14
 *
15
 * @package Redaxscript
16
 * @category View
17
 * @author Henry Ruhs
18
 */
19
20
class Comment extends ViewAbstract
21
{
22
	/**
23
	 * options of the comment
24
	 *
25
	 * @var array
26
	 */
27
28
	protected $_optionArray =
29
	[
30
		'tag' =>
31
		[
32
			'title' => 'h3',
33
			'box' => 'blockquote'
34
		],
35
		'className' =>
36
		[
37
			'title' => 'rs-title-comment',
38
			'box' => 'rs-quote-default'
39
		],
40
		'orderColumn' => 'rank'
41
	];
42
43
	/**
44
	 * stringify the comment
45
	 *
46
	 * @since 4.0.0
47
	 *
48
	 * @return string
49
	 */
50
51
	public function __toString() : string
52
	{
53
		return $this->render();
54
	}
55
56
	/**
57
	 * init the class
58
	 *
59
	 * @since 4.0.0
60
	 *
61
	 * @param array $optionArray options of the comment
62
	 */
63
64
	public function init(array $optionArray = [])
65
	{
66
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
67
	}
68
69
	/**
70
	 * render the view
71
	 *
72
	 * @since 4.0.0
73
	 *
74
	 * @param int $articleId identifier of the article
75
	 *
76
	 * @return string
77
	 */
78
79
	public function render(int $articleId = null) : string
80
	{
81
		if ($this->_registry->get('commentReplace'))
82
		{
83
			return Module\Hook::trigger('commentReplace');
84
		}
85
		$output = Module\Hook::trigger('commentStart');
86
		$accessValidator = new Validator\Access();
87
		$settingModel = new Model\Setting();
88
		$commentModel = new Model\Comment();
89
		$comments = null;
0 ignored issues
show
Unused Code introduced by
$comments is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
90
		$byline = new Helper\Byline($this->_registry, $this->_language);
91
		$byline->init();
92
		$adminDock = new Admin\View\Helper\Dock($this->_registry, $this->_language);
93
		$adminDock->init();
94
		$language = $this->_registry->get('language');
95
		$loggedIn = $this->_registry->get('loggedIn');
96
		$token = $this->_registry->get('token');
97
		$firstParameter = $this->_registry->get('firstParameter');
98
		$lastSubParameter = $this->_registry->get('lastSubParameter');
99
		$myGroups = $this->_registry->get('myGroups');
100
101
		/* html element */
102
103
		$element = new Html\Element();
104
		$titleElement = $element
105
			->copy()
106
			->init($this->_optionArray['tag']['title'],
107
			[
108
				'class' => $this->_optionArray['className']['title']
109
			]);
110
		$linkElement = $element->copy()->init('a',
111
			[
112
				'rel' => 'nofollow'
113
			]);
114
		$boxElement = $element
115
			->copy()
116
			->init($this->_optionArray['tag']['box'],
117
			[
118
				'class' => $this->_optionArray['className']['box']
119
			]);
120
121
		/* query comments */
122
123
		if ($articleId)
0 ignored issues
show
Bug Best Practice introduced by
The expression $articleId of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
124
		{
125
			if ($settingModel->get('pagination'))
0 ignored issues
show
Bug Best Practice introduced by
The expression $settingModel->get('pagination') of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
126
			{
127
				$comments = $commentModel->getByArticleAndLanguageAndOrderAndStep($articleId, $language, $this->_optionArray['orderColumn'], $lastSubParameter - 1);
0 ignored issues
show
Bug introduced by
It seems like $language defined by $this->_registry->get('language') on line 94 can also be of type array; however, Redaxscript\Model\Commen...nguageAndOrderAndStep() 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...
128
			}
129
			else
130
			{
131
				$comments = $commentModel->getByArticleAndLanguageAndOrder($articleId, $language, $this->_optionArray['orderColumn']);
0 ignored issues
show
Bug introduced by
It seems like $language defined by $this->_registry->get('language') on line 94 can also be of type array; however, Redaxscript\Model\Commen...leAndLanguageAndOrder() 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...
132
			}
133
		}
134
		else
135
		{
136
			$comments = $commentModel->getByLanguageAndOrder($language, $this->_optionArray['orderColumn']);
0 ignored issues
show
Bug introduced by
It seems like $language defined by $this->_registry->get('language') on line 94 can also be of type array; however, Redaxscript\Model\Conten...getByLanguageAndOrder() 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...
137
		}
138
139
		/* process comments */
140
141
		foreach ($comments as $value)
0 ignored issues
show
Bug introduced by
The expression $comments of type object|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
142
		{
143
			if ($accessValidator->validate($value->access, $myGroups))
0 ignored issues
show
Bug introduced by
It seems like $myGroups defined by $this->_registry->get('myGroups') on line 99 can also be of type array; however, Redaxscript\Validator\Access::validate() 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...
144
			{
145
				$output .= Module\Hook::trigger('commentFragmentStart', (array)$value);
146
				$output .= $titleElement
147
					->attr('id', 'comment-' . $value->id)
148
					->html($value->url ? $linkElement
149
						->attr('href', $value->url)
150
						->text($value->author) : $value->author
151
					);
152
				$output .= $boxElement->text($value->text) . $byline->render($value->date) . Module\Hook::trigger('commentFragmentEnd', (array)$value);
153
154
				/* admin dock */
155
156
				if ($loggedIn === $token && $firstParameter !== 'logout')
157
				{
158
					$output .= $adminDock->render('comments', $value->id);
159
				}
160
			}
161
		}
162
		$output .= Module\Hook::trigger('commentEnd');
163
		return $output;
164
	}
165
}
166