Passed
Push — develop ( 34ba8b...8b818b )
by Richard
03:44
created

LsfAddress   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 25
c 1
b 0
f 0
dl 0
loc 72
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A response() 0 6 1
A validationRules() 0 23 5
A errorMessages() 0 17 4
1
<?php
2
3
namespace Riclep\StoryblokForms\Blocks;
4
5
use Illuminate\Support\Str;
6
use Riclep\StoryblokForms\Input;
7
8
class LsfAddress extends Input
9
{
10
	// Interface this....
11
12
	protected $type = 'address';
13
14
	/**
15
	 * All the Validation rules for this Input
16
	 *
17
	 * @return mixed
18
	 */
19
	public function validationRules() {
20
		$rules = $this->validators->validationRules();
0 ignored issues
show
Bug Best Practice introduced by
The property validators does not exist on Riclep\StoryblokForms\Blocks\LsfAddress. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method validationRules() 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

20
		/** @scrutinizer ignore-call */ 
21
  $rules = $this->validators->validationRules();

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...
21
22
		/**
23
		 * Rewrite required to required_if for items inside conditional selects
24
		 */
25
		if ($this->parent() instanceof LsfConditionalSelect) {
26
			if (is_array($this->settings('lsf_is_conditional')['when_parent_is'])) {
27
				$requiredWhen = implode(',', $this->settings('lsf_is_conditional')['when_parent_is']);
28
			} else {
29
				$requiredWhen = $this->settings('lsf_is_conditional')['when_parent_is'];
30
			}
31
32
			foreach ($rules as $key => $rule) {
33
				if (in_array('required', $rule)) {
34
					$requiredKey = array_search('required', $rule);
35
36
					$rules[$key][$requiredKey] = 'required_if:' . $this->parent()->input_dot_name . '.selected,' . $requiredWhen;
0 ignored issues
show
Bug Best Practice introduced by
The property input_dot_name does not exist on Riclep\StoryblokForms\Blocks\LsfConditionalSelect. Since you implemented __get, consider adding a @property annotation.
Loading history...
37
				}
38
			}
39
		}
40
41
		return $rules;
42
	}
43
44
	/**
45
	 * All the Validation rules for this Input
46
	 *
47
	 * @return mixed
48
	 */
49
	public function errorMessages() {
50
		$messages = $this->validators->errorMessages();
0 ignored issues
show
Bug Best Practice introduced by
The property validators does not exist on Riclep\StoryblokForms\Blocks\LsfAddress. Since you implemented __get, consider adding a @property annotation.
Loading history...
51
52
		/**
53
		 * Rewrite required to required_if for items inside conditional selects
54
		 */
55
		if ($this->parent() instanceof LsfConditionalSelect) {
56
			foreach ($messages as $key => $rule) {
57
				if (Str::endsWith($key, 'required')) {
58
					$messages[$key . '_if'] = $messages[$key];
59
60
					unset($messages[$key]);
61
				}
62
			}
63
		}
64
65
		return $messages;
66
	}
67
68
	/**
69
	 * Returns the Input’s response after the form has been submitted and validated
70
	 *
71
	 * @param $input
72
	 * @return mixed
73
	 */
74
	public function response($input) {
75
		return [
76
			'label' => $this->label,
0 ignored issues
show
Bug Best Practice introduced by
The property label does not exist on Riclep\StoryblokForms\Blocks\LsfAddress. Since you implemented __get, consider adding a @property annotation.
Loading history...
77
			'name' => $this->name,
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Riclep\StoryblokForms\Blocks\LsfAddress. Since you implemented __get, consider adding a @property annotation.
Loading history...
78
			'response' => $input,
79
			'type' => $this->type,
80
		];
81
	}
82
}