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

includes/Admin/View/ModuleTable.php (7 issues)

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