Passed
Push — main ( 20efc0...d61e7d )
by Michael
04:14
created

WithLoopFunctions::attributesToProperties()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 17
ccs 13
cts 13
cp 1
rs 9.8333
c 0
b 0
f 0
cc 4
nc 3
nop 2
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\LoopFunctions\Traits;
6
7
use Illuminate\Database\Eloquent\Model;
8
9
trait WithLoopFunctions
10
{
11
    /**
12
     * Maps your model attributes to local class properties.
13
     *
14
     * @param Model|null $model
15
     * @param mixed      $rescue
16
     *
17
     * @return void
18
     */
19 8
    public function attributesToProperties(?Model $model = null, mixed $rescue = null): void
20
    {
21 8
        if (! is_null($model)) {
22 8
            $toIgnore = config('loop-functions.ignore_attributes');
23
24 8
            $ignores = is_array($toIgnore)
25 7
                ? $toIgnore
26 1
                : ['id', 'password'];
27
28 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

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

52
        collect(/** @scrutinizer ignore-type */ $data ?? [])->each(function ($value, $key) use ($rescue) {
Loading history...
53 1
            if (property_exists($this, $key)) {
54 1
                rescue(
55 1
                    fn () => $this->{$key} = $value,
56
                    $rescue,
57 1
                    config('loop-functions.log') ?? false
58
                );
59
            }
60 1
        });
61 1
    }
62
}
63