Completed
Push — master ( a52438...bf8826 )
by Henry
06:30
created

includes/View/Comment.php (1 issue)

Check for loose comparison of integers.

Best Practice Bug Major

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' => 'div'
35
		],
36
		'className' =>
37
		[
38
			'title' => 'rs-title-comment',
39
			'box' => 'rs-box-comment'
40
		],
41
		'orderColumn' => 'rank'
42
	];
43
44
	/**
45
	 * init the class
46
	 *
47
	 * @since 4.0.0
48
	 *
49
	 * @param array $optionArray options of the comment
50
	 *
51
	 * @return self
52
	 */
53
54 4
	public function init(array $optionArray = []) : self
55
	{
56 4
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
57 4
		return $this;
58
	}
59
60
	/**
61
	 * render the view
62
	 *
63
	 * @since 4.0.0
64
	 *
65
	 * @param int $articleId identifier of the article
66
	 *
67
	 * @return string
68
	 */
69
70 4
	public function render(int $articleId = null) : string
71
	{
72 4
		if ($this->_registry->get('commentReplace'))
73
		{
74
			return Module\Hook::trigger('commentReplace');
75
		}
76 4
		$output = Module\Hook::trigger('commentStart');
77 4
		$accessValidator = new Validator\Access();
78 4
		$byline = new Helper\Byline($this->_registry, $this->_language);
79 4
		$byline->init();
80 4
		$adminDock = new Admin\View\Helper\Dock($this->_registry, $this->_language);
81 4
		$adminDock->init();
82 4
		$loggedIn = $this->_registry->get('loggedIn');
83 4
		$token = $this->_registry->get('token');
84 4
		$firstParameter = $this->_registry->get('firstParameter');
85 4
		$myGroups = $this->_registry->get('myGroups');
86
87
		/* html element */
88
89 4
		$element = new Html\Element();
90
		$titleElement = $element
91 4
			->copy()
92 4
			->init($this->_optionArray['tag']['title'],
93
			[
94 4
				'class' => $this->_optionArray['className']['title']
95
			]);
96
		$linkElement = $element
97 4
			->copy()
98 4
			->init('a',
99
			[
100 4
				'rel' => 'nofollow'
101
			]);
102
		$boxElement = $element
103 4
			->copy()
104 4
			->init($this->_optionArray['tag']['box'],
105
			[
106 4
				'class' => $this->_optionArray['className']['box']
107
			]);
108 4
		$comments = $this->queryComments($articleId);
109
110
		/* process comments */
111
112 4
		foreach ($comments as $value)
113
		{
114 4
			if ($accessValidator->validate($value->access, $myGroups))
115
			{
116 4
				$output .= Module\Hook::trigger('commentFragmentStart', (array)$value);
117
				$output .= $titleElement
118 4
					->attr('id', 'comment-' . $value->id)
119 4
					->html($value->url ? $linkElement
120
						->attr('href', $value->url)
121 4
						->text($value->author) : $value->author
122
					);
123 4
				$output .= $boxElement->html($value->text) . $byline->render($value->date) . Module\Hook::trigger('commentFragmentEnd', (array)$value);
124
125
				/* admin dock */
126
127 4
				if ($loggedIn === $token && $firstParameter !== 'logout')
128
				{
129 1
					$output .= $adminDock->render('comments', $value->id);
130
				}
131
			}
132
		}
133 4
		$output .= Module\Hook::trigger('commentEnd');
134 4
		return $output;
135
	}
136
137
	/**
138
	 * query the comments
139
	 *
140
	 * @since 4.0.0
141
	 *
142
	 * @param int $articleId identifier of the article
143
	 *
144
	 * @return object|null
145
	 */
146
147 4
	public function queryComments(int $articleId = null) : ?object
148
	{
149 4
		$commentModel = new Model\Comment();
150 4
		$settingModel = new Model\Setting();
151 4
		$lastSubParameter = $this->_registry->get('lastSubParameter');
152 4
		$language = $this->_registry->get('language');
153
154
		/* query comments */
155
156 4
		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...
157
		{
158 2
			$limitStep = $settingModel->get('pagination') ? $lastSubParameter - 1 : null;
159 2
			return $commentModel->getByArticleAndLanguageAndOrderAndStep($articleId, $language, $this->_optionArray['orderColumn'], $limitStep);
160
		}
161 2
		return $commentModel->getByLanguageAndOrder($language, $this->_optionArray['orderColumn']);
162
	}
163
}
164