Passed
Push — develop ( e66b52...83bea4 )
by Richard
03:56
created

Input::getSizeAttribute()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 3
nc 2
nop 0
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
	 * @var string[] All the Validators for this Input
19
	 */
20
	protected $_casts = ['validators' => Validators::class];
21
22
	public function loopKey($key) {
23
		$this->key = $key;
24
25
		return $this;
26
	}
27
28
29
	/**
30
	 * All the Validation rules for this Input
31
	 *
32
	 * @return mixed
33
	 */
34
	public function validationRules() {
35
		$rules = $this->validators->validationRules();
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 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

35
		/** @scrutinizer ignore-call */ 
36
  $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...
36
37
		if ($this->parent() instanceof LsfConditionalSelect) {
38
			if (is_array($this->settings('lsf_is_conditional')['when_parent_is'])) {
39
				$requiredWhen = implode(',', $this->settings('lsf_is_conditional')['when_parent_is']);
40
			} else {
41
				$requiredWhen = $this->settings('lsf_is_conditional')['when_parent_is'];
42
			}
43
44
			foreach ($rules as $key => $rule) {
45
				if (in_array('required', $rule)) {
46
					$requiredKey = array_search('required', $rule);
47
48
					$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...
49
				}
50
			}
51
		}
52
53
		return $rules;
54
	}
55
56
	/**
57
	 * All the error messages for this Input
58
	 *
59
	 * @return mixed
60
	 */
61
	public function errorMessages() {
62
		$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...
63
64
		/**
65
		 * Rewrite required to required_if for items inside conditional selects
66
		 */
67
		if ($this->parent() instanceof LsfConditionalSelect) {
68
			foreach ($messages as $key => $rule) {
69
				if (Str::endsWith($key, 'required')) {
70
					$messages[$key . '_if'] = $messages[$key];
71
72
					unset($messages[$key]);
73
				}
74
			}
75
		}
76
77
		return $messages;
78
	}
79
}