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

includes/Admin/View/CategoryTable.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 category table
11
 *
12
 * @since 4.0.0
13
 *
14
 * @package Redaxscript
15
 * @category View
16
 * @author Henry Ruhs
17
 */
18
19
class CategoryTable 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('adminCategoryTableStart');
32 2
		$parameterRoute = $this->_registry->get('parameterRoute');
33 2
		$categoriesNew = $this->_registry->get('categoriesNew');
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('categories'));
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/categories'
51
			])
52 2
			->text($this->_language->get('category_new'));
53
54
		/* collect output */
55
56 2
		$output .= $titleElement;
57 2
		if ($categoriesNew)
58
		{
59 1
			$output .= $linkElement;
60
		}
61 2
		$output .= $this->_renderTable();
62 2
		$output .= Module\Hook::trigger('adminCategoryTableEnd');
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
			'title' => $this->_language->get('title'),
83 2
			'alias' => $this->_language->get('alias'),
84 2
			'language' => $this->_language->get('language'),
85 2
			'rank' => $this->_language->get('rank')
86
		];
87 2
		$adminControl = new Helper\Control($this->_registry, $this->_language);
88 2
		$adminControl->init();
89 2
		$categoryModel = new Admin\Model\Category();
90 2
		$categories = $categoryModel->getAllByOrder('rank');
91 2
		$categoriesTotal = $categories->count();
92 2
		$parameterRoute = $this->_registry->get('parameterRoute');
93
94
		/* html element */
95
96 2
		$element = new Html\Element();
97
		$wrapperElement = $element
98 2
			->copy()
99 2
			->init('div',
100
			[
101 2
				'class' => 'rs-admin-wrapper-table'
102
			]);
103
		$tableElement = $element
104 2
			->copy()
105 2
			->init('table',
106
			[
107 2
				'class' => 'rs-admin-js-sort rs-admin-table-default'
108
			]);
109
		$linkElement = $element
110 2
			->copy()
111 2
			->init('a',
112
			[
113 2
				'class' => 'rs-admin-link-default'
114
			]);
115 2
		$theadElement = $element->copy()->init('thead');
116 2
		$tbodyElement = $element->copy()->init('tbody');
117 2
		$tfootElement = $element->copy()->init('tfoot');
118 2
		$trElement = $element->copy()->init('tr');
119 2
		$thElement = $element->copy()->init('th');
120 2
		$tdElement = $element->copy()->init('td');
121
122
		/* process table */
123
124 2
		foreach ($tableArray as $key => $value)
125
		{
126 2
			$outputHead .= $thElement->copy()->text($value);
127 2
			$outputFoot .= $tdElement->copy()->text($value);
128
		}
129
130
		/* process categories */
131
132 2
		if ($categoriesTotal)
133
		{
134 2
			foreach ($categories as $key => $value)
0 ignored issues
show
The expression $categories 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...
135
			{
136
				$outputBody .= $trElement
137 2
					->copy()
138 2
					->attr('id', 'row-' . $value->id)
139 2
					->addClass($value->parent ? 'rs-admin-is-children' : null)
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 . $categoryModel->getRouteById($value->id))
145 2
								->text($value->title) .
146 2
							$adminControl->render('categories', $value->id, $value->alias, $value->status)
147
						) .
148 2
						$tdElement->copy()->text($value->alias) .
149 2
						$tdElement->copy()->text($value->language ? $this->_language->get($value->language, '_index') : $this->_language->get('all')) .
150
						$tdElement
151 2
							->copy()
152 2
							->addClass('rs-admin-js-move rs-admin-col-move')
153 2
							->addClass($categoriesTotal > 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('category_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