Completed
Push — master ( 96a033...7625bb )
by Henry
07:07
created

Extra::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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
	 * init the class
85
	 *
86
	 * @since 4.0.0
87
	 *
88
	 * @param array $optionArray options of the extra
89
	 *
90
	 * @return self
91
	 */
92
93
	public function init(array $optionArray = []) : self
94
	{
95
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
96
		return $this;
97
	}
98
99
	/**
100
	 * render the view
101
	 *
102
	 * @since 4.0.0
103
	 *
104
	 * @param int $extraId identifier of the extra
105
	 *
106
	 * @return string
107
	 */
108
109
	public function render(int $extraId = null) : string
110
	{
111
		if ($this->_registry->get('extraReplace'))
112
		{
113
			return Module\Hook::trigger('extraReplace');
114
		}
115
		$output = Module\Hook::trigger('extraStart');
116
		$accessValidator = new Validator\Access();
117
		$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...
118
		$contentParser = new Content\Parser($this->_registry, $this->_request, $this->_language, $this->_config);
119
		$adminDock = new Admin\View\Helper\Dock($this->_registry, $this->_language);
120
		$adminDock->init();
121
		$loggedIn = $this->_registry->get('loggedIn');
122
		$token = $this->_registry->get('token');
123
		$categoryId = $this->_registry->get('categoryId');
124
		$articleId = $this->_registry->get('articleId');
125
		$firstParameter = $this->_registry->get('firstParameter');
126
		$myGroups = $this->_registry->get('myGroups');
127
128
		/* html element */
129
130
		$element = new Html\Element();
131
		$titleElement = $element
132
			->copy()
133
			->init($this->_optionArray['tag']['title'],
134
			[
135
				'class' => $this->_optionArray['className']['title']
136
			]);
137
		$boxElement = $element
138
			->copy()
139
			->init($this->_optionArray['tag']['box'],
140
			[
141
				'class' => $this->_optionArray['className']['box']
142
			]);
143
		$extras = $this->queryExtras($extraId);
144
145
		/* process extras */
146
147
		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...
148
		{
149
			$validateContent = true;
150
			if ($value->category)
151
			{
152
				$validateContent = (string)$value->category === $categoryId;
153
			}
154
			if ($value->article)
155
			{
156
				$validateContent = (string)$value->article === $articleId;
157
			}
158
			if ($accessValidator->validate($value->access, $myGroups) && $validateContent)
0 ignored issues
show
Bug introduced by
It seems like $myGroups defined by $this->_registry->get('myGroups') on line 126 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...
159
			{
160
				$output .= Module\Hook::trigger('extraFragmentStart', (array)$value);
161
				if ($value->headline)
162
				{
163
					$output .= $titleElement
164
						->attr('id', 'extra-' . $value->alias)
165
						->text($value->title);
166
				}
167
				$contentParser->process($value->text);
168
				$output .= $boxElement->html($contentParser->getOutput()) . Module\Hook::trigger('extraFragmentEnd', (array)$value);
169
170
				/* admin dock */
171
172
				if ($loggedIn === $token && $firstParameter !== 'logout')
173
				{
174
					$output .= $adminDock->render('extras', $value->id);
175
				}
176
			}
177
		}
178
		$output .= Module\Hook::trigger('extraEnd');
179
		return $output;
180
	}
181
182
	/**
183
	 * query the extras
184
	 *
185
	 * @since 4.0.0
186
	 *
187
	 * @param int $extraId identifier of the extra
188
	 *
189
	 * @return object|null
190
	 */
191
192
	public function queryExtras(int $extraId = null) : ?object
193
	{
194
		$extraModel = new Model\Extra();
195
		$language = $this->_registry->get('language');
196
197
		/* query extras */
198
199
		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...
200
		{
201
			return $extraModel->getSiblingByIdAndLanguageAndOrder($extraId, $language, $this->_optionArray['orderColumn']);
0 ignored issues
show
Bug introduced by
It seems like $language defined by $this->_registry->get('language') on line 195 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...
202
		}
203
		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 195 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...
204
	}
205
}
206