Form::addElements()   B
last analyzed

Complexity

Conditions 11
Paths 14

Size

Total Lines 58
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 34
nc 14
nop 2
dl 0
loc 58
rs 7.3166
c 1
b 0
f 0

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
3
namespace Epesi\Core\System\View;
4
5
use atk4\ui\Form as BaseForm;
6
use Epesi\Core\System\Modules\Concerns\Notifies;
7
use atk4\ui\Label;
8
9
class Form extends BaseForm
10
{
11
	use Notifies;
12
	
13
	public $buttonSave = null;
14
	
15
	protected $controlRules = [];
16
	protected $validationRules = [];
17
	
18
	public function addElements($elements, $parent = null) {
19
		$parent = $parent?: $this;
20
		
21
		foreach ($elements as $name => $desc) {
22
			$name = $desc['name']?? $name;
23
			
24
			$this->addControlRules($name, $desc['rules']?? []);
25
			
26
			switch ($desc['type']?? 'field') {
27
				case 'field':
28
					$desc = is_string($desc)? [
29
						'decorator' => [$desc]
30
					]: $desc;
31
					
32
					$field = $parent->addControl($name, $desc['decorator']?? [], $desc['options']?? []);
33
					
34
					if ($default = $desc['default']) {
35
						$field->set($default);
36
					}
37
					
38
					if ($desc['display']?? false) {
39
						$this->addControlDisplayRules([$name => $desc['display']]);
40
					}
41
					break;
42
					
43
				case 'group':
44
					$seed = $desc['seed']?? [$name];
45
					
46
					$group = $parent->addGroup($seed);
47
					
48
					$this->addElements($desc['elements'], $group);
49
					
50
					if ($desc['display']?? false) {
51
						$this->addGroupDisplayRules([$name => $desc['display']]);
52
					}
53
					break;
54
					
55
				case 'header':
56
					$seed = $desc['seed']?? [$name];
57
					
58
					$parent->addHeader($seed);
0 ignored issues
show
Bug introduced by
It seems like $seed can also be of type array; however, parameter $title of atk4\ui\Form::addHeader() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
					$parent->addHeader(/** @scrutinizer ignore-type */ $seed);
Loading history...
59
					break;
60
					
61
				case 'view':
62
					$seed = $desc['seed']?? [Label::class, $name];
63
					
64
					$region = $desc['region']?? null;
65
					
66
					$this->add($seed, $region);
0 ignored issues
show
Bug introduced by
It seems like $seed can also be of type array<integer,mixed|string>; however, parameter $object of atk4\ui\View::add() does only seem to accept atk4\ui\View, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
					$this->add(/** @scrutinizer ignore-type */ $seed, $region);
Loading history...
67
					break;
68
					
69
				default:
70
					;
71
					break;
72
			}
73
		}
74
		
75
		return $this;
76
	}
77
	
78
	public function addControlDisplayRules($controlDisplayRules) {
79
		$this->setControlsDisplayRules(array_merge($this->controlDisplayRules?: [], $controlDisplayRules));
80
	}
81
	
82
	public function addGroupDisplayRules($groupDisplayRules) {
83
		$controlDisplayRules = $this->controlDisplayRules;
84
		
85
		$this->setGroupDisplayRules($groupDisplayRules);
86
		
87
		$this->addControlDisplayRules($controlDisplayRules);
88
	}
89
	
90
	public function addControlRules($field, $rules = []) {
91
		if (! $rules) return;
92
		
93
		$this->controlRules[$field] = $rules['rules']?? [
94
				'identifier' => $field,
95
				'rules' => $rules
96
		];
97
		
98
		return $this;
99
	}
100
	
101
	public function validate($callback)
102
	{
103
		$this->setApiConfig([
104
				'beforeSend' => new \atk4\ui\JsFunction(['settings'], [
105
						new \atk4\ui\JsExpression('return $(this).form("is valid")')
106
				]),
107
		]);
108
		
109
		$this->setFormConfig([
110
				'fields' => $this->controlRules
111
		]);
112
		
113
		$this->onSubmit(function ($form) use ($callback) {
114
			$errors = [];
115
			foreach ($this->validationRules?: [] as $ruleCallback) {
116
				if (! is_callable($ruleCallback)) continue;
117
				
118
				$ruleErrors = $ruleCallback($form);
119
				
120
				$errors = array_merge($errors, $ruleErrors?: []);
121
			}
122
			
123
			return $errors?: $callback($this);
124
		});
125
			
126
		return $this;
127
	}
128
	
129
	public function submit()
130
	{
131
		return $this->js()->form('submit');
132
	}
133
	
134
	public function confirmLeave($confirm = true)
135
	{
136
		$this->canLeave = ! $confirm;
137
		
138
		return $this;
139
	}
140
}