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

Extra::queryExtras()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 0
cp 0
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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
use function array_replace_recursive;
15
16
/**
17
 * children class to create the extra
18
 *
19
 * @since 4.0.0
20
 *
21
 * @package Redaxscript
22
 * @category View
23
 * @author Henry Ruhs
24
 */
25
26
class Extra extends ViewAbstract
27
{
28
	/**
29
	 * instance of the request class
30
	 *
31
	 * @var Request
32
	 */
33
34
	protected $_request;
35
36
	/**
37
	 * instance of the config class
38
	 *
39
	 * @var Config
40
	 */
41
42
	protected $_config;
43
44
	/**
45
	 * options of the extra
46
	 *
47
	 * @var array
48
	 */
49
50
	protected $_optionArray =
51
	[
52
		'tag' =>
53
		[
54
			'title' => 'h3',
55
			'box' => 'div'
56
		],
57
		'className' =>
58
		[
59
			'title' => 'rs-title-extra',
60
			'box' => 'rs-box-extra'
61
		],
62
		'orderColumn' => 'rank'
63
	];
64
65
	/**
66
	 * constructor of the class
67
	 *
68
	 * @since 4.0.0
69
	 *
70
	 * @param Registry $registry instance of the registry class
71
	 * @param Request $request instance of the request class
72
	 * @param Language $language instance of the language class
73
	 * @param Config $config instance of the config class
74
	 */
75
76
	public function __construct(Registry $registry, Request $request, Language $language, Config $config)
77
	{
78
		parent::__construct($registry, $language);
79
		$this->_request = $request;
80
		$this->_config = $config;
81
	}
82
83
	/**
84
	 * stringify the extra
85
	 *
86
	 * @since 4.0.0
87
	 *
88
	 * @return string
89
	 */
90
91
	public function __toString() : string
92
	{
93
		return $this->render();
94
	}
95
96
	/**
97
	 * init the class
98
	 *
99
	 * @since 4.0.0
100
	 *
101
	 * @param array $optionArray options of the extra
102
	 */
103
104
	public function init(array $optionArray = []) : void
105
	{
106
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
107
	}
108
109
	/**
110
	 * render the view
111
	 *
112
	 * @since 4.0.0
113
	 *
114
	 * @param int $extraId identifier of the extra
115
	 *
116
	 * @return string
117
	 */
118
119
	public function render(int $extraId = null) : string
120
	{
121
		if ($this->_registry->get('extraReplace'))
122
		{
123
			return Module\Hook::trigger('extraReplace');
124
		}
125
		$output = Module\Hook::trigger('extraStart');
126
		$accessValidator = new Validator\Access();
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
		$loggedIn = $this->_registry->get('loggedIn');
132
		$token = $this->_registry->get('token');
133
		$categoryId = $this->_registry->get('categoryId');
134
		$articleId = $this->_registry->get('articleId');
135
		$firstParameter = $this->_registry->get('firstParameter');
136
		$myGroups = $this->_registry->get('myGroups');
137
138
		/* html element */
139
140
		$element = new Html\Element();
141
		$titleElement = $element
142
			->copy()
143
			->init($this->_optionArray['tag']['title'],
144
			[
145
				'class' => $this->_optionArray['className']['title']
146
			]);
147
		$boxElement = $element
148
			->copy()
149
			->init($this->_optionArray['tag']['box'],
150
			[
151
				'class' => $this->_optionArray['className']['box']
152
			]);
153
		$extras = $this->queryExtras($extraId);
154
155
		/* process extras */
156
157
		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...
158
		{
159
			$validateCategory = $categoryId === $value->category || !$value->category;
160
			$validateArticle = $articleId === $value->article || !$value->article;
161
			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 136 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...
162
			{
163
				$output .= Module\Hook::trigger('extraFragmentStart', (array)$value);
164
				if ($value->headline)
165
				{
166
					$output .= $titleElement
167
						->attr('id', 'extra-' . $value->alias)
168
						->text($value->title);
169
				}
170
				$contentParser->process($value->text);
171
				$output .= $boxElement->html($contentParser->getOutput()) . Module\Hook::trigger('extraFragmentEnd', (array)$value);
172
173
				/* admin dock */
174
175
				if ($loggedIn === $token && $firstParameter !== 'logout')
176
				{
177
					$output .= $adminDock->render('extras', $value->id);
178
				}
179
			}
180
		}
181
		$output .= Module\Hook::trigger('extraEnd');
182
		return $output;
183
	}
184
185
	/**
186
	 * query the extras
187
	 *
188
	 * @since 4.0.0
189
	 *
190
	 * @param int $extraId identifier of the extra
191
	 *
192
	 * @return object|null
193
	 */
194
195
	public function queryExtras(int $extraId = null) : ?object
196
	{
197
		$extraModel = new Model\Extra();
198
		$language = $this->_registry->get('language');
199
200
		/* query extras */
201
202
		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...
203
		{
204
			return $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 198 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...
205
		}
206
		return $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 198 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...
207
	}
208
}
209