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

includes/Admin/View/ExtraTable.php (6 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\Html;
6
use Redaxscript\Module;
7
use function count;
8
9
/**
10
 * children class to create the admin extra table
11
 *
12
 * @since 4.0.0
13
 *
14
 * @package Redaxscript
15
 * @category View
16
 * @author Henry Ruhs
17
 */
18
19
class ExtraTable extends ViewAbstract
20
{
21
	/**
22
	 * render the view
23
	 *
24
	 * @since 4.0.0
25
	 *
26
	 * @return string
27
	 */
28
29 2
	public function render() : string
30
	{
31 2
		$output = Module\Hook::trigger('adminExtraTableStart');
32 2
		$parameterRoute = $this->_registry->get('parameterRoute');
33 2
		$extrasNew = $this->_registry->get('extrasNew');
34
35
		/* html element */
36
37 2
		$element = new Html\Element();
38
		$titleElement = $element
39 2
			->copy()
40 2
			->init('h2',
41
			[
42 2
				'class' => 'rs-admin-title-content',
43
			])
44 2
			->text($this->_language->get('extras'));
0 ignored issues
show
It seems like $this->_language->get('extras') 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
		$linkElement = $element
46 2
			->copy()
47 2
			->init('a',
48
			[
49 2
				'class' => 'rs-admin-button-default rs-admin-button-create',
50 2
				'href' => $parameterRoute . 'admin/new/extras'
51
			])
52 2
			->text($this->_language->get('extra_new'));
0 ignored issues
show
It seems like $this->_language->get('extra_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
56 2
		$output .= $titleElement;
57 2
		if ($extrasNew)
58
		{
59 1
			$output .= $linkElement;
60
		}
61 2
		$output .= $this->_renderTable();
62 2
		$output .= Module\Hook::trigger('adminExtraTableEnd');
63 2
		return $output;
64
	}
65
66
	/**
67
	 * render the table
68
	 *
69
	 * @since 4.0.0
70
	 *
71
	 * @return string|null
72
	 */
73
74 2
	protected function _renderTable() : ?string
75
	{
76 2
		$output = null;
77 2
		$outputHead = null;
78 2
		$outputBody = null;
79 2
		$outputFoot = null;
80
		$tableArray =
81
		[
82 2
			'title' => $this->_language->get('title'),
83 2
			'alias' => $this->_language->get('alias'),
84 2
			'rank' => $this->_language->get('rank')
85
		];
86 2
		$adminControl = new Helper\Control($this->_registry, $this->_language);
87 2
		$adminControl->init();
88 2
		$extraModel = new Admin\Model\Extra();
89 2
		$extras = $extraModel->getAllByOrder('rank');
90 2
		$extrasTotal = $extras->count();
91
92
		/* html element */
93
94 2
		$element = new Html\Element();
95
		$wrapperElement = $element
96 2
			->copy()
97 2
			->init('div',
98
			[
99 2
				'class' => 'rs-admin-wrapper-table'
100
			]);
101
		$tableElement = $element
102 2
			->copy()
103 2
			->init('table',
104
			[
105 2
				'class' => 'rs-admin-js-sort rs-admin-table-default'
106
			]);
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 2
		$thElement = $element->copy()->init('th');
112 2
		$tdElement = $element->copy()->init('td');
113
114
		/* process table */
115
116 2
		foreach ($tableArray as $key => $value)
117
		{
118 2
			$outputHead .= $thElement->copy()->text($value);
0 ignored issues
show
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 2
			$outputFoot .= $tdElement->copy()->text($value);
0 ignored issues
show
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
		/* process extras */
123
124 2
		if ($extrasTotal)
125
		{
126 2
			foreach ($extras as $key => $value)
0 ignored issues
show
The expression $extras 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...
127
			{
128
				$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->title . $adminControl->render('extras', $value->id, $value->alias, $value->status)) .
134 2
						$tdElement->copy()->text($value->alias) .
135
						$tdElement
136 2
							->copy()
137 2
							->addClass('rs-admin-js-move rs-admin-col-move')
138 2
							->addClass($extrasTotal > 1 ? 'rs-admin-is-active' : null)
139 2
							->text($value->rank)
140
				);
141
			}
142
		}
143
		else
144
		{
145
			$outputBody .= $trElement
146
				->copy()
147
				->html(
148
					$tdElement
149
						->copy()
150
						->attr('colspan', count($tableArray))
151
						->text($this->_language->get('extra_no'))
0 ignored issues
show
It seems like $this->_language->get('extra_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...
152
				);
153
		}
154
155
		/* collect output */
156
157 2
		$outputHead = $theadElement->html(
158 2
			$trElement->html($outputHead)
159
		);
160 2
		$outputBody = $tbodyElement->html($outputBody);
161 2
		$outputFoot = $tfootElement->html(
162 2
			$trElement->html($outputFoot)
163
		);
164 2
		$output .= $wrapperElement->copy()->html(
165 2
			$tableElement->html($outputHead . $outputBody . $outputFoot)
166
		);
167 2
		return $output;
168
	}
169
}
170