Test Failed
Push — main ( b792b4...c898a9 )
by Michael
04:01
created

LoopFunctions::dumpProperties()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 12
nc 2
nop 3
dl 0
loc 25
ccs 0
cts 0
cp 0
crap 6
rs 9.8666
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\LoopFunctions\Traits;
6
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Collection;
9
10
trait LoopFunctions
11
{
12
    use HelpsLoopFunctions;
13
14
    /**
15
     * Choose proper strategy to loop over the data.
16
     *
17
     * @param Model|\ArrayAccess|array|null $data
18
     * @param mixed|null                    $rescue
19
     *
20
     * @return void
21 7
     */
22
    public function propertiesFrom(Model|\ArrayAccess|array|null $data = null, mixed $rescue = null): void
23 7
    {
24
        if ($data) {
25 7
            match (true) {
26 6
                $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
                default                => $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...
28
            };
29
        }
30
    }
31
32
    /**
33
     * Maps your model attributes to local class properties.
34
     *
35
     * @param Model|null $model
36
     * @param mixed      $rescue
37
     *
38
     * @return void
39 9
     */
40
    public function attributesToProperties(?Model $model = null, mixed $rescue = null): void
41 9
    {
42 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

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

63
        collect(/** @scrutinizer ignore-type */ $data ?? [])
Loading history...
64 7
            ->except($this->ignoredPropertyNames())
65 7
            ->each(function ($value, $key) use ($rescue) {
66
                $this->assignValue($key, $value, $rescue);
67 7
68 6
                if ($this->canWalkRecursively($value)) {
69
                    $this->propertiesFrom($value, $rescue);
70
                }
71
            });
72
    }
73
74
    /**
75
     * Dump class properties as key-valued array.
76
     *
77
     * @param string|object|null $class
78
     * @param int|null           $filter
79
     * @param bool               $asCollection
80
     *
81
     * @return array|Collection
82
     * @throws \ReflectionException
83
     */
84
    public function dumpProperties(
85
        string|object|null $class = null,
86
        ?int $filter = null,
87
        bool $asCollection = false
88
    ): array|Collection {
89
        $class = match (true) {
90
            is_string($class) => app($class),
0 ignored issues
show
Bug introduced by
It seems like $class can also be of type object; however, parameter $abstract of app() does only seem to accept null|string, 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

90
            is_string($class) => app(/** @scrutinizer ignore-type */ $class),
Loading history...
91
            is_object($class) => $class,
92
            default => $this,
93
        };
94
95
        $properties = (new \ReflectionClass($class))
96
            ->getProperties($filter);
97
98
        $collection = collect($properties)->mapWithKeys(
0 ignored issues
show
Bug introduced by
$properties of type ReflectionProperty[] 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

98
        $collection = collect(/** @scrutinizer ignore-type */ $properties)->mapWithKeys(
Loading history...
99
            fn (\ReflectionProperty $property) => [
100
                $property->getName() => $property->getValue($class),
0 ignored issues
show
Bug introduced by
It seems like $class can also be of type string; however, parameter $object of ReflectionProperty::getValue() does only seem to accept null|object, 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

100
                $property->getName() => $property->getValue(/** @scrutinizer ignore-type */ $class),
Loading history...
101
            ]
102
        );
103
104
        if ($asCollection) {
105
            return $collection;
106
        }
107
108
        return $collection->all();
109
    }
110
}
111