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

Category   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 8
dl 0
loc 126
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 21 1
B renderList() 0 64 8
1
<?php
2
namespace Redaxscript\Navigation;
3
4
use Redaxscript\Html;
5
use Redaxscript\Model;
6
use Redaxscript\Module;
7
use Redaxscript\Validator;
8
9
/**
10
 * children class to create the category navigation
11
 *
12
 * @since 3.3.0
13
 *
14
 * @package Redaxscript
15
 * @category Navigation
16
 * @author Henry Ruhs
17
 */
18
19
class Category extends NavigationAbstract
20
{
21
	/**
22
	 * options of the navigation
23
	 *
24
	 * @var array
25
	 */
26
27
	protected $_optionArray =
28
	[
29
		'className' =>
30
		[
31
			'list' => 'rs-list-categories',
32
			'children' => 'rs-list-children',
33
			'active' => 'rs-item-active'
34
		],
35
		'orderColumn' => 'rank',
36
		'parentId' => 0
37
	];
38
39
	/**
40
	 * render the view
41
	 *
42
	 * @since 3.3.0
43
	 *
44
	 * @return string
45
	 */
46
47 4
	public function render() : string
48
	{
49 4
		$output = Module\Hook::trigger('navigationCategoryStart');
50 4
		$categoryModel = new Model\Category();
51
52
		/* query categories */
53
54
		$categories = $categoryModel
55 4
			->query()
56 4
			->whereLanguageIs($this->_registry->get('language'))
0 ignored issues
show
Bug introduced by
It seems like $this->_registry->get('language') targeting Redaxscript\Registry::get() can also be of type array; however, Redaxscript\Db::whereLanguageIs() does only seem to accept null|string, 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...
57 4
			->where('status', 1)
58 4
			->orderBySetting($this->_optionArray['orderColumn'])
59 4
			->limit($this->_optionArray['limit'])
60 4
			->findMany();
61
62
		/* collect output */
63
64 4
		$output .= $this->renderList($categories, $this->_optionArray);
65 4
		$output .= Module\Hook::trigger('navigationCategoryEnd');
66 4
		return $output;
67
	}
68
69
	/**
70
	 * render the list
71
	 *
72
	 * @since 3.3.0
73
	 *
74
	 * @param object $categories
75
	 * @param array $optionArray
76
	 *
77
	 * @return string|null
78
	 */
79
80 4
	protected function renderList(object $categories = null, array $optionArray = []) : ?string
81
	{
82 4
		$output = null;
83 4
		$outputItem = null;
84 4
		$categoryModel = new Model\Category();
85 4
		$accessValidator = new Validator\Access();
86
87
		/* html element */
88
89 4
		$element = new Html\Element();
90
		$listElement = $element
91 4
			->copy()
92 4
			->init('ul',
93
			[
94 4
				'class' => $optionArray['className']['list']
95
			]);
96 4
		$itemElement = $element->copy()->init('li');
97 4
		$linkElement = $element->copy()->init('a');
98
99
		/* collect item output */
100
101 4
		foreach ($categories as $value)
0 ignored issues
show
Bug introduced by
The expression $categories of type null|object<Redaxscript\Navigation\object> 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...
102
		{
103 4
			if ($accessValidator->validate($value->access, $this->_registry->get('myGroups')) && $optionArray['parentId'] === (int)$value->parent)
0 ignored issues
show
Bug introduced by
It seems like $this->_registry->get('myGroups') targeting Redaxscript\Registry::get() can also be of type array; however, Redaxscript\Validator\Access::validate() does only seem to accept null|string, 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...
104
			{
105
				$outputItem .= $itemElement
106 4
					->copy()
107 4
					->addClass((int)$this->_registry->get('categoryId') === (int)$value->id ? $this->_optionArray['className']['active'] : null)
108 4
					->html($linkElement
109 4
						->copy()
110 4
						->attr(
111
						[
112 4
							'href' => $this->_registry->get('parameterRoute') . $categoryModel->getRouteById($value->id)
113
						])
114 4
						->text($value->title)
115
					)
116 4
					->append(
117 4
						$this->renderList($categories,
118
						[
119
							'className' =>
120
							[
121 4
								'list' => $value->parent ? $optionArray['className']['list'] : $optionArray['className']['children'],
122 4
								'active' => $optionArray['className']['active'],
123
							],
124 4
							'parentId' => (int)$value->id
125
						])
126
					);
127
			}
128
		}
129
130
		/* collect output */
131
132 4
		if ($outputItem)
0 ignored issues
show
Bug Best Practice introduced by
The expression $outputItem of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
133
		{
134 4
			$output .= $listElement->html($outputItem);
135
		}
136 4
		else if (!$categories->count())
137
		{
138
			$output .= $listElement->html(
139
				$itemElement->text($this->_language->get('category_no'))
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('category_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...
140
			);
141
		}
142
		return $output;
143
	}
144
}
145