Completed
Push — master ( 29f0c5...0d72ed )
by Henry
09:47
created

includes/Admin/Controller/Setting.php (14 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
7
/**
8
 * children class to process the admin setting request
9
 *
10
 * @since 4.0.0
11
 *
12
 * @package Redaxscript
13
 * @category Controller
14
 * @author Henry Ruhs
15
 */
16
17
class Setting extends ControllerAbstract
18
{
19
	/**
20
	 * process the class
21
	 *
22
	 * @since 4.0.0
23
	 *
24
	 * @param string $action action to process
25
	 *
26
	 * @return string
27
	 */
28
29
	public function process(string $action = null) : string
30
	{
31
		$postArray = $this->_normalizePost($this->_sanitizePost());
32
		$validateArray = $this->_validatePost($postArray);
0 ignored issues
show
It seems like $postArray defined by $this->_normalizePost($this->_sanitizePost()) on line 31 can also be of type null; however, Redaxscript\Admin\Contro...etting::_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...
33
34
		/* validate post */
35
36
		if ($validateArray)
0 ignored issues
show
Bug Best Practice introduced by
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...
37
		{
38
			return $this->_error(
39
			[
40
				'route' => $this->_getErrorRoute(),
41
				'message' => $validateArray
42
			]);
43
		}
44
45
		/* handle update */
46
47
		if ($action === 'update')
48
		{
49
			$updateArray =
50
			[
51
				'language' => $postArray['language'],
52
				'template' => $postArray['template'],
53
				'title' => $postArray['title'],
54
				'author' => $postArray['author'],
55
				'copyright' => $postArray['copyright'],
56
				'description' => $postArray['description'],
57
				'keywords' => $postArray['keywords'],
58
				'robots' => $postArray['robots'],
59
				'email' => $postArray['email'],
60
				'subject' => $postArray['subject'],
61
				'notification' => $postArray['notification'],
62
				'charset' => $postArray['charset'],
63
				'divider' => $postArray['divider'],
64
				'zone' => $postArray['zone'],
65
				'time' => $postArray['time'],
66
				'date' => $postArray['date'],
67
				'homepage' => $postArray['homepage'],
68
				'limit' => $postArray['limit'],
69
				'order' => $postArray['order'],
70
				'pagination' => $postArray['pagination'],
71
				'moderation' => $postArray['moderation'],
72
				'registration' => $postArray['registration'],
73
				'verification' => $postArray['verification'],
74
				'recovery' => $postArray['recovery'],
75
				'captcha' => $postArray['captcha']
76
			];
77
			if ($this->_update($updateArray))
78
			{
79
				return $this->_success(
80
				[
81
					'route' => 'admin',
82
					'timeout' => 2
83
				]);
84
			}
85
		}
86
87
		/* handle error */
88
89
		return $this->_error(
90
		[
91
			'route' => $this->_getErrorRoute()
92
		]);
93
	}
94
95
	/**
96
	 * sanitize the post
97
	 *
98
	 * @since 4.0.0
99
	 *
100
	 * @return array
101
	 */
102
103
	protected function _sanitizePost() : array
104
	{
105
		$numberFilter = new Filter\Number();
106
		$specialFilter = new Filter\Special();
107
		$emailFilter = new Filter\Email();
108
109
		/* sanitize post */
110
111
		return
112
		[
113
			'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...
114
			'template' => $specialFilter->sanitize($this->_request->getPost('template')),
0 ignored issues
show
It seems like $this->_request->getPost('template') 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...
115
			'title' => $this->_request->getPost('title'),
116
			'author' => $this->_request->getPost('author'),
117
			'copyright' => $this->_request->getPost('copyright'),
118
			'description' => $this->_request->getPost('description'),
119
			'keywords' => $this->_request->getPost('keywords'),
120
			'robots' => $this->_request->getPost('robots'),
121
			'email' => $emailFilter->sanitize($this->_request->getPost('email')),
0 ignored issues
show
It seems like $this->_request->getPost('email') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Email::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...
122
			'subject' => $this->_request->getPost('subject'),
123
			'notification' => $numberFilter->sanitize($this->_request->getPost('notification')),
0 ignored issues
show
It seems like $this->_request->getPost('notification') 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...
124
			'charset' => $this->_request->getPost('charset'),
125
			'divider' => $this->_request->getPost('divider'),
126
			'zone' => $this->_request->getPost('zone'),
127
			'time' => $this->_request->getPost('time'),
128
			'date' => $this->_request->getPost('date'),
129
			'homepage' => $this->_request->getPost('homepage'),
130
			'limit' => $numberFilter->sanitize($this->_request->getPost('limit')),
0 ignored issues
show
It seems like $this->_request->getPost('limit') 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...
131
			'order' => $specialFilter->sanitize($this->_request->getPost('order')),
0 ignored issues
show
It seems like $this->_request->getPost('order') 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...
132
			'pagination' => $numberFilter->sanitize($this->_request->getPost('pagination')),
0 ignored issues
show
It seems like $this->_request->getPost('pagination') 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...
133
			'moderation' => $numberFilter->sanitize($this->_request->getPost('moderation')),
0 ignored issues
show
It seems like $this->_request->getPost('moderation') 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...
134
			'registration' => $numberFilter->sanitize($this->_request->getPost('registration')),
0 ignored issues
show
It seems like $this->_request->getPost('registration') 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
			'verification' => $numberFilter->sanitize($this->_request->getPost('verification')),
0 ignored issues
show
It seems like $this->_request->getPost('verification') 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
			'recovery' => $numberFilter->sanitize($this->_request->getPost('recovery')),
0 ignored issues
show
It seems like $this->_request->getPost('recovery') 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...
137
			'captcha' => $numberFilter->sanitize($this->_request->getPost('captcha'))
0 ignored issues
show
It seems like $this->_request->getPost('captcha') 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...
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['charset'] || !$postArray['limit'])
158
		{
159
			$validateArray[] = $this->_language->get('input_empty');
160
		}
161
		return $validateArray;
162
	}
163
164
	/**
165
	 * update the setting
166
	 *
167
	 * @since 4.0.0
168
	 *
169
	 * @param array $updateArray array of the update
170
	 *
171
	 * @return bool
172
	 */
173
174
	protected function _update(array $updateArray = []) : bool
175
	{
176
		$settingModel = new Admin\Model\Setting();
177
		return $settingModel->updateByArray($updateArray);
178
	}
179
180
	/**
181
	 * get error route
182
	 *
183
	 * @since 4.1.0
184
	 *
185
	 * @return string
186
	 */
187
188
	protected function _getErrorRoute() : string
189
	{
190
		if ($this->_registry->get('settingsEdit'))
191
		{
192
			return 'admin/edit/settings';
193
		}
194
		return 'admin';
195
	}
196
}
197