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

includes/Admin/Controller/Module.php (2 issues)

Check for mismatching type of a variable.

Bug Minor

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
8
/**
9
 * children class to process the admin module request
10
 *
11
 * @since 4.0.0
12
 *
13
 * @package Redaxscript
14
 * @category Controller
15
 * @author Henry Ruhs
16
 */
17
18
class Module extends ControllerAbstract
19
{
20
	/**
21
	 * process the class
22
	 *
23
	 * @since 4.0.0
24
	 *
25
	 * @param string $action action to process
26
	 *
27
	 * @return string
28
	 */
29
30
	public function process(string $action = null) : string
31
	{
32
		$postArray = $this->_normalizePost($this->_sanitizePost());
33
		$validateArray = $this->_validatePost($postArray);
34
35
		/* validate post */
36
37
		if ($validateArray)
38
		{
39
			return $this->_error(
40
			[
41
				'route' => $this->_getErrorRoute($postArray),
42
				'message' => $validateArray
43
			]);
44
		}
45
46
		/* handle update */
47
48
		if ($action === 'update')
49
		{
50
			$updateArray =
51
			[
52
				'name' => $postArray['name'],
53
				'description' => $postArray['description'],
54
				'status' => $postArray['status'],
55
				'access' => $postArray['access']
56
			];
57
			if ($this->_update($postArray['id'], $updateArray))
58
			{
59
				return $this->_success(
60
				[
61
					'route' => $this->_getSuccessRoute($postArray),
62
					'timeout' => 2
63
				]);
64
			}
65
		}
66
67
		/* handle error */
68
69
		return $this->_error(
70
		[
71
			'route' => $this->_getErrorRoute($postArray)
72
		]);
73
	}
74
75
	/**
76
	 * sanitize the post
77
	 *
78
	 * @since 4.0.0
79
	 *
80
	 * @return array
81
	 */
82
83
	protected function _sanitizePost() : array
84
	{
85
		$numberFilter = new Filter\Number();
86
87
		/* sanitize post */
88
89
		return
90
		[
91
			'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...
92
			'name' => $this->_request->getPost('name'),
93
			'description' => $this->_request->getPost('description'),
94
			'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...
95
			'access' => json_encode($this->_request->getPost('access'))
96
		];
97
	}
98
99
	/**
100
	 * validate the post
101
	 *
102
	 * @since 4.0.0
103
	 *
104
	 * @param array $postArray array of the post
105
	 *
106
	 * @return array
107
	 */
108
109
	protected function _validatePost(array $postArray = []) : array
110
	{
111
		$validateArray = [];
112
113
		/* validate post */
114
115
		if (!$postArray['name'])
116
		{
117
			$validateArray[] = $this->_language->get('name_empty');
118
		}
119
		return $validateArray;
120
	}
121
122
	/**
123
	 * update the module
124
	 *
125
	 * @since 4.0.0
126
	 *
127
	 * @param int $moduleId identifier of the module
128
	 * @param array $updateArray array of the update
129
	 *
130
	 * @return bool
131
	 */
132
133
	protected function _update(int $moduleId = null, array $updateArray = []) : bool
134
	{
135
		$moduleModel = new Admin\Model\Module();
136
		return $moduleModel->updateByIdAndArray($moduleId, $updateArray);
137
	}
138
139
	/**
140
	 * get success route
141
	 *
142
	 * @since 4.0.0
143
	 *
144
	 * @param array $postArray array of the post
145
	 *
146
	 * @return string
147
	 */
148
149
	protected function _getSuccessRoute(array $postArray = []) : string
150
	{
151
		if ($this->_registry->get('modulesEdit') && $postArray['id'])
152
		{
153
			return 'admin/view/modules#row-' . $postArray['id'];
154
		}
155
		return 'admin';
156
	}
157
158
	/**
159
	 * get error route
160
	 *
161
	 * @since 4.0.0
162
	 *
163
	 * @param array $postArray array of the post
164
	 *
165
	 * @return string
166
	 */
167
168
	protected function _getErrorRoute(array $postArray = []) : string
169
	{
170
		if ($this->_registry->get('modulesEdit'))
171
		{
172
			if ($postArray['id'])
173
			{
174
				return 'admin/edit/modules/' . $postArray['id'];
175
			}
176
			return 'admin/view/modules';
177
		}
178
		return 'admin';
179
	}
180
}
181