HasFieldsRules::wrapScopedFieldRules()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace NovaFlexibleContent\Layouts\LayoutTraits;
4
5
use Illuminate\Support\Str;
6
use Laravel\Nova\Fields\Field;
7
use NovaFlexibleContent\Http\FlexibleAttribute;
8
use NovaFlexibleContent\Http\ScopedRequest;
9
10
trait HasFieldsRules
11
{
12
    /**
13
     * Get validation rules for fields concerned by given request.
14
     */
15 6
    public function generateRules(ScopedRequest $request, string $key, ?string $type = null): array
16
    {
17 6
        return $this->fieldsCollection()->map(fn ($field) => $this->getScopedFieldRules($field, $request, $key, $type))
0 ignored issues
show
Bug introduced by
It seems like fieldsCollection() 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

17
        return $this->/** @scrutinizer ignore-call */ fieldsCollection()->map(fn ($field) => $this->getScopedFieldRules($field, $request, $key, $type))
Loading history...
18 6
            ->collapse()
19 6
            ->all();
20
    }
21
22
    /**
23
     * Get validation rules for fields concerned by given request.
24
     */
25 6
    protected function getScopedFieldRules(Field $field, ScopedRequest $request, string $key, ?string $type = null): array
26
    {
27 6
        $type   = Str::ucfirst($type);
28 6
        $method = "get{$type}Rules";
29
30 6
        $rules = [];
31 6
        if(method_exists($field, $method)) {
32 6
            $rules = call_user_func([$field, $method], $request);
33
        }
34
35 6
        return collect($rules)
0 ignored issues
show
Bug introduced by
It seems like $rules can also be of type array; however, parameter $value of collect() does only seem to accept Illuminate\Contracts\Support\Arrayable, maybe add an additional type check? ( 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 */ $rules)
Loading history...
36 6
            ->mapWithKeys(function ($validatorRules, $attribute) use ($key, $field, $request) {
37 6
                $key = $request->isFileAttribute($attribute)
38 2
                    ? $request->getFileAttribute($attribute)
39 6
                    : "{$key}.attributes.{$attribute}";
40
41 6
                return [$key => $this->wrapScopedFieldRules($field, $validatorRules)];
42 6
            })
43 6
            ->filter()
44 6
            ->all();
45
    }
46
47
    /**
48
     * Wrap the rules in an array containing field information for later use.
49
     */
50 6
    protected function wrapScopedFieldRules(Field $field, array $rules = []): array
51
    {
52 6
        if (is_a($rules['attribute'] ?? null, FlexibleAttribute::class)) {
53 1
            return $rules;
54
        }
55
56 6
        return [
57 6
            'attribute' => FlexibleAttribute::make($field->attribute, $this->inUseKey()),
0 ignored issues
show
Bug introduced by
It seems like inUseKey() 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

57
            'attribute' => FlexibleAttribute::make($field->attribute, $this->/** @scrutinizer ignore-call */ inUseKey()),
Loading history...
58 6
            'rules'     => $rules,
59 6
        ];
60
    }
61
}
62