Passed
Branch main (db7939)
by Michael
04:16 queued 10s
created

LoopFunctions::arrayToProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 3
c 1
b 1
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
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 2
        if ($data) {
24
            match (true) {
25 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...
26 1
                $data instanceof Model => $this->attributesToProperties($data, $rescue),
0 ignored issues
show
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...
Bug introduced by
$data of type array is incompatible with the type Illuminate\Database\Eloquent\Model|null expected by parameter $model of MichaelRubel\LoopFunctio...ttributesToProperties(). ( Ignorable by Annotation )

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

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

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

62
        collect(/** @scrutinizer ignore-type */ $data ?? [])
Loading history...
63 6
            ->except($this->ignoredPropertyNames())
64 6
            ->each(fn ($value, $key) => $this->assignValue($key, $value, $rescue));
65
    }
66
}
67