Test Failed
Push — main ( 706477...92dbda )
by Yaroslav
03:43
created

HasFieldsRules::getScopedFieldRules()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3.0026

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
nc 2
nop 4
dl 0
loc 20
ccs 14
cts 15
cp 0.9333
crap 3.0026
rs 9.8333
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 1
    public function generateRules(ScopedRequest $request, string $key, ?string $type = null): array
16
    {
17 1
        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 1
            ->collapse()
19 1
            ->all();
20
    }
21
22
    /**
23
     * Get validation rules for fields concerned by given request.
24
     */
25 1
    protected function getScopedFieldRules(Field $field, ScopedRequest $request, string $key, ?string $type = null): array
26
    {
27 1
        $type   = Str::ucfirst($type);
28 1
        $method = "get{$type}Rules";
29
30 1
        $rules = [];
31 1
        if(method_exists($field, $method)) {
32 1
            $rules = call_user_func([$field, $method], $request);
33
        }
34
35 1
        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 1
            ->mapWithKeys(function ($validatorRules, $attribute) use ($key, $field, $request) {
37 1
                $key = $request->isFileAttribute($attribute)
38
                    ? $request->getFileAttribute($attribute)
39 1
                    : "{$key}.attributes.{$attribute}";
40
41 1
                return [$key => $this->wrapScopedFieldRules($field, $validatorRules)];
42 1
            })
43 1
            ->filter()
44 1
            ->all();
45
    }
46
47
    /**
48
     * Wrap the rules in an array containing field information for later use.
49
     */
50 1
    protected function wrapScopedFieldRules(Field $field, array $rules = []): array
51
    {
52 1
        if (is_a($rules['attribute'] ?? null, FlexibleAttribute::class)) {
53
            return $rules;
54
        }
55
56 1
        return [
57 1
            '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 1
            'rules'     => $rules,
59 1
        ];
60
    }
61
}
62