Passed
Push — main ( 177e33...6b50d8 )
by Michael
03:20
created

LoopFunctions::ignoreKeys()   A

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 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\LoopFunctions\Traits;
6
7
use Illuminate\Database\Eloquent\Model;
8
9
trait LoopFunctions
10
{
11
    use HelpsLoopFunctions;
12
13
    /**
14
     * Maps your model attributes to local class properties.
15
     *
16
     * @param Model|null $model
17
     * @param mixed      $rescue
18
     *
19
     * @return void
20
     */
21 8
    public function attributesToProperties(?Model $model = null, mixed $rescue = null): void
22
    {
23 8
        if (! is_null($model)) {
24 8
            collect($model->getAttributes())
0 ignored issues
show
Bug introduced by
$model->getAttributes() 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

24
            collect(/** @scrutinizer ignore-type */ $model->getAttributes())
Loading history...
25 8
                ->except($this->ignoreKeys())
26 8
                ->each(function ($value, $property) use ($model, $rescue) {
27 8
                    if (property_exists($this, $property)) {
28 8
                        rescue(
29 8
                            fn () => $this->{$property} = $model->{$property},
30
                            $rescue,
31 8
                            config('loop-functions.log') ?? false
32
                        );
33
                    }
34 8
                });
35
        }
36 8
    }
37
38
    /**
39
     * Map array data to class properties.
40
     *
41
     * @param array|null $data
42
     * @param mixed|null $rescue
43
     *
44
     * @return void
45
     */
46 5
    public function arrayToProperties(?array $data, mixed $rescue = null): void
47
    {
48 5
        collect($data ?? [])
0 ignored issues
show
Bug introduced by
$data ?? array() 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

48
        collect(/** @scrutinizer ignore-type */ $data ?? [])
Loading history...
49 5
            ->except($this->ignoreKeys())
50 5
            ->each(function ($value, $key) use ($rescue) {
51 5
                if ($this->canWalkRecursively($value)) {
52 3
                    $this->arrayToProperties($value, $rescue);
53
                }
54
55 5
                $this->assignValue($key, $value, $rescue);
56 5
            });
57 5
    }
58
}
59