Completed
Push — master ( cbd317...231b94 )
by Henry
09:43
created

GroupTable   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 0
loc 147
ccs 65
cts 70
cp 0.9286
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 36 2
B _renderTable() 0 91 5
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 group table
11
 *
12
 * @since 4.0.0
13
 *
14
 * @package Redaxscript
15
 * @category View
16
 * @author Henry Ruhs
17
 */
18
19
class GroupTable extends ViewAbstract
20
{
21
	/**
22
	 * render the view
23
	 *
24
	 * @since 4.0.0
25
	 *
26
	 * @return string
27
	 */
28 2
29
	public function render() : string
30 2
	{
31 2
		$output = Module\Hook::trigger('adminGroupTableStart');
32 2
		$parameterRoute = $this->_registry->get('parameterRoute');
33
		$groupsNew = $this->_registry->get('groupsNew');
34
35
		/* html element */
36 2
37
		$element = new Html\Element();
38 2
		$titleElement = $element
39 2
			->copy()
40
			->init('h2',
41 2
			[
42
				'class' => 'rs-admin-title-content',
43 2
			])
44
			->text($this->_language->get('groups'));
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('groups') 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...
45 2
		$linkElement = $element
46 2
			->copy()
47
			->init('a',
48 2
			[
49 2
				'class' => 'rs-admin-button-default rs-admin-button-create',
50
				'href' => $parameterRoute . 'admin/new/groups'
51 2
			])
52
			->text($this->_language->get('group_new'));
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('group_new') 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...
53
54
		/* collect output */
55 2
56 2
		$output .= $titleElement;
57
		if ($groupsNew)
58 1
		{
59
			$output .= $linkElement;
60 2
		}
61 2
		$output .= $this->_renderTable();
62 2
		$output .= Module\Hook::trigger('adminGroupTableEnd');
63
		return $output;
64
	}
65
66
	/**
67
	 * render the table
68
	 *
69
	 * @since 4.0.0
70
	 *
71
	 * @return string|null
72
	 */
73 2
74
	protected function _renderTable() : ?string
75 2
	{
76 2
		$output = null;
77 2
		$outputHead = null;
78 2
		$outputBody = null;
79
		$outputFoot = null;
80
		$tableArray =
81 2
		[
82 2
			'name' => $this->_language->get('name'),
83 2
			'alias' => $this->_language->get('alias'),
84
			'description' => $this->_language->get('description')
85 2
		];
86 2
		$adminControl = new Helper\Control($this->_registry, $this->_language);
87 2
		$adminControl->init();
88 2
		$userModel = new Admin\Model\Group();
89
		$groups = $userModel->getAll();
90
		$groupsTotal = $groups->count();
91
92 2
		/* html element */
93
94 2
		$element = new Html\Element();
95 2
		$wrapperElement = $element
96
			->copy()
97 2
			->init('div',
98
			[
99
				'class' => 'rs-admin-wrapper-table'
100 2
			]);
101 2
		$tableElement = $element
102
			->copy()
103 2
			->init('table',
104
			[
105 2
				'class' => 'rs-admin-table-default'
106 2
			]);
107 2
		$theadElement = $element->copy()->init('thead');
108 2
		$tbodyElement = $element->copy()->init('tbody');
109 2
		$tfootElement = $element->copy()->init('tfoot');
110 2
		$trElement = $element->copy()->init('tr');
111
		$thElement = $element->copy()->init('th');
112
		$tdElement = $element->copy()->init('td');
113
114 2
		/* process table */
115
116 2
		foreach ($tableArray as $key => $value)
117 2
		{
118
			$outputHead .= $thElement->copy()->text($value);
0 ignored issues
show
Bug introduced by
It seems like $value defined by $value on line 116 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...
119
			$outputFoot .= $tdElement->copy()->text($value);
0 ignored issues
show
Bug introduced by
It seems like $value defined by $value on line 116 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...
120
		}
121
122 2
		/* process categories */
123
124 2
		if ($groupsTotal)
125
		{
126
			foreach ($groups as $key => $value)
0 ignored issues
show
Bug introduced by
The expression $groups 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...
127 2
			{
128 2
				$outputBody .= $trElement
129 2
					->copy()
130 2
					->attr('id', 'row-' . $value->id)
131 2
					->addClass(!$value->status ? 'rs-admin-is-disabled' : null)
132 2
					->html(
133 2
						$tdElement->copy()->html($value->name . $adminControl->render('groups', $value->id, $value->alias, $value->status)) .
134
						$tdElement->copy()->text($value->alias) .
135
						$tdElement->copy()->text($value->description)
136
				);
137
			}
138
		}
139
		else
140
		{
141
			$outputBody .= $trElement
142
				->copy()
143
				->html(
144
					$tdElement
145
						->copy()
146
						->attr('colspan', count($tableArray))
147
						->text($this->_language->get('group_no'))
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('group_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...
148
				);
149
		}
150
151 2
		/* collect output */
152 2
153
		$outputHead = $theadElement->html(
154 2
			$trElement->html($outputHead)
155 2
		);
156 2
		$outputBody = $tbodyElement->html($outputBody);
157
		$outputFoot = $tfootElement->html(
158 2
			$trElement->html($outputFoot)
159 2
		);
160
		$output .= $wrapperElement->copy()->html(
161 2
			$tableElement->html($outputHead . $outputBody . $outputFoot)
162
		);
163
		return $output;
164
	}
165
}
166