Completed
Push — master ( e9cd30...c2303d )
by Henry
05:05
created

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