Passed
Push — main ( c8490e...f599e4 )
by Michael
11:55
created

LoopFunctions::arrayToProperties()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 6
c 1
b 1
f 0
nc 1
nop 2
dl 0
loc 10
ccs 6
cts 7
cp 0.8571
crap 2.0116
rs 10
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
        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

23
        collect(/** @scrutinizer ignore-type */ $model?->getAttributes())
Loading history...
24 8
            ->except($this->ignoreKeys())
25 8
            ->each(
26 8
                fn ($value, $property) => $this->assignValue(
27
                    $property,
28 8
                    $model->{$property},
29
                    $rescue
30
                )
31
            );
32
    }
33
34
    /**
35
     * Map array data to class properties.
36
     *
37
     * @param array|null $data
38
     * @param mixed|null $rescue
39
     *
40
     * @return void
41
     */
42 5
    public function arrayToProperties(?array $data, mixed $rescue = null): void
43
    {
44 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

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