Completed
Push — master ( 0ea243...da58d4 )
by Henry
10:25 queued 33s
created

includes/View/Comment.php (7 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
use function array_replace_recursive;
10
11
/**
12
 * children class to create the comment
13
 *
14
 * @since 4.0.0
15
 *
16
 * @package Redaxscript
17
 * @category View
18
 * @author Henry Ruhs
19
 */
20
21
class Comment extends ViewAbstract
22
{
23
	/**
24
	 * options of the comment
25
	 *
26
	 * @var array
27
	 */
28
29
	protected $_optionArray =
30
	[
31
		'tag' =>
32
		[
33
			'title' => 'h3',
34
			'box' => 'blockquote'
35
		],
36
		'className' =>
37
		[
38
			'title' => 'rs-title-comment',
39
			'box' => 'rs-quote-default'
40
		],
41
		'orderColumn' => 'rank'
42
	];
43
44
	/**
45
	 * stringify the comment
46
	 *
47
	 * @since 4.0.0
48
	 *
49
	 * @return string
50
	 */
51
52
	public function __toString() : string
53
	{
54
		return $this->render();
55
	}
56
57
	/**
58
	 * init the class
59
	 *
60
	 * @since 4.0.0
61
	 *
62
	 * @param array $optionArray options of the comment
63
	 */
64
65
	public function init(array $optionArray = []) : void
66
	{
67
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
68
	}
69
70
	/**
71
	 * render the view
72
	 *
73
	 * @since 4.0.0
74
	 *
75
	 * @param int $articleId identifier of the article
76
	 *
77
	 * @return string
78
	 */
79
80
	public function render(int $articleId = null) : string
81
	{
82
		if ($this->_registry->get('commentReplace'))
83
		{
84
			return Module\Hook::trigger('commentReplace');
85
		}
86
		$output = Module\Hook::trigger('commentStart');
87
		$accessValidator = new Validator\Access();
88
		$byline = new Helper\Byline($this->_registry, $this->_language);
89
		$byline->init();
90
		$adminDock = new Admin\View\Helper\Dock($this->_registry, $this->_language);
91
		$adminDock->init();
92
		$loggedIn = $this->_registry->get('loggedIn');
93
		$token = $this->_registry->get('token');
94
		$firstParameter = $this->_registry->get('firstParameter');
95
		$myGroups = $this->_registry->get('myGroups');
96
97
		/* html element */
98
99
		$element = new Html\Element();
100
		$titleElement = $element
101
			->copy()
102
			->init($this->_optionArray['tag']['title'],
103
			[
104
				'class' => $this->_optionArray['className']['title']
105
			]);
106
		$linkElement = $element->copy()->init('a',
107
			[
108
				'rel' => 'nofollow'
109
			]);
110
		$boxElement = $element
111
			->copy()
112
			->init($this->_optionArray['tag']['box'],
113
			[
114
				'class' => $this->_optionArray['className']['box']
115
			]);
116
		$comments = $this->queryComments($articleId);
117
118
		/* process comments */
119
120
		foreach ($comments as $value)
0 ignored issues
show
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...
121
		{
122
			if ($accessValidator->validate($value->access, $myGroups))
0 ignored issues
show
It seems like $myGroups defined by $this->_registry->get('myGroups') on line 95 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...
123
			{
124
				$output .= Module\Hook::trigger('commentFragmentStart', (array)$value);
125
				$output .= $titleElement
126
					->attr('id', 'comment-' . $value->id)
127
					->html($value->url ? $linkElement
128
						->attr('href', $value->url)
129
						->text($value->author) : $value->author
130
					);
131
				$output .= $boxElement->text($value->text) . $byline->render($value->date) . Module\Hook::trigger('commentFragmentEnd', (array)$value);
132
133
				/* admin dock */
134
135
				if ($loggedIn === $token && $firstParameter !== 'logout')
136
				{
137
					$output .= $adminDock->render('comments', $value->id);
138
				}
139
			}
140
		}
141
		$output .= Module\Hook::trigger('commentEnd');
142
		return $output;
143
	}
144
145
	/**
146
	 * query the comments
147
	 *
148
	 * @since 4.0.0
149
	 *
150
	 * @param int $articleId identifier of the article
151
	 *
152
	 * @return object|null
153
	 */
154
155
	public function queryComments(int $articleId = null) : ?object
156
	{
157
		$commentModel = new Model\Comment();
158
		$settingModel = new Model\Setting();
159
		$lastSubParameter = $this->_registry->get('lastSubParameter');
160
		$language = $this->_registry->get('language');
161
162
		/* query comments */
163
164
		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...
165
		{
166
			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...
167
			{
168
				return $commentModel->getByArticleAndLanguageAndOrderAndStep($articleId, $language, $this->_optionArray['orderColumn'], $lastSubParameter - 1);
0 ignored issues
show
It seems like $language defined by $this->_registry->get('language') on line 160 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...
169
			}
170
			return $commentModel->getByArticleAndLanguageAndOrder($articleId, $language, $this->_optionArray['orderColumn']);
0 ignored issues
show
It seems like $language defined by $this->_registry->get('language') on line 160 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...
171
		}
172
		return $commentModel->getByLanguageAndOrder($language, $this->_optionArray['orderColumn']);
0 ignored issues
show
It seems like $language defined by $this->_registry->get('language') on line 160 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...
173
	}
174
}
175