Completed
Push — master ( 4eb4a8...a52438 )
by Henry
07:48
created

includes/View/Extra.php (4 issues)

Labels

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
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 6
	public function __construct(Registry $registry, Request $request, Language $language, Config $config)
77
	{
78 6
		parent::__construct($registry, $language);
79 6
		$this->_request = $request;
80 6
		$this->_config = $config;
81 6
	}
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 6
	public function init(array $optionArray = []) : self
94
	{
95 6
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
96 6
		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 6
	public function render(int $extraId = null) : string
110
	{
111 6
		if ($this->_registry->get('extraReplace'))
112
		{
113
			return Module\Hook::trigger('extraReplace');
114
		}
115 6
		$output = Module\Hook::trigger('extraStart');
116 6
		$accessValidator = new Validator\Access();
117 6
		$extras = null;
118 6
		$contentParser = new Content\Parser($this->_registry, $this->_request, $this->_language, $this->_config);
119 6
		$adminDock = new Admin\View\Helper\Dock($this->_registry, $this->_language);
120 6
		$adminDock->init();
121 6
		$loggedIn = $this->_registry->get('loggedIn');
122 6
		$token = $this->_registry->get('token');
123 6
		$categoryId = $this->_registry->get('categoryId');
124 6
		$articleId = $this->_registry->get('articleId');
125 6
		$firstParameter = $this->_registry->get('firstParameter');
126 6
		$myGroups = $this->_registry->get('myGroups');
127
128
		/* html element */
129
130 6
		$element = new Html\Element();
131
		$titleElement = $element
132 6
			->copy()
133 6
			->init($this->_optionArray['tag']['title'],
134
			[
135 6
				'class' => $this->_optionArray['className']['title']
136
			]);
137
		$boxElement = $element
138 6
			->copy()
139 6
			->init($this->_optionArray['tag']['box'],
140
			[
141 6
				'class' => $this->_optionArray['className']['box']
142
			]);
143 6
		$extras = $this->queryExtras($extraId);
144
145
		/* process extras */
146
147 6
		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...
148
		{
149 6
			$validateContent = true;
150 6
			if ($value->category)
151
			{
152 4
				$validateContent = (string)$value->category === $categoryId;
153
			}
154 6
			if ($value->article)
155
			{
156 4
				$validateContent = (string)$value->article === $articleId;
157
			}
158 6
			if ($accessValidator->validate($value->access, $myGroups) && $validateContent)
0 ignored issues
show
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 6
				$output .= Module\Hook::trigger('extraFragmentStart', (array)$value);
161 6
				if ($value->headline)
162
				{
163
					$output .= $titleElement
164 6
						->attr('id', 'extra-' . $value->alias)
165 6
						->text($value->title);
166
				}
167 6
				$contentParser->process($value->text);
168 6
				$output .= $boxElement->html($contentParser->getOutput()) . Module\Hook::trigger('extraFragmentEnd', (array)$value);
169
170
				/* admin dock */
171
172 6
				if ($loggedIn === $token && $firstParameter !== 'logout')
173
				{
174 1
					$output .= $adminDock->render('extras', $value->id);
175
				}
176
			}
177
		}
178 6
		$output .= Module\Hook::trigger('extraEnd');
179 6
		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 6
	public function queryExtras(int $extraId = null) : ?object
193
	{
194 6
		$extraModel = new Model\Extra();
195 6
		$language = $this->_registry->get('language');
196
197
		/* query extras */
198
199 6
		if ($extraId)
200
		{
201 2
			return $extraModel->getSiblingByIdAndLanguageAndOrder($extraId, $language, $this->_optionArray['orderColumn']);
0 ignored issues
show
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 4
		return $extraModel->getByLanguageAndOrder($language, $this->_optionArray['orderColumn']);
0 ignored issues
show
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