Completed
Push — master ( aaa756...6a04f6 )
by Henry
70:00 queued 35:28
created

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