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 | use function strtotime; |
||
9 | |||
10 | /** |
||
11 | * children class to process the admin article request |
||
12 | * |
||
13 | * @since 4.0.0 |
||
14 | * |
||
15 | * @package Redaxscript |
||
16 | * @category Controller |
||
17 | * @author Henry Ruhs |
||
18 | */ |
||
19 | |||
20 | class Article extends ControllerAbstract |
||
21 | { |
||
22 | /** |
||
23 | * process the class |
||
24 | * |
||
25 | * @since 4.0.0 |
||
26 | * |
||
27 | * @param string $action action to process |
||
28 | * |
||
29 | * @return string |
||
30 | */ |
||
31 | |||
32 | public function process(string $action = null) : string |
||
33 | { |
||
34 | $postArray = $this->_normalizePost($this->_sanitizePost()); |
||
35 | $validateArray = $this->_validatePost($postArray); |
||
0 ignored issues
–
show
|
|||
36 | $myUser = $this->_registry->get('myUser'); |
||
37 | $now = $this->_registry->get('now'); |
||
38 | |||
39 | /* validate post */ |
||
40 | |||
41 | if ($validateArray) |
||
42 | { |
||
43 | return $this->_error( |
||
44 | [ |
||
45 | 'route' => $this->_getErrorRoute($postArray), |
||
0 ignored issues
–
show
It seems like
$postArray defined by $this->_normalizePost($this->_sanitizePost()) on line 34 can also be of type null ; however, Redaxscript\Admin\Contro...ticle::_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...
|
|||
46 | 'message' => $validateArray |
||
47 | ]); |
||
48 | } |
||
49 | |||
50 | /* handle create */ |
||
51 | |||
52 | if ($action === 'create') |
||
53 | { |
||
54 | $createArray = |
||
55 | [ |
||
56 | 'title' => $postArray['title'], |
||
57 | 'alias' => $postArray['alias'], |
||
58 | 'author' => $myUser, |
||
59 | 'description' => $postArray['description'], |
||
60 | 'keywords' => $postArray['keywords'], |
||
61 | 'robots' => $postArray['robots'], |
||
62 | 'text' => $postArray['text'], |
||
63 | 'language' => $postArray['language'], |
||
64 | 'template' => $postArray['template'], |
||
65 | 'sibling' => $postArray['sibling'], |
||
66 | 'category' => $postArray['category'], |
||
67 | 'headline' => $postArray['headline'], |
||
68 | 'byline' => $postArray['byline'], |
||
69 | 'comments' => $postArray['comments'], |
||
70 | 'status' => $postArray['date'] > $now ? 2 : $postArray['status'], |
||
71 | 'rank' => $postArray['rank'], |
||
72 | 'access' => $postArray['access'], |
||
73 | 'date' => $postArray['date'] ? : $now |
||
74 | ]; |
||
75 | if ($this->_create($createArray)) |
||
76 | { |
||
77 | return $this->_success( |
||
78 | [ |
||
79 | 'route' => $this->_getSuccessRoute($postArray), |
||
80 | 'timeout' => 2 |
||
81 | ]); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | /* handle update */ |
||
86 | |||
87 | if ($action === 'update') |
||
88 | { |
||
89 | $updateArray = |
||
90 | [ |
||
91 | 'title' => $postArray['title'], |
||
92 | 'alias' => $postArray['alias'], |
||
93 | 'author' => $myUser, |
||
94 | 'description' => $postArray['description'], |
||
95 | 'keywords' => $postArray['keywords'], |
||
96 | 'robots' => $postArray['robots'], |
||
97 | 'text' => $postArray['text'], |
||
98 | 'language' => $postArray['language'], |
||
99 | 'template' => $postArray['template'], |
||
100 | 'sibling' => $postArray['sibling'], |
||
101 | 'category' => $postArray['category'], |
||
102 | 'headline' => $postArray['headline'], |
||
103 | 'byline' => $postArray['byline'], |
||
104 | 'comments' => $postArray['comments'], |
||
105 | 'status' => $postArray['date'] > $now ? 2 : $postArray['status'], |
||
106 | 'rank' => $postArray['rank'], |
||
107 | 'access' => $postArray['access'], |
||
108 | 'date' => $postArray['date'] ? : $now |
||
109 | ]; |
||
110 | if ($this->_update($postArray['id'], $updateArray)) |
||
111 | { |
||
112 | return $this->_success( |
||
113 | [ |
||
114 | 'route' => $this->_getSuccessRoute($postArray), |
||
115 | 'timeout' => 2 |
||
116 | ]); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | /* handle error */ |
||
121 | |||
122 | return $this->_error( |
||
123 | [ |
||
124 | 'route' => $this->_getErrorRoute($postArray) |
||
0 ignored issues
–
show
It seems like
$postArray defined by $this->_normalizePost($this->_sanitizePost()) on line 34 can also be of type null ; however, Redaxscript\Admin\Contro...ticle::_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...
|
|||
125 | ]); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * sanitize the post |
||
130 | * |
||
131 | * @since 4.0.0 |
||
132 | * |
||
133 | * @return array |
||
134 | */ |
||
135 | |||
136 | protected function _sanitizePost() : array |
||
137 | { |
||
138 | $numberFilter = new Filter\Number(); |
||
139 | $aliasFilter = new Filter\Alias(); |
||
140 | $specialFilter = new Filter\Special(); |
||
141 | $htmlFilter = new Filter\Html(); |
||
142 | |||
143 | /* sanitize post */ |
||
144 | |||
145 | return |
||
146 | [ |
||
147 | 'id' => $numberFilter->sanitize($this->_request->getPost('id')), |
||
148 | 'title' => $this->_request->getPost('title'), |
||
149 | 'alias' => $aliasFilter->sanitize($this->_request->getPost('alias')), |
||
150 | 'description' => $this->_request->getPost('description'), |
||
151 | 'keywords' => $this->_request->getPost('keywords'), |
||
152 | 'robots' => $this->_request->getPost('robots'), |
||
153 | 'text' => $htmlFilter->sanitize($this->_request->getPost('text'), $this->_registry->get('filter')), |
||
154 | 'language' => $specialFilter->sanitize($this->_request->getPost('language')), |
||
155 | 'template' => $specialFilter->sanitize($this->_request->getPost('template')), |
||
156 | 'sibling' => $this->_request->getPost('sibling'), |
||
157 | 'category' => $this->_request->getPost('category'), |
||
158 | 'headline' => $numberFilter->sanitize($this->_request->getPost('headline')), |
||
159 | 'byline' => $numberFilter->sanitize($this->_request->getPost('byline')), |
||
160 | 'comments' => $numberFilter->sanitize($this->_request->getPost('comments')), |
||
161 | 'status' => $numberFilter->sanitize($this->_request->getPost('status')), |
||
162 | 'rank' => $numberFilter->sanitize($this->_request->getPost('rank')), |
||
163 | 'access' => json_encode($this->_request->getPost('access')), |
||
164 | 'date' => strtotime($this->_request->getPost('date')) |
||
165 | ]; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * validate the post |
||
170 | * |
||
171 | * @since 4.0.0 |
||
172 | * |
||
173 | * @param array $postArray array of the post |
||
174 | * |
||
175 | * @return array |
||
176 | */ |
||
177 | |||
178 | protected function _validatePost(array $postArray = []) : array |
||
179 | { |
||
180 | $aliasValidator = new Validator\Alias(); |
||
181 | $articleModel = new Admin\Model\Article(); |
||
182 | $validateArray = []; |
||
183 | |||
184 | /* validate post */ |
||
185 | |||
186 | if (!$postArray['title']) |
||
187 | { |
||
188 | $validateArray[] = $this->_language->get('title_empty'); |
||
189 | } |
||
190 | if (!$postArray['alias']) |
||
191 | { |
||
192 | $validateArray[] = $this->_language->get('alias_empty'); |
||
193 | } |
||
194 | else if ($aliasValidator->validate($postArray['alias'], 'general') || $aliasValidator->validate($postArray['alias'], 'system')) |
||
195 | { |
||
196 | $validateArray[] = $this->_language->get('alias_incorrect'); |
||
197 | } |
||
198 | else if (!$articleModel->isUniqueByIdAndAlias($postArray['id'], $postArray['alias'])) |
||
199 | { |
||
200 | $validateArray[] = $this->_language->get('alias_exists'); |
||
201 | } |
||
202 | if (!$postArray['text']) |
||
203 | { |
||
204 | $validateArray[] = $this->_language->get('article_empty'); |
||
205 | } |
||
206 | return $validateArray; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * create the article |
||
211 | * |
||
212 | * @since 4.0.0 |
||
213 | * |
||
214 | * @param array $createArray array of the create |
||
215 | * |
||
216 | * @return bool |
||
217 | */ |
||
218 | |||
219 | protected function _create(array $createArray = []) : bool |
||
220 | { |
||
221 | $articleModel = new Admin\Model\Article(); |
||
222 | return $articleModel->createByArray($createArray); |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * update the article |
||
227 | * |
||
228 | * @since 4.0.0 |
||
229 | * |
||
230 | * @param int $articleId identifier of the article |
||
231 | * @param array $updateArray array of the update |
||
232 | * |
||
233 | * @return bool |
||
234 | */ |
||
235 | |||
236 | protected function _update(int $articleId = null, array $updateArray = []) : bool |
||
237 | { |
||
238 | $articleModel = new Admin\Model\Article(); |
||
239 | return $articleModel->updateByIdAndArray($articleId, $updateArray); |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * get success route |
||
244 | * |
||
245 | * @since 4.0.0 |
||
246 | * |
||
247 | * @param array $postArray array of the post |
||
248 | * |
||
249 | * @return string |
||
250 | */ |
||
251 | |||
252 | protected function _getSuccessRoute(array $postArray = []) : string |
||
253 | { |
||
254 | if ($this->_registry->get('articlesEdit') && $postArray['id']) |
||
255 | { |
||
256 | return 'admin/view/articles#row-' . $postArray['id']; |
||
257 | } |
||
258 | if ($this->_registry->get('articlesEdit') && $postArray['alias']) |
||
259 | { |
||
260 | $articleModel = new Admin\Model\Article(); |
||
261 | return 'admin/view/articles#row-' . $articleModel->getByAlias($postArray['alias'])->id; |
||
262 | } |
||
263 | return 'admin'; |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * get error route |
||
268 | * |
||
269 | * @since 4.0.0 |
||
270 | * |
||
271 | * @param array $postArray array of the post |
||
272 | * |
||
273 | * @return string |
||
274 | */ |
||
275 | |||
276 | protected function _getErrorRoute(array $postArray = []) : string |
||
277 | { |
||
278 | if ($this->_registry->get('articlesEdit') && $postArray['id']) |
||
279 | { |
||
280 | return 'admin/edit/articles/' . $postArray['id']; |
||
281 | } |
||
282 | if ($this->_registry->get('articlesNew')) |
||
283 | { |
||
284 | return 'admin/new/articles'; |
||
285 | } |
||
286 | return 'admin'; |
||
287 | } |
||
288 | } |
||
289 |
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:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.