ToJson   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 7
Bugs 1 Features 2
Metric Value
wmc 5
eloc 14
c 7
b 1
f 2
dl 0
loc 31
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A jsonSerialize() 0 24 5
1
<?php
2
3
namespace Riclep\StoryblokForms\Traits;
4
5
use Illuminate\Support\Collection;
6
7
trait ToJson
8
{
9
	/**
10
	 * Converts a field to JSON for VueJS
11
	 *
12
	 * @return Collection
13
	 */
14
	public function jsonSerialize(): mixed
15
	{
16
		// get the validation rules for the field
17
		$rules = array_map_recursive(function ($rule) {
18
			return (string) $rule;
19
		}, $this->validationRules());
0 ignored issues
show
Bug introduced by
It seems like validationRules() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

19
		}, $this->/** @scrutinizer ignore-call */ validationRules());
Loading history...
20
21
		$content = $this->content();
0 ignored issues
show
Bug introduced by
It seems like content() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

21
		/** @scrutinizer ignore-call */ 
22
  $content = $this->content();
Loading history...
22
		$content['validators'] = $rules ? $rules[array_key_first($rules)] : [];
23
		$content['dot_name'] = $this->input_json_dot_name;
24
25
		// see if any settings fields have been added to the field
26
		if (method_exists($this, 'hasSettings') && $this->hasSettings()) {
27
			$content['settings'] = $this->settings();
0 ignored issues
show
Bug introduced by
It seems like settings() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

27
			/** @scrutinizer ignore-call */ 
28
   $content['settings'] = $this->settings();
Loading history...
28
		}
29
30
		// run any field specific additions
31
		if (method_exists($this, 'addToJson')) {
32
			$content = $content->merge($this->addToJson());
33
		}
34
35
		return collect([
0 ignored issues
show
Bug introduced by
array('component' => $th... 'content' => $content) of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

35
		return collect(/** @scrutinizer ignore-type */ [
Loading history...
36
			'component' => $this->component(),
0 ignored issues
show
Bug introduced by
It seems like component() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

36
			'component' => $this->/** @scrutinizer ignore-call */ component(),
Loading history...
37
			'content' => $content,
38
		]);
39
	}
40
}
41