Passed
Push — main ( f599e4...0342d9 )
by Michael
07:47 queued 04:53
created

LoopFunctions::propertiesFrom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
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
     * Choose proper strategy to loop over the data.
15
     *
16
     * @param Model|array|null $data
17
     * @param mixed|null       $rescue
18
     *
19
     * @return void
20
     */
21 2
    public function propertiesFrom(Model|array|null $data = null, mixed $rescue = null): void
22
    {
23
        match (true) {
24 2
            is_array($data)        => $this->arrayToProperties($data, $rescue),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->arrayToProperties($data, $rescue) targeting MichaelRubel\LoopFunctio...ns::arrayToProperties() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
25 1
            $data instanceof Model => $this->attributesToProperties($data, $rescue),
0 ignored issues
show
Bug introduced by
It seems like $data can also be of type array; however, parameter $model of MichaelRubel\LoopFunctio...ttributesToProperties() does only seem to accept Illuminate\Database\Eloquent\Model|null, 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

25
            $data instanceof Model => $this->attributesToProperties(/** @scrutinizer ignore-type */ $data, $rescue),
Loading history...
Bug introduced by
Are you sure the usage of $this->attributesToProperties($data, $rescue) targeting MichaelRubel\LoopFunctio...ttributesToProperties() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
26
        };
27
    }
28
29
    /**
30
     * Maps your model attributes to local class properties.
31
     *
32
     * @param Model|null $model
33
     * @param mixed      $rescue
34
     *
35
     * @return void
36
     */
37 9
    public function attributesToProperties(?Model $model = null, mixed $rescue = null): void
38
    {
39 9
        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

39
        collect(/** @scrutinizer ignore-type */ $model?->getAttributes())
Loading history...
40 9
            ->except($this->ignoreKeys())
41 9
            ->each(
42 9
                fn ($value, $property) => $this->assignValue(
43
                    $property,
44 9
                    $model->{$property},
45
                    $rescue
46
                )
47
            );
48
    }
49
50
    /**
51
     * Map array data to class properties.
52
     *
53
     * @param array|null $data
54
     * @param mixed|null $rescue
55
     *
56
     * @return void
57
     */
58 6
    public function arrayToProperties(?array $data, mixed $rescue = null): void
59
    {
60 6
        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

60
        collect(/** @scrutinizer ignore-type */ $data ?? [])
Loading history...
61 6
            ->except($this->ignoreKeys())
62 6
            ->each(function ($value, $key) use ($rescue) {
63 6
                if ($this->canWalkRecursively($value, $key)) {
64
                    $this->arrayToProperties($value, $rescue);
65
                }
66
67 6
                $this->assignValue($key, $value, $rescue);
68
            });
69
    }
70
}
71