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

includes/Admin/Controller/Comment.php (11 issues)

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\Admin\Controller;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Filter;
6
use function json_encode;
7
use function strtotime;
8
9
/**
10
 * children class to process the admin comment request
11
 *
12
 * @since 4.0.0
13
 *
14
 * @package Redaxscript
15
 * @category Controller
16
 * @author Henry Ruhs
17
 */
18
19
class Comment extends ControllerAbstract
20
{
21
	/**
22
	 * process the class
23
	 *
24
	 * @since 4.0.0
25
	 *
26
	 * @param string $action action to process
27
	 *
28
	 * @return string
29
	 */
30
31
	public function process(string $action = null) : string
32
	{
33
		$postArray = $this->_normalizePost($this->_sanitizePost());
34
		$validateArray = $this->_validatePost($postArray);
0 ignored issues
show
It seems like $postArray defined by $this->_normalizePost($this->_sanitizePost()) on line 33 can also be of type null; however, Redaxscript\Admin\Contro...omment::_validatePost() does only seem to accept array, 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...
35
		$myUser = $this->_registry->get('myUser');
36
		$myEmail = $this->_registry->get('myEmail');
37
		$now = $this->_registry->get('now');
38
39
		/* validate post */
40
41
		if ($validateArray)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $validateArray of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
42
		{
43
			return $this->_error(
44
			[
45
				'route' => $this->_getErrorRoute($postArray),
0 ignored issues
show
It seems like $postArray defined by $this->_normalizePost($this->_sanitizePost()) on line 33 can also be of type null; however, Redaxscript\Admin\Contro...mment::_getErrorRoute() does only seem to accept array, 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...
46
				'message' => $validateArray
47
			]);
48
		}
49
50
		/* handle create */
51
52
		if ($action === 'create')
53
		{
54
			$createArray =
55
			[
56
				'author' => $myUser,
57
				'email' => $myEmail,
58
				'url' => $postArray['url'],
59
				'text' => $postArray['text'],
60
				'language' => $postArray['language'],
61
				'article' => $postArray['article'],
62
				'status' => $postArray['date'] > $now ? 2 : $postArray['status'],
63
				'rank' => $postArray['rank'],
64
				'access' => $postArray['access'],
65
				'date' => $postArray['date'] ? : $now
66
			];
67
			if ($this->_create($createArray))
68
			{
69
				return $this->_success(
70
				[
71
					'route' => $this->_getSuccessRoute($postArray),
72
					'timeout' => 2
73
				]);
74
			}
75
		}
76
77
		/* handle update */
78
79
		if ($action === 'update')
80
		{
81
			$updateArray =
82
			[
83
				'url' => $postArray['url'],
84
				'text' => $postArray['text'],
85
				'language' => $postArray['language'],
86
				'article' => $postArray['article'],
87
				'status' => $postArray['date'] > $now ? 2 : $postArray['status'],
88
				'rank' => $postArray['rank'],
89
				'access' => $postArray['access'],
90
				'date' => $postArray['date'] ? : $now
91
			];
92
			if ($this->_update($postArray['id'], $updateArray))
93
			{
94
				return $this->_success(
95
				[
96
					'route' => $this->_getSuccessRoute($postArray),
97
					'timeout' => 2
98
				]);
99
			}
100
		}
101
102
		/* handle error */
103
104
		return $this->_error(
105
		[
106
			'route' => $this->_getErrorRoute($postArray)
0 ignored issues
show
It seems like $postArray defined by $this->_normalizePost($this->_sanitizePost()) on line 33 can also be of type null; however, Redaxscript\Admin\Contro...mment::_getErrorRoute() does only seem to accept array, 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...
107
		]);
108
	}
109
110
	/**
111
	 * sanitize the post
112
	 *
113
	 * @since 4.0.0
114
	 *
115
	 * @return array
116
	 */
117
118
	protected function _sanitizePost() : array
119
	{
120
		$numberFilter = new Filter\Number();
121
		$specialFilter = new Filter\Special();
122
		$urlFilter = new Filter\Url();
123
		$htmlFilter = new Filter\Html();
124
125
		/* sanitize post */
126
127
		return
128
		[
129
			'id' => $numberFilter->sanitize($this->_request->getPost('id')),
0 ignored issues
show
It seems like $this->_request->getPost('id') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Number::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
130
			'url' => $urlFilter->sanitize($this->_request->getPost('url')),
0 ignored issues
show
It seems like $this->_request->getPost('url') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Url::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
131
			'text' => $htmlFilter->sanitize($this->_request->getPost('text'), $this->_registry->get('filter')),
0 ignored issues
show
It seems like $this->_request->getPost('text') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Html::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
$this->_registry->get('filter') is of type string|array|null, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
132
			'language' => $specialFilter->sanitize($this->_request->getPost('language')),
0 ignored issues
show
It seems like $this->_request->getPost('language') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Special::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
133
			'article' => $this->_request->getPost('article'),
134
			'status' => $numberFilter->sanitize($this->_request->getPost('status')),
0 ignored issues
show
It seems like $this->_request->getPost('status') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Number::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
135
			'rank' => $numberFilter->sanitize($this->_request->getPost('rank')),
0 ignored issues
show
It seems like $this->_request->getPost('rank') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Number::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
136
			'access' => json_encode($this->_request->getPost('access')),
137
			'date' => strtotime($this->_request->getPost('date'))
138
		];
139
	}
140
141
	/**
142
	 * validate the post
143
	 *
144
	 * @since 4.0.0
145
	 *
146
	 * @param array $postArray array of the post
147
	 *
148
	 * @return array
149
	 */
150
151
	protected function _validatePost(array $postArray = []) : array
152
	{
153
		$validateArray = [];
154
155
		/* validate post */
156
157
		if (!$postArray['text'])
158
		{
159
			$validateArray[] = $this->_language->get('comment_empty');
160
		}
161
		if (!$postArray['article'])
162
		{
163
			$validateArray[] = $this->_language->get('article_empty');
164
		}
165
		return $validateArray;
166
	}
167
168
	/**
169
	 * create the comment
170
	 *
171
	 * @since 4.0.0
172
	 *
173
	 * @param array $createArray array of the create
174
	 *
175
	 * @return bool
176
	 */
177
178
	protected function _create(array $createArray = []) : bool
179
	{
180
		$commentModel = new Admin\Model\Comment();
181
		return $commentModel->createByArray($createArray);
182
	}
183
184
	/**
185
	 * update the comment
186
	 *
187
	 * @since 4.0.0
188
	 *
189
	 * @param int $commentId identifier of the comment
190
	 * @param array $updateArray array of the update
191
	 *
192
	 * @return bool
193
	 */
194
195
	protected function _update(int $commentId = null, array $updateArray = []) : bool
196
	{
197
		$commentModel = new Admin\Model\Comment();
198
		return $commentModel->updateByIdAndArray($commentId, $updateArray);
199
	}
200
201
	/**
202
	 * get success route
203
	 *
204
	 * @since 4.0.0
205
	 *
206
	 * @param array $postArray array of the post
207
	 *
208
	 * @return string
209
	 */
210
211
	protected function _getSuccessRoute(array $postArray = []) : string
212
	{
213
		if ($this->_registry->get('commentsEdit'))
214
		{
215
			if ($postArray['id'])
216
			{
217
				return 'admin/view/comments#row-' . $postArray['id'];
218
			}
219
			return 'admin/view/comments';
220
		}
221
		return 'admin';
222
	}
223
224
	/**
225
	 * get error route
226
	 *
227
	 * @since 4.0.0
228
	 *
229
	 * @param array $postArray array of the post
230
	 *
231
	 * @return string
232
	 */
233
234
	protected function _getErrorRoute(array $postArray = []) : string
235
	{
236
		if ($this->_registry->get('commentsEdit') && $postArray['id'])
237
		{
238
			return 'admin/edit/comments/' . $postArray['id'];
239
		}
240
		if ($this->_registry->get('commentsNew'))
241
		{
242
			return 'admin/new/comments';
243
		}
244
		return 'admin';
245
	}
246
}
247