findGroupRecursiveAndSetAttribute()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
nc 3
nop 3
dl 0
loc 19
ccs 10
cts 10
cp 1
crap 3
rs 9.9666
c 1
b 0
f 0
1
<?php
2
3
namespace NovaFlexibleContent\Layouts\LayoutTraits;
4
5
use NovaFlexibleContent\Flexible;
6
use NovaFlexibleContent\Layouts\Layout;
7
8
trait HasFlexibleFieldInLayout
9
{
10
    /**
11
     * If layout contains sub flexible groups, find group by key recursive.
12
     *
13
     * @param string $groupKey
14
     * @return Layout|null
15
     */
16 4
    public function findFlexibleGroupRecursive(string $groupKey): ?Layout
17
    {
18 4
        if ($this->isUseKey($groupKey)) {
0 ignored issues
show
Bug introduced by
It seems like isUseKey() 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

18
        if ($this->/** @scrutinizer ignore-call */ isUseKey($groupKey)) {
Loading history...
19 1
            return $this;
20
        }
21
22 4
        foreach ($this->fieldsCollection() as $field) {
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

22
        foreach ($this->/** @scrutinizer ignore-call */ fieldsCollection() as $field) {
Loading history...
23 4
            if ($field instanceof Flexible) {
24 4
                if ($group = $field->findGroupRecursive($groupKey)) {
25 3
                    return $group;
26
                }
27
            }
28
        }
29
30 2
        return null;
31
    }
32
33
    /**
34
     * TODO: rebuild
35
     */
36 2
    public function findGroupRecursiveAndSetAttribute($groupKey, $fieldKey, $newValue): bool
37
    {
38 2
        $data = $this->getAttributes();
0 ignored issues
show
Bug introduced by
It seems like getAttributes() 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

38
        /** @scrutinizer ignore-call */ 
39
        $data = $this->getAttributes();
Loading history...
39
40 2
        if ($this->isUseKey($groupKey)) {
41 1
            if (array_key_exists($fieldKey, $data)) {
42 1
                $this->setAttribute($fieldKey, $newValue);
0 ignored issues
show
Bug introduced by
The method setAttribute() does not exist on NovaFlexibleContent\Layo...asFlexibleFieldInLayout. Did you maybe mean setAttributeValue()? ( Ignorable by Annotation )

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

42
                $this->/** @scrutinizer ignore-call */ 
43
                       setAttribute($fieldKey, $newValue);

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...
43
44 1
                return true;
45
            }
46
47 1
            return false;
48
        }
49
50 2
        $result = $this->setAttributeValue($data, $groupKey, $fieldKey, $newValue);
51
52 2
        $this->attributes = $data;
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
53
54 2
        return $result;
55
    }
56
57 2
    public function setAttributeValue(array &$array, string $groupKey, string $fieldKey, mixed $newValue): bool
58
    {
59 2
        foreach ($array as $key => $value) {
60 2
            if (is_object($value)
61 2
                && property_exists($value, 'key')
62 2
                && property_exists($value, 'attributes')
63 2
                && $value->key === $groupKey
64 2
                && is_object($value->attributes)) {
65 2
                foreach ($value->attributes as $attribute => $attrValue) {
66 2
                    if ($attribute === $fieldKey) {
67 2
                        $value->attributes->$attribute = $newValue;
68
69 2
                        return true;
70
                    }
71
                }
72
            }
73 2
            if (is_array($value)) {
74 2
                if ($this->setAttributeValue($array[$key], $groupKey, $fieldKey, $newValue)) {
75 2
                    return true;
76
                }
77
            }
78
        }
79
80 1
        return false;
81
    }
82
}
83