Completed
Push — master ( d0c173...e44a53 )
by Henry
09:36
created

Article::__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\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
	 * @return self
109
	 */
110
111
	public function init(array $optionArray = []) : self
112
	{
113
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
114
		return $this;
115
	}
116
117
	/**
118
	 * render the view
119
	 *
120
	 * @since 4.0.0
121
	 *
122
	 * @param int $categoryId identifier of the category
123
	 * @param int $articleId identifier of the article
124
	 *
125
	 * @return string
126
	 */
127
128
	public function render(int $categoryId = null, int $articleId = null) : string
129
	{
130
		if ($this->_registry->get('articleReplace'))
131
		{
132
			return Module\Hook::trigger('articleReplace');
133
		}
134
		$output = Module\Hook::trigger('articleStart');
135
		$outputFragment = null;
136
		$accessValidator = new Validator\Access();
137
		$articleModel = new Model\Article();
138
		$articles = null;
0 ignored issues
show
Unused Code introduced by
$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...
139
		$contentParser = new Content\Parser($this->_registry, $this->_request, $this->_language, $this->_config);
140
		$byline = new Helper\Byline($this->_registry, $this->_language);
141
		$byline->init();
142
		$adminDock = new Admin\View\Helper\Dock($this->_registry, $this->_language);
143
		$adminDock->init();
144
		$loggedIn = $this->_registry->get('loggedIn');
145
		$token = $this->_registry->get('token');
146
		$firstParameter = $this->_registry->get('firstParameter');
147
		$lastTable = $this->_registry->get('lastTable');
148
		$parameterRoute = $this->_registry->get('parameterRoute');
149
		$myGroups = $this->_registry->get('myGroups');
150
151
		/* html element */
152
153
		$element = new Html\Element();
154
		$titleElement = $element
155
			->copy()
156
			->init($this->_optionArray['tag']['title'],
157
			[
158
				'class' => $this->_optionArray['className']['title']
159
			]);
160
		$linkElement = $element->copy()->init('a');
161
		$boxElement = $element
162
			->copy()
163
			->init($this->_optionArray['tag']['box'],
164
			[
165
				'class' => $this->_optionArray['className']['box']
166
			]);
167
		$articles = $this->queryArticles($categoryId, $articleId);
168
169
		/* process articles */
170
171
		foreach ($articles as $value)
0 ignored issues
show
Bug introduced by
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...
172
		{
173
			if ($accessValidator->validate($value->access, $myGroups))
0 ignored issues
show
Bug introduced by
It seems like $myGroups defined by $this->_registry->get('myGroups') on line 149 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...
174
			{
175
				$outputFragment .= Module\Hook::trigger('articleFragmentStart', (array)$value);
176
				if ($value->headline)
177
				{
178
					$outputFragment .= $titleElement
179
						->attr('id', 'article-' . $value->alias)
180
						->html($lastTable === 'categories' ? $linkElement
181
							->attr('href', $parameterRoute . $articleModel->getRouteById($value->id))
182
							->text($value->title) : $value->title
183
						);
184
				}
185
				$contentParser->process($value->text);
186
				$outputFragment .= $boxElement->html($contentParser->getOutput()) . $byline->render($value->date, $value->author) . Module\Hook::trigger('articleFragmentEnd', (array)$value);
187
188
				/* admin dock */
189
190
				if ($loggedIn === $token && $firstParameter !== 'logout')
191
				{
192
					$outputFragment .= $adminDock->render('articles', $value->id);
193
				}
194
			}
195
		}
196
197
		/* collect output */
198
199
		$output .= $outputFragment ? : Template\Tag::partial($this->_registry->get('template') . '/' . $this->_optionArray['partial']['error']);
200
		$output .= Module\Hook::trigger('articleEnd');
201
		return $output;
202
	}
203
204
	/**
205
	 * query the articles
206
	 *
207
	 * @since 4.0.0
208
	 *
209
	 * @param int $categoryId identifier of the category
210
	 * @param int $articleId identifier of the article
211
	 *
212
	 * @return object|null
213
	 */
214
215
	public function queryArticles(int $categoryId = null, int $articleId = null) : ?object
216
	{
217
		$articleModel = new Model\Article();
218
		$settingModel = new Model\Setting();
219
		$lastSubParameter = $this->_registry->get('lastSubParameter');
220
		$language = $this->_registry->get('language');
221
222
		/* query articles */
223
224
		if ($categoryId)
0 ignored issues
show
Bug Best Practice introduced by
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...
225
		{
226
			$limitStep = $settingModel->get('pagination') ? $lastSubParameter - 1 : null;
227
			return $articleModel->getSiblingByCategoryAndLanguageAndOrderAndStep($categoryId, $language, $this->_optionArray['orderColumn'], $limitStep);
0 ignored issues
show
Bug introduced by
It seems like $language defined by $this->_registry->get('language') on line 220 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...
228
		}
229
		if ($articleId)
0 ignored issues
show
Bug Best Practice introduced by
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->getSiblingByIdAndLanguageAndOrder($articleId, $language, $this->_optionArray['orderColumn']);
0 ignored issues
show
Bug introduced by
It seems like $language defined by $this->_registry->get('language') on line 220 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
It seems like $language defined by $this->_registry->get('language') on line 220 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