Completed
Push — master ( 1da492...320203 )
by Henry
07:00
created

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

Labels
Severity

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\Filesystem;
6
use Redaxscript\Html;
7
use Redaxscript\Module;
8
use function array_diff;
9
use function count;
10
use function in_array;
11
12
/**
13
 * children class to create the admin module table
14
 *
15
 * @since 4.0.0
16
 *
17
 * @package Redaxscript
18
 * @category View
19
 * @author Henry Ruhs
20
 */
21
22
class ModuleTable extends ViewAbstract
23
{
24
	/**
25
	 * render the view
26
	 *
27
	 * @since 4.0.0
28
	 *
29
	 * @return string
30
	 */
31
32
	public function render() : string
33
	{
34
		$output = Module\Hook::trigger('adminModuleTableStart');
35
36
		/* html element */
37
38
		$element = new Html\Element();
39
		$titleElement = $element
40
			->copy()
41
			->init('h2',
42
			[
43
				'class' => 'rs-admin-title-content',
44
			])
45
			->text($this->_language->get('modules'));
46
47
		/* collect output */
48
49
		$output .= $titleElement . $this->_renderTable();
50
		$output .= Module\Hook::trigger('adminModuleTableEnd');
51
		return $output;
52
	}
53
54
	/**
55
	 * render the table
56
	 *
57
	 * @since 4.0.0
58
	 *
59
	 * @return string|null
60
	 */
61
62
	protected function _renderTable() : ?string
63
	{
64
		$output = null;
65
		$outputHead = null;
66
		$outputBody = null;
67
		$outputFoot = null;
68
		$tableArray =
69
		[
70
			'name' => $this->_language->get('name'),
71
			'description' => $this->_language->get('description'),
72
			'version' => $this->_language->get('version')
73
		];
74
		$adminControl = new Helper\Control($this->_registry, $this->_language);
75
		$adminControl->init();
76
		$moduleModel = new Admin\Model\Module();
77
		$modules = $moduleModel->getAll();
78
		$modulesTotal = $modules->count();
79
		$modulesFilesystem = new Filesystem\Filesystem();
80
		$modulesFilesystem->init('modules');
81
		$modulesFilesystemArray = $modulesFilesystem->getSortArray();
82
83
		/* html element */
84
85
		$element = new Html\Element();
86
		$wrapperElement = $element
87
			->copy()
88
			->init('div',
89
			[
90
				'class' => 'rs-admin-wrapper-table'
91
			]);
92
		$tableElement = $element
93
			->copy()
94
			->init('table',
95
			[
96
				'class' => 'rs-admin-table-default'
97
			]);
98
		$theadElement = $element->copy()->init('thead');
99
		$tbodyElement = $element->copy()->init('tbody');
100
		$tfootElement = $element->copy()->init('tfoot');
101
		$trElement = $element->copy()->init('tr');
102
		$thElement = $element->copy()->init('th');
103
		$tdElement = $element->copy()->init('td');
104
105
		/* process table */
106
107
		foreach ($tableArray as $key => $value)
108
		{
109
			$outputHead .= $thElement->copy()->text($value);
110
			$outputFoot .= $tdElement->copy()->text($value);
111
		}
112
113
		/* process modules */
114
115
		if ($modulesTotal)
116
		{
117
			foreach ($modules as $key => $value)
0 ignored issues
show
The expression $modules 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...
118
			{
119
				$outputBody .= $trElement
120
					->copy()
121
					->attr('id', 'row-' . $value->id)
122
					->addClass(!$value->status ? 'rs-admin-is-disabled' : null)
123
					->addClass(!in_array($value->alias, $modulesFilesystemArray) ? 'rs-admin-is-corrupted' : null)
124
					->html(
125
						$tdElement->copy()->html($value->name . $adminControl->render('modules', $value->id, $value->alias, $value->status)) .
126
						$tdElement->copy()->text($value->description) .
127
						$tdElement->copy()->text($value->version)
128
				);
129
				$modulesFilesystemArray = array_diff($modulesFilesystemArray,
130
				[
131
					$value->alias
132
				]);
133
			}
134
		}
135
		if ($modulesFilesystemArray)
136
		{
137
			foreach ($modulesFilesystemArray as $key => $value)
138
			{
139
				$outputBody .= $trElement
140
					->copy()
141
					->html(
142
						$tdElement
143
							->copy()
144
							->attr('colspan', count($tableArray))
145
							->html($value . $adminControl->render('modules', null, $value, null))
146
					);
147
			}
148
		}
149
		if (!$modulesTotal && !$modulesFilesystemArray)
150
		{
151
			$outputBody .= $trElement
152
				->copy()
153
				->html(
154
					$tdElement
155
						->copy()
156
						->attr('colspan', count($tableArray))
157
						->text($this->_language->get('module_no'))
158
				);
159
		}
160
161
		/* collect output */
162
163
		$outputHead = $theadElement->html(
164
			$trElement->html($outputHead)
165
		);
166
		$outputBody = $tbodyElement->html($outputBody);
167
		$outputFoot = $tfootElement->html(
168
			$trElement->html($outputFoot)
169
		);
170
		$output .= $wrapperElement->copy()->html(
171
			$tableElement->html($outputHead . $outputBody . $outputFoot)
172
		);
173
		return $output;
174
	}
175
}
176