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