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

ArticleTable::_renderTable()   C

Complexity

Conditions 8
Paths 4

Size

Total Lines 112

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 61
CRAP Score 8.0277

Importance

Changes 0
Metric Value
dl 0
loc 112
ccs 61
cts 66
cp 0.9242
rs 6.7555
c 0
b 0
f 0
cc 8
nc 4
nop 0
crap 8.0277

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 article table
11
 *
12
 * @since 4.0.0
13
 *
14
 * @package Redaxscript
15
 * @category View
16
 * @author Henry Ruhs
17
 */
18
19
class ArticleTable 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('adminArticleTableStart');
32 2
		$parameterRoute = $this->_registry->get('parameterRoute');
33 2
		$articlesNew = $this->_registry->get('articlesNew');
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('articles'));
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('articles') 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/articles'
51
			])
52 2
			->text($this->_language->get('article_new'));
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('article_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 ($articlesNew)
58
		{
59 1
			$output .= $linkElement;
60
		}
61 2
		$output .= $this->_renderTable();
62 2
		$output .= Module\Hook::trigger('adminArticleTableEnd');
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
			'language' => $this->_language->get('language'),
85 2
			'category' => $this->_language->get('category'),
86 2
			'rank' => $this->_language->get('rank')
87
		];
88 2
		$adminControl = new Helper\Control($this->_registry, $this->_language);
89 2
		$adminControl->init();
90 2
		$categoryModel = new Admin\Model\Category();
91 2
		$articleModel = new Admin\Model\Article();
92 2
		$articles = $articleModel->getAllByOrder('rank');
93 2
		$articlesTotal = $articles->count();
94 2
		$parameterRoute = $this->_registry->get('parameterRoute');
95
96
		/* html element */
97
98 2
		$element = new Html\Element();
99
		$wrapperElement = $element
100 2
			->copy()
101 2
			->init('div',
102
			[
103 2
				'class' => 'rs-admin-wrapper-table'
104
			]);
105
		$tableElement = $element
106 2
			->copy()
107 2
			->init('table',
108
			[
109 2
				'class' => 'rs-admin-js-sort rs-admin-table-default'
110
			]);
111
		$linkElement = $element
112 2
			->copy()
113 2
			->init('a',
114
			[
115 2
				'class' => 'rs-admin-link-default'
116
			]);
117 2
		$theadElement = $element->copy()->init('thead');
118 2
		$tbodyElement = $element->copy()->init('tbody');
119 2
		$tfootElement = $element->copy()->init('tfoot');
120 2
		$trElement = $element->copy()->init('tr');
121 2
		$thElement = $element->copy()->init('th');
122 2
		$tdElement = $element->copy()->init('td');
123
124
		/* process table */
125
126 2
		foreach ($tableArray as $key => $value)
127
		{
128 2
			$outputHead .= $thElement->copy()->text($value);
0 ignored issues
show
Bug introduced by
It seems like $value defined by $value on line 126 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...
129 2
			$outputFoot .= $tdElement->copy()->text($value);
0 ignored issues
show
Bug introduced by
It seems like $value defined by $value on line 126 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...
130
		}
131
132
		/* process categories */
133
134 2
		if ($articlesTotal)
135
		{
136 2
			foreach ($articles as $key => $value)
0 ignored issues
show
Bug introduced by
The expression $articles 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...
137
			{
138
				$outputBody .= $trElement
139 2
					->copy()
140 2
					->attr('id', 'row-' . $value->id)
141 2
					->addClass(!$value->status ? 'rs-admin-is-disabled' : null)
142 2
					->html(
143 2
						$tdElement->copy()->html(
144
							$linkElement
145 2
								->attr('href', $parameterRoute . $articleModel->getRouteById($value->id))
146 2
								->text($value->title) .
147 2
							$adminControl->render('articles', $value->id, $value->alias, $value->status)
148
						) .
149 2
						$tdElement->copy()->text($value->alias) .
150 2
						$tdElement->copy()->text($value->language ? $this->_language->get($value->language, '_index') : $this->_language->get('all')) .
0 ignored issues
show
Bug introduced by
It seems like $value->language ? $this...->_language->get('all') 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...
151 2
						$tdElement->copy()->text($value->category ? $categoryModel->getById($value->category)->title : $this->_language->get('uncategorized')) .
152
						$tdElement
153 2
							->copy()
154 2
							->addClass('rs-admin-js-move rs-admin-col-move')
155 2
							->addClass($articlesTotal > 1 ? 'rs-admin-is-active' : null)
156 2
							->text($value->rank)
157
				);
158
			}
159
		}
160
		else
161
		{
162
			$outputBody .= $trElement
163
				->copy()
164
				->html(
165
					$tdElement
166
						->copy()
167
						->attr('colspan', count($tableArray))
168
						->text($this->_language->get('article_no'))
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('article_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...
169
				);
170
		}
171
172
		/* collect output */
173
174 2
		$outputHead = $theadElement->html(
175 2
			$trElement->html($outputHead)
176
		);
177 2
		$outputBody = $tbodyElement->html($outputBody);
178 2
		$outputFoot = $tfootElement->html(
179 2
			$trElement->html($outputFoot)
180
		);
181 2
		$output .= $wrapperElement->copy()->html(
182 2
			$tableElement->html($outputHead . $outputBody . $outputFoot)
183
		);
184 2
		return $output;
185
	}
186
}
187