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

Article::queryArticles()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 4
cp 0
rs 9.552
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 20
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\Template;
14
use Redaxscript\Validator;
15
use function array_replace_recursive;
16
17
/**
18
 * children class to create the article
19
 *
20
 * @since 4.0.0
21
 *
22
 * @package Redaxscript
23
 * @category View
24
 * @author Henry Ruhs
25
 */
26
27
class Article extends ViewAbstract
28
{
29
	/**
30
	 * instance of the request class
31
	 *
32
	 * @var Request
33
	 */
34
35
	protected $_request;
36
37
	/**
38
	 * instance of the config class
39
	 *
40
	 * @var Config
41
	 */
42
43
	protected $_config;
44
45
	/**
46
	 * options of the article
47
	 *
48
	 * @var array
49
	 */
50
51
	protected $_optionArray =
52
	[
53
		'tag' =>
54
		[
55
			'title' => 'h2',
56
			'box' => 'div'
57
		],
58
		'className' =>
59
		[
60
			'title' => 'rs-title-content',
61
			'box' => 'rs-box-content'
62
		],
63
		'orderColumn' => 'rank',
64
		'partial' =>
65
		[
66
			'error' => 'error.phtml'
67
		]
68
	];
69
70
	/**
71
	 * constructor of the class
72
	 *
73
	 * @since 4.0.0
74
	 *
75
	 * @param Registry $registry instance of the registry class
76
	 * @param Request $request instance of the request class
77
	 * @param Language $language instance of the language class
78
	 * @param Config $config instance of the config class
79
	 */
80
81
	public function __construct(Registry $registry, Request $request, Language $language, Config $config)
82
	{
83
		parent::__construct($registry, $language);
84
		$this->_request = $request;
85
		$this->_config = $config;
86
	}
87
88
	/**
89
	 * stringify the article
90
	 *
91
	 * @since 4.0.0
92
	 *
93
	 * @return string
94
	 */
95
96
	public function __toString() : string
97
	{
98
		return $this->render();
99
	}
100
101
	/**
102
	 * init the class
103
	 *
104
	 * @since 4.0.0
105
	 *
106
	 * @param array $optionArray options of the article
107
	 */
108
109
	public function init(array $optionArray = []) : void
110
	{
111
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
112
	}
113
114
	/**
115
	 * render the view
116
	 *
117
	 * @since 4.0.0
118
	 *
119
	 * @param int $categoryId identifier of the category
120
	 * @param int $articleId identifier of the article
121
	 *
122
	 * @return string
123
	 */
124
125
	public function render(int $categoryId = null, int $articleId = null) : string
126
	{
127
		if ($this->_registry->get('articleReplace'))
128
		{
129
			return Module\Hook::trigger('articleReplace');
130
		}
131
		$output = Module\Hook::trigger('articleStart');
132
		$outputFragment = null;
133
		$accessValidator = new Validator\Access();
134
		$articleModel = new Model\Article();
135
		$articles = null;
0 ignored issues
show
Unused Code introduced by redaxmedia
$articles 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...
136
		$contentParser = new Content\Parser($this->_registry, $this->_request, $this->_language, $this->_config);
137
		$byline = new Helper\Byline($this->_registry, $this->_language);
138
		$byline->init();
139
		$adminDock = new Admin\View\Helper\Dock($this->_registry, $this->_language);
140
		$adminDock->init();
141
		$loggedIn = $this->_registry->get('loggedIn');
142
		$token = $this->_registry->get('token');
143
		$firstParameter = $this->_registry->get('firstParameter');
144
		$lastTable = $this->_registry->get('lastTable');
145
		$parameterRoute = $this->_registry->get('parameterRoute');
146
		$myGroups = $this->_registry->get('myGroups');
147
148
		/* html element */
149
150
		$element = new Html\Element();
151
		$titleElement = $element
152
			->copy()
153
			->init($this->_optionArray['tag']['title'],
154
			[
155
				'class' => $this->_optionArray['className']['title']
156
			]);
157
		$linkElement = $element->copy()->init('a');
158
		$boxElement = $element
159
			->copy()
160
			->init($this->_optionArray['tag']['box'],
161
			[
162
				'class' => $this->_optionArray['className']['box']
163
			]);
164
		$articles = $this->queryArticles($categoryId, $articleId);
165
166
		/* process articles */
167
168
		foreach ($articles as $value)
0 ignored issues
show
Bug introduced by Henry Ruhs
The expression $articles 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
			if ($accessValidator->validate($value->access, $myGroups))
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $myGroups defined by $this->_registry->get('myGroups') on line 146 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...
171
			{
172
				$outputFragment .= Module\Hook::trigger('articleFragmentStart', (array)$value);
173
				if ($value->headline)
174
				{
175
					$outputFragment .= $titleElement
176
						->attr('id', 'article-' . $value->alias)
177
						->html($lastTable === 'categories' ? $linkElement
178
							->attr('href', $parameterRoute . $articleModel->getRouteById($value->id))
179
							->text($value->title) : $value->title
180
						);
181
				}
182
				$contentParser->process($value->text);
183
				$outputFragment .= $boxElement->html($contentParser->getOutput()) . $byline->render($value->date, $value->author) . Module\Hook::trigger('articleFragmentEnd', (array)$value);
184
185
				/* admin dock */
186
187
				if ($loggedIn === $token && $firstParameter !== 'logout')
188
				{
189
					$outputFragment .= $adminDock->render('articles', $value->id);
190
				}
191
			}
192
		}
193
194
		/* collect output */
195
196
		$output .= $outputFragment ? : Template\Tag::partial($this->_registry->get('template') . '/' . $this->_optionArray['partial']['error']);
197
		$output .= Module\Hook::trigger('articleEnd');
198
		return $output;
199
	}
200
201
	/**
202
	 * query the articles
203
	 *
204
	 * @since 4.0.0
205
	 *
206
	 * @param int $categoryId identifier of the category
207
	 * @param int $articleId identifier of the article
208
	 *
209
	 * @return object|null
210
	 */
211
212
	public function queryArticles(int $categoryId = null, int $articleId = null) : ?object
213
	{
214
		$articleModel = new Model\Article();
215
		$settingModel = new Model\Setting();
216
		$lastSubParameter = $this->_registry->get('lastSubParameter');
217
		$language = $this->_registry->get('language');
218
219
		/* query articles */
220
221
		if ($categoryId)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $categoryId 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...
222
		{
223
			if ($settingModel->get('pagination'))
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $settingModel->get('pagination') of type string|null is loosely compared to true; this is ambiguous if the string can be empty. 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 string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
224
			{
225
				return $articleModel->getByCategoryAndLanguageAndOrderAndStep($categoryId, $language, $this->_optionArray['orderColumn'], $lastSubParameter - 1);
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $language defined by $this->_registry->get('language') on line 217 can also be of type array; however, Redaxscript\Model\Articl...nguageAndOrderAndStep() 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...
226
			}
227
			return $articleModel->getByCategoryAndLanguageAndOrder($categoryId, $language, $this->_optionArray['orderColumn']);
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $language defined by $this->_registry->get('language') on line 217 can also be of type array; however, Redaxscript\Model\Articl...ryAndLanguageAndOrder() 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...
228
		}
229
		if ($articleId)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $articleId 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...
230
		{
231
			return $articleModel->getByIdAndLanguageAndOrder($articleId, $language, $this->_optionArray['orderColumn']);
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $language defined by $this->_registry->get('language') on line 217 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...
232
		}
233
		return $articleModel->getByLanguageAndOrder($language, $this->_optionArray['orderColumn']);
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $language defined by $this->_registry->get('language') on line 217 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...
234
	}
235
}
236