Completed
Push — master ( e9b1ca...cbd317 )
by Henry
07:52
created

Template::render()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 24
cts 24
cp 1
rs 9.0472
c 0
b 0
f 0
cc 4
nc 2
nop 0
crap 4

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\Navigation;
3
4
use Redaxscript\Filesystem;
5
use Redaxscript\Html;
6
use Redaxscript\Module;
7
8
/**
9
 * children class to create the language navigation
10
 *
11
 * @since 3.3.0
12
 *
13
 * @package Redaxscript
14
 * @category Navigation
15
 * @author Henry Ruhs
16
 */
17
18
class Template extends NavigationAbstract
19
{
20
	/**
21
	 * options of the navigation
22
	 *
23
	 * @var array
24
	 */
25
26
	protected $_optionArray =
27
	[
28
		'className' =>
29
		[
30
			'list' => 'rs-list-templates',
31
			'active' => 'rs-item-active'
32
		]
33
	];
34
35
	/**
36
	 * render the view
37
	 *
38
	 * @since 3.3.0
39
	 *
40
	 * @return string
41
	 */
42
43 3
	public function render() : string
44
	{
45 3
		$output = Module\Hook::trigger('navigationTemplateStart');
46 3
		$outputItem = null;
47
48
		/* html element */
49
50 3
		$element = new Html\Element();
51
		$listElement = $element
52 3
			->copy()
53 3
			->init('ul',
54
			[
55 3
				'class' => $this->_optionArray['className']['list']
56
			]);
57 3
		$itemElement = $element->copy()->init('li');
58 3
		$linkElement = $element->copy()->init('a');
59
60
		/* template directory */
61
62 3
		$templateFilesystem = new Filesystem\Filesystem();
63 3
		$templateFilesystem->init('templates', false,
64
		[
65 3
			'admin',
66
			'console',
67
			'install'
68
		]);
69 3
		$templateFilesystemArray = $templateFilesystem->getSortArray();
70
71
		/* collect item output */
72
73 3
		foreach ($templateFilesystemArray as $value)
74
		{
75
			$outputItem .= $itemElement
76 3
				->copy()
77 3
				->addClass($this->_registry->get('template') === $value ? $this->_optionArray['className']['active'] : null)
78 3
				->html($linkElement
79 3
					->copy()
80 3
					->attr(
81
					[
82 3
						'href' => $this->_registry->get('parameterRoute') . $this->_registry->get('fullRoute') . $this->_registry->get('templateRoute') . $value,
83 3
						'rel' => 'nofollow'
84
					])
85 3
					->text($value)
86
				);
87
		}
88
89
		/* collect output */
90
91 3
		$output .= $listElement->html($outputItem ? : $itemElement->text($this->_language->get('template_no')));
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('template_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...
Bug introduced by
It seems like $outputItem ?: $itemElem...ge->get('template_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...
92
		$output .= Module\Hook::trigger('navigationTemplateEnd');
93 3
		return $output;
94
	}
95
}
96