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

includes/Navigation/Category.php (3 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\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
	];
37
38
	/**
39
	 * render the view
40
	 *
41
	 * @since 3.3.0
42
	 *
43
	 * @return string
44
	 */
45
46 3
	public function render() : string
47
	{
48 3
		$output = Module\Hook::trigger('navigationCategoryStart');
49 3
		$categoryModel = new Model\Category();
50
51
		/* query categories */
52
53
		$categories = $categoryModel
54 3
			->query()
55 3
			->whereLanguageIs($this->_registry->get('language'))
56 3
			->whereNull('parent')
57 3
			->where('status', 1)
58 3
			->orderBySetting($this->_optionArray['orderColumn'])
59 3
			->limit($this->_optionArray['limit'])
60 3
			->findMany();
61
62
		/* collect output */
63
64 3
		$output .= $this->_renderList($categories);
65 3
		$output .= Module\Hook::trigger('navigationCategoryEnd');
66 3
		return $output;
67
	}
68
69
	/**
70
	 * render the list
71
	 *
72
	 * @since 3.3.0
73
	 *
74
	 * @param object $categories
75
	 * @param int $level
76
	 *
77
	 * @return string|null
78
	 */
79
80 3
	protected function _renderList(object $categories = null, int $level = 0) : ?string
81
	{
82 3
		$output = null;
83 3
		$outputItem = null;
84 3
		$categoryModel = new Model\Category();
85 3
		$accessValidator = new Validator\Access();
86
87
		/* html element */
88
89 3
		$element = new Html\Element();
90
		$listElement = $element
91 3
			->copy()
92 3
			->init('ul',
93
			[
94 3
				'class' => $level === 0 ? $this->_optionArray['className']['list'] : $this->_optionArray['className']['children']
95
			]);
96 3
		$itemElement = $element->copy()->init('li');
97 3
		$linkElement = $element->copy()->init('a');
98 3
		$textElement = $element->copy()->init('span');
99
100
		/* collect item output */
101
102 3
		foreach ($categories as $value)
0 ignored issues
show
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...
103
		{
104 3
			if ($accessValidator->validate($value->access, $this->_registry->get('myGroups')))
105
			{
106
				$outputItem .= $itemElement
107 3
					->copy()
108 3
					->addClass($this->_registry->get('firstParameter') === $value->alias ? $this->_optionArray['className']['active'] : null)
109 3
					->html($linkElement
110 3
						->copy()
111 3
						->attr(
112
						[
113 3
							'href' => $this->_registry->get('parameterRoute') . $categoryModel->getRouteById($value->id)
114
						])
115 3
						->text($value->title)
116
					)
117 3
					->append(
118 3
						$this->_renderList($categoryModel
119 3
							->query()
120 3
							->whereLanguageIs($this->_registry->get('language'))
121 3
							->where(
122
							[
123 3
								'parent' => $value->id,
124 3
								'status' => 1
125
							])
126 3
							->orderBySetting($this->_optionArray['orderColumn'])
127 3
							->limit($this->_optionArray['limit'])
128 3
							->findMany(), ++$level
129
						)
130
					);
131
			}
132
		}
133
134
		/* collect output */
135
136 3
		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...
137
		{
138 3
			$output .= $listElement->html($outputItem);
139
		}
140 3
		else if ($level === 0)
141
		{
142
			$output .= $listElement->html($itemElement->html($textElement->text($this->_language->get('category_no'))));
0 ignored issues
show
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...
143
		}
144 3
		return $output;
145
	}
146
}
147