Completed
Push — master ( aaa756...6a04f6 )
by Henry
70:00 queued 35:28
created

ModuleTable::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 10
cts 10
cp 1
rs 9.584
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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
9
/**
10
 * children class to create the admin module table
11
 *
12
 * @since 4.0.0
13
 *
14
 * @package Redaxscript
15
 * @category View
16
 * @author Henry Ruhs
17
 */
18
19
class ModuleTable extends ViewAbstract
20
{
21
	/**
22
	 * render the view
23
	 *
24
	 * @since 4.0.0
25
	 *
26
	 * @return string
27
	 */
28
29 1
	public function render() : string
30
	{
31 1
		$output = Module\Hook::trigger('adminModuleTableStart');
32
33
		/* html element */
34
35 1
		$element = new Html\Element();
36
		$titleElement = $element
37 1
			->copy()
38 1
			->init('h2',
39
			[
40 1
				'class' => 'rs-admin-title-content',
41
			])
42 1
			->text($this->_language->get('modules'));
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('modules') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
43
44
		/* collect output */
45
46 1
		$output .= $titleElement . $this->_renderTable();
47 1
		$output .= Module\Hook::trigger('adminModuleTableEnd');
48 1
		return $output;
49
	}
50
51
	/**
52
	 * render the table
53
	 *
54
	 * @since 4.0.0
55
	 *
56
	 * @return string|null
57
	 */
58
59 1
	protected function _renderTable() : ?string
60
	{
61 1
		$output = null;
62 1
		$outputHead = null;
63 1
		$outputBody = null;
64 1
		$outputFoot = null;
65
		$tableArray =
66
		[
67 1
			'name' => $this->_language->get('name'),
68 1
			'description' => $this->_language->get('description'),
69 1
			'version' => $this->_language->get('version')
70
		];
71 1
		$adminControl = new Helper\Control($this->_registry, $this->_language);
72 1
		$moduleModel = new Admin\Model\Module();
73 1
		$modules = $moduleModel->getAll();
74 1
		$modulesTotal = $modules->count();
75 1
		$modulesFilesystem = new Filesystem\Filesystem();
76 1
		$modulesFilesystem->init('modules');
77 1
		$modulesFilesystemArray = $modulesFilesystem->getSortArray();
78
79
		/* html element */
80
81 1
		$element = new Html\Element();
82
		$wrapperElement = $element
83 1
			->copy()
84 1
			->init('div',
85
			[
86 1
				'class' => 'rs-admin-wrapper-table'
87
			]);
88
		$tableElement = $element
89 1
			->copy()
90 1
			->init('table',
91
			[
92 1
				'class' => 'rs-admin-table-default'
93
			]);
94 1
		$theadElement = $element->copy()->init('thead');
95 1
		$tbodyElement = $element->copy()->init('tbody');
96 1
		$tfootElement = $element->copy()->init('tfoot');
97 1
		$trElement = $element->copy()->init('tr');
98 1
		$thElement = $element->copy()->init('th');
99 1
		$tdElement = $element->copy()->init('td');
100
101
		/* process table */
102
103 1
		foreach ($tableArray as $key => $value)
104
		{
105 1
			$outputHead .= $thElement->copy()->text($value);
0 ignored issues
show
Bug introduced by
It seems like $value defined by $value on line 103 can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
106 1
			$outputFoot .= $tdElement->copy()->text($value);
0 ignored issues
show
Bug introduced by
It seems like $value defined by $value on line 103 can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
107
		}
108
109
		/* process modules */
110
111 1
		if ($modulesTotal)
112
		{
113 1
			foreach ($modules as $key => $value)
0 ignored issues
show
Bug introduced by
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...
114
			{
115
				$outputBody .= $trElement
116 1
					->copy()
117 1
					->attr('id', 'row-' . $value->id)
118 1
					->addClass(!$value->status ? 'rs-admin-is-disabled' : null)
119 1
					->html(
120 1
						$tdElement->copy()->html($value->name . $adminControl->render('modules', $value->id, $value->alias, $value->status)) .
121 1
						$tdElement->copy()->text($value->description) .
122 1
						$tdElement->copy()->text($value->version)
123
				);
124 1
				$modulesFilesystemArray = array_diff($modulesFilesystemArray,
125
				[
126 1
					$value->alias
127
				]);
128
			}
129
		}
130 1
		if ($modulesFilesystemArray)
0 ignored issues
show
Bug Best Practice introduced by
The expression $modulesFilesystemArray of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
131
		{
132 1
			foreach ($modulesFilesystemArray as $key => $value)
133
			{
134
				$outputBody .= $trElement
135 1
					->copy()
136 1
					->html(
137
						$tdElement
138 1
							->copy()
139 1
							->attr('colspan', count($tableArray))
140 1
							->html($value . $adminControl->render('modules', null, $value, null))
141
					);
142
			}
143
		}
144 1
		if (!$modulesTotal && !$modulesFilesystemArray)
0 ignored issues
show
Bug Best Practice introduced by
The expression $modulesFilesystemArray of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
145
		{
146
			$outputBody .= $trElement
147
				->copy()
148
				->html(
149
					$tdElement
150
						->copy()
151
						->attr('colspan', count($tableArray))
152
						->text($this->_language->get('module_no'))
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('module_no') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
153
				);
154
		}
155
156
		/* collect output */
157
158 1
		$outputHead = $theadElement->html(
159 1
			$trElement->html($outputHead)
160
		);
161 1
		$outputBody = $tbodyElement->html($outputBody);
162 1
		$outputFoot = $tfootElement->html(
163 1
			$trElement->html($outputFoot)
164
		);
165 1
		$output .= $wrapperElement->copy()->html(
166 1
			$tableElement->html($outputHead . $outputBody . $outputFoot)
167
		);
168 1
		return $output;
169
	}
170
}
171