Completed
Push — master ( ab2e37...f0a9c0 )
by Henry
13:19 queued 11:13
created

includes/Navigation/Language.php (2 issues)

Labels
Severity

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\Filesystem;
5
use Redaxscript\Html;
6
use Redaxscript\Module;
7
use function substr;
8
9
/**
10
 * children class to create the language navigation
11
 *
12
 * @since 3.3.0
13
 *
14
 * @package Redaxscript
15
 * @category Navigation
16
 * @author Henry Ruhs
17
 */
18
19
class Language 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-languages',
32
			'active' => 'rs-item-active'
33
		]
34
	];
35
36
	/**
37
	 * render the view
38
	 *
39
	 * @since 3.3.0
40
	 *
41
	 * @return string
42
	 */
43
44 3
	public function render() : string
45
	{
46 3
		$output = Module\Hook::trigger('navigationLanguageStart');
47 3
		$outputItem = null;
48
49
		/* html element */
50
51 3
		$element = new Html\Element();
52
		$listElement = $element
53 3
			->copy()
54 3
			->init('ul',
55
			[
56 3
				'class' => $this->_optionArray['className']['list']
57
			]);
58 3
		$itemElement = $element->copy()->init('li');
59 3
		$linkElement = $element->copy()->init('a');
60 3
		$textElement = $element->copy()->init('span');
61
62
		/* languages directory */
63
64 3
		$languageFilesystem = new Filesystem\Filesystem();
65 3
		$languageFilesystem->init('languages');
66 3
		$languageFilesystemArray = $languageFilesystem->getSortArray();
67
68
		/* collect item output */
69
70 3
		foreach ($languageFilesystemArray as $value)
71
		{
72 3
			$value = substr($value, 0, 2);
73
			$outputItem .= $itemElement
74 3
				->copy()
75 3
				->addClass($this->_registry->get('language') === $value ? $this->_optionArray['className']['active'] : null)
76 3
				->html($linkElement
77 3
					->copy()
78 3
					->attr(
79
					[
80 3
						'href' => $this->_registry->get('parameterRoute') . $this->_registry->get('fullRoute') . $this->_registry->get('languageRoute') . $value,
81 3
						'rel' => 'nofollow'
82
					])
83 3
					->text($this->_language->get($value, '_index'))
84
			);
85
		}
86
87
		/* collect output */
88
89 3
		$output .= $listElement->html($outputItem ? : $itemElement->html($textElement->text($this->_language->get('language_no'))));
0 ignored issues
show
It seems like $this->_language->get('language_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...
It seems like $outputItem ?: $itemElem...e->get('language_no'))) can also be of type object<Redaxscript\Html\Element>; however, Redaxscript\Html\HtmlAbstract::html() does only seem to accept null|string, 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...
90 3
		$output .= Module\Hook::trigger('navigationLanguageEnd');
91 3
		return $output;
92
	}
93
}
94