Passed
Push — develop ( a09052...5332d7 )
by Richard
02:46
created

LsfConditionalSelect   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 19
c 2
b 0
f 0
dl 0
loc 61
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A response() 0 14 2
A validationRules() 0 8 1
A errorMessages() 0 8 1
1
<?php
2
3
namespace Riclep\StoryblokForms\Blocks;
4
5
use Riclep\StoryblokForms\MultiInput;
6
7
class LsfConditionalSelect extends MultiInput
8
{
9
	/**
10
	 * @var string
11
	 */
12
	protected $optionsName = 'options';
13
14
	protected $type = 'multi-input';
15
16
17
18
19
	/**
20
	 * Returns all the validation rules for the fields in this Fieldset
21
	 *
22
	 * @return array
23
	 */
24
	public function validationRules() {
25
		$rules = [];
26
27
		$this->fields->each(function ($field) use (&$rules) {
0 ignored issues
show
Bug Best Practice introduced by
The property fields does not exist on Riclep\StoryblokForms\Blocks\LsfConditionalSelect. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method each() does not exist on null. ( Ignorable by Annotation )

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

27
		$this->fields->/** @scrutinizer ignore-call */ 
28
                 each(function ($field) use (&$rules) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
28
			$rules = array_merge($rules, $field->validationRules());
29
		});
30
31
		return $rules;
32
	}
33
34
	/**
35
	 * Returns all the error messages for the fields in this Fieldset
36
	 *
37
	 * @return array
38
	 */
39
	public function errorMessages() {
40
		$rules = [];
41
42
		$this->fields->each(function ($field) use (&$rules) {
0 ignored issues
show
Bug Best Practice introduced by
The property fields does not exist on Riclep\StoryblokForms\Blocks\LsfConditionalSelect. Since you implemented __get, consider adding a @property annotation.
Loading history...
43
			$rules = array_merge($rules, $field->errorMessages());
44
		});
45
46
		return $rules;
47
	}
48
49
50
51
	// TODO - store the selected input value
52
	// TODO - only respond with conditionally on inputs
53
54
	public function response($input) {
55
		return [
56
			'label' => $this->label,
0 ignored issues
show
Bug Best Practice introduced by
The property label does not exist on Riclep\StoryblokForms\Blocks\LsfConditionalSelect. Since you implemented __get, consider adding a @property annotation.
Loading history...
57
			'response' => $this->fields->map(function ($field) use ($input) {
0 ignored issues
show
Bug Best Practice introduced by
The property fields does not exist on Riclep\StoryblokForms\Blocks\LsfConditionalSelect. Since you implemented __get, consider adding a @property annotation.
Loading history...
58
59
				// Handle empty radio buttons etc. sending nothing in POST request
60
				if (!array_key_exists($field->name, $input)) {
61
					$input[$field->name] = null;
62
				}
63
64
65
				return $field->response($input[$field->name]);
66
			})->toArray(),
67
			'type' => $this->type,
68
		];
69
	}
70
}