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

includes/View/Extra.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\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;
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
		$firstParameter = $this->_registry->get('firstParameter');
135
		$myGroups = $this->_registry->get('myGroups');
136
137
		/* html element */
138
139
		$element = new Html\Element();
140
		$titleElement = $element
141
			->copy()
142
			->init($this->_optionArray['tag']['title'],
143
			[
144
				'class' => $this->_optionArray['className']['title']
145
			]);
146
		$boxElement = $element
147
			->copy()
148
			->init($this->_optionArray['tag']['box'],
149
			[
150
				'class' => $this->_optionArray['className']['box']
151
			]);
152
153
		/* query extras */
154
155
		if ($extraId)
156
		{
157
			$extras = $extraModel->getByIdAndLanguageAndOrder($extraId, $language, $this->_optionArray['orderColumn']);
158
		}
159
		else
160
		{
161
			$extras = $extraModel->getByLanguageAndOrder($language, $this->_optionArray['orderColumn']);
162
		}
163
164
		/* process extras */
165
166
		foreach ($extras as $value)
0 ignored issues
show
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...
167
		{
168
			if ($accessValidator->validate($value->access, $myGroups))
0 ignored issues
show
It seems like $myGroups defined by $this->_registry->get('myGroups') on line 135 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...
169
			{
170
				$output .= Module\Hook::trigger('extraFragmentStart', (array)$value);
171
				if ($value->headline)
172
				{
173
					$output .= $titleElement
174
						->attr('id', 'extra-' . $value->alias)
175
						->text($value->title);
176
				}
177
				$contentParser->process($value->text);
178
				$output .= $boxElement->html($contentParser->getOutput()) . Module\Hook::trigger('extraFragmentEnd', (array)$value);
179
180
				/* admin dock */
181
182
				if ($loggedIn === $token && $firstParameter !== 'logout')
183
				{
184
					$output .= $adminDock->render('extras', $value->id);
185
				}
186
			}
187
		}
188
		$output .= Module\Hook::trigger('extraEnd');
189
		return $output;
190
	}
191
}
192