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

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

call_checks.maybe_mismatching_type_passed_with_def

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);
0 ignored issues
show
It seems like $postArray defined by $this->_normalizePost($this->_sanitizePost()) on line 32 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...
34
35
		/* validate post */
36
37
		if ($validateArray)
38
		{
39
			return $this->_error(
40
			[
41
				'route' => $this->_getErrorRoute($postArray),
0 ignored issues
show
It seems like $postArray defined by $this->_normalizePost($this->_sanitizePost()) on line 32 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...
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)
0 ignored issues
show
It seems like $postArray defined by $this->_normalizePost($this->_sanitizePost()) on line 32 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...
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')),
92
			'name' => $this->_request->getPost('name'),
93
			'description' => $this->_request->getPost('description'),
94
			'status' => $numberFilter->sanitize($this->_request->getPost('status')),
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