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

includes/Admin/View/CommentTable.php (1 issue)

arguments passed to foreach are traversable.

Bug Minor

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\Admin\View;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Html;
6
use Redaxscript\Module;
7
use function count;
8
9
/**
10
 * children class to create the admin comment table
11
 *
12
 * @since 4.0.0
13
 *
14
 * @package Redaxscript
15
 * @category View
16
 * @author Henry Ruhs
17
 */
18
19
class CommentTable extends ViewAbstract
20
{
21
	/**
22
	 * render the view
23
	 *
24
	 * @since 4.0.0
25
	 *
26
	 * @return string
27
	 */
28
29 2
	public function render() : string
30
	{
31 2
		$output = Module\Hook::trigger('adminCommentTableStart');
32 2
		$parameterRoute = $this->_registry->get('parameterRoute');
33 2
		$commentsNew = $this->_registry->get('commentsNew');
34
35
		/* html element */
36
37 2
		$element = new Html\Element();
38
		$titleElement = $element
39 2
			->copy()
40 2
			->init('h2',
41
			[
42 2
				'class' => 'rs-admin-title-content',
43
			])
44 2
			->text($this->_language->get('comments'));
45
		$linkElement = $element
46 2
			->copy()
47 2
			->init('a',
48
			[
49 2
				'class' => 'rs-admin-button-default rs-admin-button-create',
50 2
				'href' => $parameterRoute . 'admin/new/comments'
51
			])
52 2
			->text($this->_language->get('comment_new'));
53
54
		/* collect output */
55
56 2
		$output .= $titleElement;
57 2
		if ($commentsNew)
58
		{
59 1
			$output .= $linkElement;
60
		}
61 2
		$output .= $this->_renderTable();
62 2
		$output .= Module\Hook::trigger('adminCommentTableEnd');
63 2
		return $output;
64
	}
65
66
	/**
67
	 * render the table
68
	 *
69
	 * @since 4.0.0
70
	 *
71
	 * @return string|null
72
	 */
73
74 2
	protected function _renderTable() : ?string
75
	{
76 2
		$output = null;
77 2
		$outputHead = null;
78 2
		$outputBody = null;
79 2
		$outputFoot = null;
80
		$tableArray =
81
		[
82 2
			'author' => $this->_language->get('author'),
83 2
			'language' => $this->_language->get('language'),
84 2
			'article' => $this->_language->get('article'),
85 2
			'rank' => $this->_language->get('rank')
86
		];
87 2
		$adminControl = new Helper\Control($this->_registry, $this->_language);
88 2
		$adminControl->init();
89 2
		$articleModel = new Admin\Model\Article();
90 2
		$commentModel = new Admin\Model\Comment();
91 2
		$comments = $commentModel->getAllByOrder('rank');
92 2
		$commentsTotal = $comments->count();
93 2
		$parameterRoute = $this->_registry->get('parameterRoute');
94
95
		/* html element */
96
97 2
		$element = new Html\Element();
98
		$wrapperElement = $element
99 2
			->copy()
100 2
			->init('div',
101
			[
102 2
				'class' => 'rs-admin-wrapper-table'
103
			]);
104
		$tableElement = $element
105 2
			->copy()
106 2
			->init('table',
107
			[
108 2
				'class' => 'rs-admin-js-sort rs-admin-table-default'
109
			]);
110
		$linkElement = $element
111 2
			->copy()
112 2
			->init('a',
113
			[
114 2
				'class' => 'rs-admin-link-default'
115
			]);
116 2
		$theadElement = $element->copy()->init('thead');
117 2
		$tbodyElement = $element->copy()->init('tbody');
118 2
		$tfootElement = $element->copy()->init('tfoot');
119 2
		$trElement = $element->copy()->init('tr');
120 2
		$thElement = $element->copy()->init('th');
121 2
		$tdElement = $element->copy()->init('td');
122
123
		/* process table */
124
125 2
		foreach ($tableArray as $key => $value)
126
		{
127 2
			$outputHead .= $thElement->copy()->text($value);
128 2
			$outputFoot .= $tdElement->copy()->text($value);
129
		}
130
131
		/* process categories */
132
133 2
		if ($commentsTotal)
134
		{
135 2
			foreach ($comments as $key => $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...
136
			{
137
				$outputBody .= $trElement
138 2
					->copy()
139 2
					->attr('id', 'row-' . $value->id)
140 2
					->addClass(!$value->status ? 'rs-admin-is-disabled' : null)
141 2
					->html(
142 2
						$tdElement->copy()->html(
143
							$linkElement
144 2
								->attr('href', $parameterRoute . $commentModel->getRouteById($value->id))
145 2
								->text($value->author) .
146 2
							$adminControl->render('comments', $value->id, $value->alias, $value->status)
147
						) .
148 2
						$tdElement->copy()->text($value->language ? $this->_language->get($value->language, '_index') : $this->_language->get('all')) .
149 2
						$tdElement->copy()->text($value->article ? $articleModel->getById($value->article)->title : $this->_language->get('none')) .
150
						$tdElement
151 2
							->copy()
152 2
							->addClass('rs-admin-js-move rs-admin-col-move')
153 2
							->addClass($commentsTotal > 1 ? 'rs-admin-is-active' : null)
154 2
							->text($value->rank)
155
				);
156
			}
157
		}
158
		else
159
		{
160
			$outputBody .= $trElement
161
				->copy()
162
				->html(
163
					$tdElement
164
						->copy()
165
						->attr('colspan', count($tableArray))
166
						->text($this->_language->get('comment_no'))
167
				);
168
		}
169
170
		/* collect output */
171
172 2
		$outputHead = $theadElement->html(
173 2
			$trElement->html($outputHead)
174
		);
175 2
		$outputBody = $tbodyElement->html($outputBody);
176 2
		$outputFoot = $tfootElement->html(
177 2
			$trElement->html($outputFoot)
178
		);
179 2
		$output .= $wrapperElement->copy()->html(
180 2
			$tableElement->html($outputHead . $outputBody . $outputFoot)
181
		);
182 2
		return $output;
183
	}
184
}
185