Input   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 11
eloc 26
c 4
b 1
f 0
dl 0
loc 83
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A loopKey() 0 5 1
A validationRules() 0 25 6
A errorMessages() 0 18 4
1
<?php
2
3
namespace Riclep\StoryblokForms;
4
5
use Illuminate\Support\Str;
6
use Riclep\StoryblokForms\Blocks\LsfConditionalSelect;
7
use Riclep\StoryblokForms\Traits\HasNames;
8
use Riclep\StoryblokForms\Traits\InFieldset;
9
use Riclep\StoryblokForms\Traits\ToJson;
10
11
class Input extends \Riclep\Storyblok\Block
12
{
13
	use HasNames, InFieldset, ToJson;
0 ignored issues
show
Bug introduced by
The trait Riclep\StoryblokForms\Traits\HasNames requires the property $input_name which is not provided by Riclep\StoryblokForms\Input.
Loading history...
Bug introduced by
The trait Riclep\StoryblokForms\Traits\ToJson requires the property $input_json_dot_name which is not provided by Riclep\StoryblokForms\Input.
Loading history...
14
15
	protected $key;
16
17
	/**
18
	 * Casting validators to Validators allow us to take control of how we process the
19
	 * JSON from Storyblok by breaking out of the standard nested Blocks format
20
	 *
21
	 * @var string[] All the Validators for this Input
22
	 */
23
	protected array $_casts = ['validators' => Validators::class];
24
25
	/**
26
	 * Creates a key to be used for the VueJS :key on this input
27
	 *
28
	 * @param $key
29
	 * @return $this
30
	 */
31
	public function loopKey($key): Input
32
	{
33
		$this->key = $key;
34
35
		return $this;
36
	}
37
38
39
	/**
40
	 * All the Validation rules for this Input
41
	 *
42
	 * @return mixed
43
	 */
44
	public function validationRules(): mixed
45
	{
46
		if (!$this->validators) {
0 ignored issues
show
Bug Best Practice introduced by
The property validators does not exist on Riclep\StoryblokForms\Input. Since you implemented __get, consider adding a @property annotation.
Loading history...
47
			return [];
48
		}
49
50
		$rules = $this->validators->validationRules();
51
52
		if ($this->parent() instanceof LsfConditionalSelect) {
53
			if (is_array($this->settings('lsf_is_conditional')['when_parent_is'])) {
0 ignored issues
show
Bug introduced by
The method settings() does not exist on Riclep\StoryblokForms\Input. ( Ignorable by Annotation )

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

53
			if (is_array($this->/** @scrutinizer ignore-call */ settings('lsf_is_conditional')['when_parent_is'])) {

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...
54
				$requiredWhen = implode(',', $this->settings('lsf_is_conditional')['when_parent_is']);
55
			} else {
56
				$requiredWhen = $this->settings('lsf_is_conditional')['when_parent_is'];
57
			}
58
59
			foreach ($rules as $key => $rule) {
60
				if (in_array('required', $rule, true)) {
61
					$requiredKey = array_search('required', $rule);
62
63
					$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...
64
				}
65
			}
66
		}
67
68
		return $rules;
69
	}
70
71
	/**
72
	 * All the error messages for this Input
73
	 *
74
	 * @return mixed
75
	 */
76
	public function errorMessages(): mixed
77
	{
78
		$messages = $this->validators->errorMessages();
0 ignored issues
show
Bug Best Practice introduced by
The property validators does not exist on Riclep\StoryblokForms\Input. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method errorMessages() 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

78
		/** @scrutinizer ignore-call */ 
79
  $messages = $this->validators->errorMessages();

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...
79
80
		/**
81
		 * Rewrite required to required_if for items inside conditional selects
82
		 */
83
		if ($this->parent() instanceof LsfConditionalSelect) {
84
			foreach ($messages as $key => $rule) {
85
				if (Str::endsWith($key, 'required')) {
86
					$messages[$key . '_if'] = $rule;
87
88
					unset($messages[$key]);
89
				}
90
			}
91
		}
92
93
		return $messages;
94
	}
95
}