Completed
Push — master ( 03fc7e...44f12a )
by Henry
15:26
created

Extra   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 8
dl 0
loc 171
ccs 0
cts 51
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __toString() 0 4 1
A init() 0 4 1
C render() 0 77 12
1
<?php
2
namespace Redaxscript\View;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Config;
6
use Redaxscript\Content;
7
use Redaxscript\Html;
8
use Redaxscript\Language;
9
use Redaxscript\Model;
10
use Redaxscript\Module;
11
use Redaxscript\Registry;
12
use Redaxscript\Request;
13
use Redaxscript\Validator;
14
15
/**
16
 * children class to create the extra
17
 *
18
 * @since 4.0.0
19
 *
20
 * @package Redaxscript
21
 * @category View
22
 * @author Henry Ruhs
23
 */
24
25
class Extra extends ViewAbstract
26
{
27
	/**
28
	 * instance of the request class
29
	 *
30
	 * @var Request
31
	 */
32
33
	protected $_request;
34
35
	/**
36
	 * instance of the config class
37
	 *
38
	 * @var Config
39
	 */
40
41
	protected $_config;
42
43
	/**
44
	 * options of the extra
45
	 *
46
	 * @var array
47
	 */
48
49
	protected $_optionArray =
50
	[
51
		'tag' =>
52
		[
53
			'title' => 'h3',
54
			'box' => 'div'
55
		],
56
		'className' =>
57
		[
58
			'title' => 'rs-title-extra',
59
			'box' => 'rs-box-extra'
60
		],
61
		'orderColumn' => 'rank'
62
	];
63
64
	/**
65
	 * constructor of the class
66
	 *
67
	 * @since 4.0.0
68
	 *
69
	 * @param Registry $registry instance of the registry class
70
	 * @param Request $request instance of the request class
71
	 * @param Language $language instance of the language class
72
	 * @param Config $config instance of the config class
73
	 */
74
75
	public function __construct(Registry $registry, Request $request, Language $language, Config $config)
76
	{
77
		parent::__construct($registry, $language);
78
		$this->_request = $request;
79
		$this->_config = $config;
80
	}
81
82
	/**
83
	 * stringify the extra
84
	 *
85
	 * @since 4.0.0
86
	 *
87
	 * @return string
88
	 */
89
90
	public function __toString() : string
91
	{
92
		return $this->render();
93
	}
94
95
	/**
96
	 * init the class
97
	 *
98
	 * @since 4.0.0
99
	 *
100
	 * @param array $optionArray options of the extra
101
	 */
102
103
	public function init(array $optionArray = [])
104
	{
105
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
106
	}
107
108
	/**
109
	 * render the view
110
	 *
111
	 * @since 4.0.0
112
	 *
113
	 * @param int $extraId identifier of the extra
114
	 *
115
	 * @return string
116
	 */
117
118
	public function render(int $extraId = null) : string
119
	{
120
		if ($this->_registry->get('extraReplace'))
121
		{
122
			return Module\Hook::trigger('extraReplace');
123
		}
124
		$output = Module\Hook::trigger('extraStart');
125
		$accessValidator = new Validator\Access();
126
		$extraModel = new Model\Extra();
127
		$extras = null;
0 ignored issues
show
Unused Code introduced by
$extras is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
128
		$contentParser = new Content\Parser($this->_registry, $this->_request, $this->_language, $this->_config);
129
		$adminDock = new Admin\View\Helper\Dock($this->_registry, $this->_language);
130
		$adminDock->init();
131
		$language = $this->_registry->get('language');
132
		$loggedIn = $this->_registry->get('loggedIn');
133
		$token = $this->_registry->get('token');
134
		$categoryId = $this->_registry->get('categoryId');
135
		$articleId = $this->_registry->get('articleId');
136
		$firstParameter = $this->_registry->get('firstParameter');
137
		$myGroups = $this->_registry->get('myGroups');
138
139
		/* html element */
140
141
		$element = new Html\Element();
142
		$titleElement = $element
143
			->copy()
144
			->init($this->_optionArray['tag']['title'],
145
			[
146
				'class' => $this->_optionArray['className']['title']
147
			]);
148
		$boxElement = $element
149
			->copy()
150
			->init($this->_optionArray['tag']['box'],
151
			[
152
				'class' => $this->_optionArray['className']['box']
153
			]);
154
155
		/* query extras */
156
157
		if ($extraId)
0 ignored issues
show
Bug Best Practice introduced by
The expression $extraId of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
158
		{
159
			$extras = $extraModel->getByIdAndLanguageAndOrder($extraId, $language, $this->_optionArray['orderColumn']);
0 ignored issues
show
Bug introduced by
It seems like $language defined by $this->_registry->get('language') on line 131 can also be of type array; however, Redaxscript\Model\Conten...IdAndLanguageAndOrder() 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...
160
		}
161
		else
162
		{
163
			$extras = $extraModel->getByLanguageAndOrder($language, $this->_optionArray['orderColumn']);
0 ignored issues
show
Bug introduced by
It seems like $language defined by $this->_registry->get('language') on line 131 can also be of type array; however, Redaxscript\Model\Conten...getByLanguageAndOrder() 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...
164
		}
165
166
		/* process extras */
167
168
		foreach ($extras as $value)
0 ignored issues
show
Bug introduced by
The expression $extras 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...
169
		{
170
			$validateCategory = $categoryId === $value->category || !$value->category;
171
			$validateArticle = $articleId === $value->article || !$value->article;
172
			if ($accessValidator->validate($value->access, $myGroups) && ($validateCategory || $validateArticle))
0 ignored issues
show
Bug introduced by
It seems like $myGroups defined by $this->_registry->get('myGroups') on line 137 can also be of type array; however, Redaxscript\Validator\Access::validate() 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...
173
			{
174
				$output .= Module\Hook::trigger('extraFragmentStart', (array)$value);
175
				if ($value->headline)
176
				{
177
					$output .= $titleElement
178
						->attr('id', 'extra-' . $value->alias)
179
						->text($value->title);
180
				}
181
				$contentParser->process($value->text);
182
				$output .= $boxElement->html($contentParser->getOutput()) . Module\Hook::trigger('extraFragmentEnd', (array)$value);
183
184
				/* admin dock */
185
186
				if ($loggedIn === $token && $firstParameter !== 'logout')
187
				{
188
					$output .= $adminDock->render('extras', $value->id);
189
				}
190
			}
191
		}
192
		$output .= Module\Hook::trigger('extraEnd');
193
		return $output;
194
	}
195
}
196