ArticleForm::render()   F
last analyzed

Complexity

Conditions 18
Paths 12

Size

Total Lines 400

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 342

Importance

Changes 0
Metric Value
dl 0
loc 400
ccs 0
cts 176
cp 0
rs 3.8933
c 0
b 0
f 0
cc 18
nc 12
nop 1
crap 342

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Redaxscript\Admin\View;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Dater;
6
use Redaxscript\Html;
7
use Redaxscript\Module;
8
use Redaxscript\Validator;
9
use function count;
10
use function htmlspecialchars;
11
use function json_decode;
12
13
/**
14
 * children class to create the article form
15
 *
16
 * @since 3.0.0
17
 *
18
 * @package Redaxscript
19
 * @category View
20
 * @author Henry Ruhs
21
 */
22
23
class ArticleForm extends ViewAbstract
24
{
25
	/**
26
	 * render the view
27
	 *
28
	 * @since 3.0.0
29
	 *
30
	 * @param int $articleId identifier of the article
31
	 *
32
	 * @return string
33
	 */
34
35
	public function render(int $articleId = null) : string
36
	{
37
		$output = Module\Hook::trigger('adminArticleFormStart');
38
		$helperOption = new Helper\Option($this->_language);
39
		$articleModel = new Admin\Model\Article();
40
		$article = $articleModel->getById($articleId);
41
		$aliasValidator = new Validator\Alias();
42
		$dater = new Dater();
43
		$dater->init($article->date);
44
45
		/* html element */
46
47
		$titleElement = new Html\Element();
48
		$titleElement
49
			->init('h2',
50
			[
51
				'class' => 'rs-admin-title-content',
52
			])
53
			->text($article->title ? : $this->_language->get('article_new'));
54
		$formElement = new Admin\Html\Form($this->_registry, $this->_language);
55
		$formElement->init(
56
		[
57
			'form' =>
58
			[
59
				'class' => 'rs-admin-js-validate rs-admin-js-alias rs-admin-fn-tab rs-admin-component-tab rs-admin-form-default'
60
			],
61
			'button' =>
62
			[
63
				'create' =>
64
				[
65
					'name' => self::class
66
				],
67
				'save' =>
68
				[
69
					'name' => self::class
70
				]
71
			],
72
			'link' =>
73
			[
74
				'cancel' =>
75
				[
76
					'href' => $this->_registry->get('articlesEdit') && $this->_registry->get('articlesDelete') ? $this->_registry->get('parameterRoute') . 'admin/view/articles' : $this->_registry->get('parameterRoute') . 'admin'
77
				],
78
				'delete' =>
79
				[
80
					'href' => $article->id ? $this->_registry->get('parameterRoute') . 'admin/delete/articles/' . $article->id . '/' . $this->_registry->get('token') : null
81
				]
82
			]
83
		]);
84
85
		/* create the form */
86
87
		$formElement
88
89
			/* article */
90
91
			->radio(
92
			[
93
				'id' => self::class . '\Article',
94
				'class' => 'rs-admin-fn-status-tab',
95
				'name' => self::class . '\Tab',
96
				'checked' => 'checked'
97
			])
98
			->label($this->_language->get('article'),
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $this->_language->get('article') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
99
			[
100
				'class' => 'rs-admin-fn-toggle-tab rs-admin-label-tab',
101
				'for' => self::class . '\Article'
102
			])
103
			->append('<ul class="rs-admin-fn-content-tab rs-admin-box-tab"><li>')
104
			->label($this->_language->get('title'),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_language->get('title') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
105
			[
106
				'for' => 'title'
107
			])
108
			->text(
109
			[
110
				'autofocus' => 'autofocus',
111
				'class' => 'rs-admin-js-alias-input rs-admin-field-default rs-admin-field-text',
112
				'id' => 'title',
113
				'name' => 'title',
114
				'required' => 'required',
115
				'value' => $article->title
116
			])
117
			->append('</li><li>')
118
			->label($this->_language->get('alias'),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_language->get('alias') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
119
			[
120
				'for' => 'alias'
121
			])
122
			->text(
123
			[
124
				'class' => 'rs-admin-js-alias-output rs-admin-field-default rs-admin-field-text',
125
				'id' => 'alias',
126
				'name' => 'alias',
127
				'pattern' => $aliasValidator->getFormPattern(),
128
				'required' => 'required',
129
				'value' => $article->alias
130
			])
131
			->append('</li><li>')
132
			->label($this->_language->get('description'),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_language->get('description') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
133
			[
134
				'for' => 'description'
135
			])
136
			->textarea(
137
			[
138
				'class' => 'rs-admin-js-resize rs-admin-field-textarea rs-admin-field-small',
139
				'id' => 'description',
140
				'name' => 'description',
141
				'rows' => 1,
142
				'value' => $article->description
143
			])
144
			->append('</li><li>')
145
			->label($this->_language->get('keywords'),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_language->get('keywords') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
146
			[
147
				'for' => 'keywords'
148
			])
149
			->textarea(
150
			[
151
				'class' => 'rs-admin-js-resize rs-admin-field-textarea rs-admin-field-small',
152
				'id' => 'keywords',
153
				'name' => 'keywords',
154
				'rows' => 1,
155
				'value' => $article->keywords
156
			])
157
			->append('</li><li>')
158
			->label($this->_language->get('robots'),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_language->get('robots') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
159
			[
160
				'for' => 'robots'
161
			])
162
			->select($helperOption->getRobotArray(),
163
			[
164
				$article->robots
165
			],
166
			[
167
				'id' => 'robots',
168
				'name' => 'robots'
169
			])
170
			->append('</li><li>')
171
			->label($this->_language->get('text'),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_language->get('text') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
172
			[
173
				'for' => 'text'
174
			])
175
			->textarea(
176
			[
177
				'class' => 'rs-admin-js-editor rs-admin-js-resize rs-admin-field-textarea',
178
				'id' => 'text',
179
				'name' => 'text',
180
				'required' => 'required',
181
				'value' => htmlspecialchars($article->text, ENT_QUOTES)
182
			])
183
			->append('</li></ul>')
184
185
			/* general */
186
187
			->radio(
188
			[
189
				'id' => self::class . '\General',
190
				'class' => 'rs-admin-fn-status-tab',
191
				'name' => self::class . '\Tab'
192
			])
193
			->label($this->_language->get('general'),
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $this->_language->get('general') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
194
			[
195
				'class' => 'rs-admin-fn-toggle-tab rs-admin-label-tab',
196
				'for' => self::class . '\General'
197
			])
198
			->append('<ul class="rs-admin-fn-content-tab rs-admin-box-tab"><li>')
199
			->label($this->_language->get('language'),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_language->get('language') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
200
			[
201
				'for' => 'language'
202
			])
203
			->select($helperOption->getLanguageArray(),
204
			[
205
				$article->language
206
			],
207
			[
208
				'id' => 'language',
209
				'name' => 'language'
210
			])
211
			->append('</li><li>')
212
			->label($this->_language->get('template'),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_language->get('template') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
213
			[
214
				'for' => 'template'
215
			])
216
			->select($helperOption->getTemplateArray(),
217
			[
218
				$article->template
219
			],
220
			[
221
				'id' => 'template',
222
				'name' => 'template'
223
			])
224
			->append('</li><li>')
225
			->label($this->_language->get('article_sibling'),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_language->get('article_sibling') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
226
			[
227
				'for' => 'sibling'
228
			])
229
			->select($helperOption->getSiblingForArticleArray($article->id),
230
			[
231
				$article->sibling
232
			],
233
			[
234
				'id' => 'sibling',
235
				'name' => 'sibling'
236
			])
237
			->append('</li><li>')
238
			->label($this->_language->get('category'),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_language->get('category') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
239
			[
240
				'for' => 'category'
241
			])
242
			->select($helperOption->getCategoryArray(),
243
			[
244
				$article->category
245
			],
246
			[
247
				'id' => 'category',
248
				'name' => 'category'
249
			])
250
			->append('</li></ul>')
251
252
			/* customize */
253
254
			->radio(
255
			[
256
				'id' => self::class . '\Customize',
257
				'class' => 'rs-admin-fn-status-tab',
258
				'name' => self::class . '\Tab'
259
			])
260
			->label($this->_language->get('customize'),
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $this->_language->get('customize') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
261
			[
262
				'class' => 'rs-admin-fn-toggle-tab rs-admin-label-tab',
263
				'for' => self::class . '\Customize'
264
			])
265
			->append('<ul class="rs-admin-fn-content-tab rs-admin-box-tab"><li>')
266
			->label($this->_language->get('headline'),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_language->get('headline') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
267
			[
268
				'for' => 'headline'
269
			])
270
			->checkbox(!$article->id || $article->headline ?
271
			[
272
				'id' => 'headline',
273
				'class' => 'rs-admin-fn-status-switch',
274
				'name' => 'headline',
275
				'checked' => 'checked'
276
			] :
277
			[
278
				'id' => 'headline',
279
				'class' => 'rs-admin-fn-status-switch',
280
				'name' => 'headline'
281
			])
282
			->label(null,
283
			[
284
				'class' => 'rs-admin-label-switch',
285
				'for' => 'headline',
286
				'data-on' => $this->_language->get('enable'),
287
				'data-off' => $this->_language->get('disable')
288
			])
289
			->append('</li><li>')
290
			->label($this->_language->get('byline'),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_language->get('byline') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
291
			[
292
				'for' => 'byline'
293
			])
294
			->checkbox(!$article->id || $article->byline ?
295
			[
296
				'id' => 'byline',
297
				'class' => 'rs-admin-fn-status-switch',
298
				'name' => 'byline',
299
				'checked' => 'checked'
300
			] :
301
			[
302
				'id' => 'byline',
303
				'class' => 'rs-admin-fn-status-switch',
304
				'name' => 'byline'
305
			])
306
			->label(null,
307
			[
308
				'class' => 'rs-admin-label-switch',
309
				'for' => 'byline',
310
				'data-on' => $this->_language->get('enable'),
311
				'data-off' => $this->_language->get('disable')
312
			])
313
			->append('</li><li>')
314
			->label($this->_language->get('comments'),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_language->get('comments') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
315
			[
316
				'for' => 'comments'
317
			])
318
			->checkbox($article->comments ?
319
			[
320
				'id' => 'comments',
321
				'class' => 'rs-admin-fn-status-switch',
322
				'name' => 'comments',
323
				'checked' => 'checked'
324
			] :
325
			[
326
				'id' => 'comments',
327
				'class' => 'rs-admin-fn-status-switch',
328
				'name' => 'comments'
329
			])
330
			->label(null,
331
			[
332
				'class' => 'rs-admin-label-switch',
333
				'for' => 'comments',
334
				'data-on' => $this->_language->get('enable'),
335
				'data-off' => $this->_language->get('disable')
336
			])
337
			->append('</li><li>')
338
			->label($this->_language->get('status'),
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $this->_language->get('status') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
339
			[
340
				'for' => 'status'
341
			])
342
			->checkbox(!$article->id || $article->status ?
343
			[
344
				'id' => 'status',
345
				'class' => 'rs-admin-fn-status-switch',
346
				'name' => 'status',
347
				'checked' => 'checked'
348
			] :
349
			[
350
				'id' => 'status',
351
				'class' => 'rs-admin-fn-status-switch',
352
				'name' => 'status'
353
			])
354
			->label(null,
355
			[
356
				'class' => 'rs-admin-label-switch',
357
				'for' => 'status',
358
				'data-on' => $this->_language->get('publish'),
359
				'data-off' => $this->_language->get('unpublish')
360
			])
361
			->append('</li><li>')
362
			->label($this->_language->get('rank'),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_language->get('rank') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
363
			[
364
				'for' => 'rank'
365
			])
366
			->number(
367
			[
368
				'id' => 'rank',
369
				'name' => 'rank',
370
				'value' => $article->id ? $article->rank : $articleModel->query()->max('rank') + 1
371
			])
372
			->append('</li>');
373
		if ($this->_registry->get('groupsEdit'))
374
		{
375
			$formElement
376
				->append('<li>')
377
				->label($this->_language->get('access'),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_language->get('access') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
378
				[
379
					'for' => 'access'
380
				])
381
				->select($helperOption->getGroupArray(),
382
				(array)json_decode($article->access),
383
				[
384
					'id' => 'access',
385
					'name' => 'access[]',
386
					'multiple' => 'multiple',
387
					'size' => count($helperOption->getGroupArray())
388
				])
389
				->append('</li>');
390
		}
391
		$formElement
392
			->append('<li>')
393
			->label($this->_language->get('date'),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_language->get('date') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() 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...
394
			[
395
				'for' => 'date'
396
			])
397
			->datetime(
398
			[
399
				'id' => 'date',
400
				'name' => 'date',
401
				'value' => $dater->formatField()
402
			])
403
			->append('</li></ul>')
404
			->hidden(
405
			[
406
				'name' => 'id',
407
				'value' => $article->id
408
			])
409
			->token()
410
			->append('<div class="rs-admin-wrapper-button">')
411
			->cancel();
412
		if ($article->id)
413
		{
414
			if ($this->_registry->get('articlesDelete'))
415
			{
416
				$formElement->delete();
417
			}
418
			if ($this->_registry->get('articlesEdit'))
419
			{
420
				$formElement->save();
421
			}
422
		}
423
		else if ($this->_registry->get('articlesNew'))
424
		{
425
			$formElement->create();
426
		}
427
		$formElement->append('</div>');
428
429
		/* collect output */
430
431
		$output .= $titleElement . $formElement;
432
		$output .= Module\Hook::trigger('adminArticleFormEnd');
433
		return $output;
434
	}
435
}
436