Passed
Push — main ( a3e25e...75016e )
by Michael
04:15
created

LoopFunctions::__set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
     */
22 9
    public function propertiesFrom(Model|\ArrayAccess|array|null $data = null, mixed $rescue = null): void
23
    {
24 9
        if ($data) {
25
            match (true) {
26 9
                $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 7
                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
     */
40 10
    public function attributesToProperties(?Model $model = null, mixed $rescue = null): void
41
    {
42 10
        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 10
            ->except($this->ignoredPropertyNames())
44 10
            ->each(
45 10
                fn ($value, $property) => $this->assignValue(
46
                    $property,
47 10
                    $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
     */
61 8
    public function arrayToProperties(array|\ArrayAccess|null $data, mixed $rescue = null): void
62
    {
63 8
        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 8
            ->except($this->ignoredPropertyNames())
65 8
            ->each(function ($value, $key) use ($rescue) {
66 8
                $this->assignValue($key, $value, $rescue);
67
68 8
                if ($this->canWalkRecursively($value)) {
69 6
                    $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 3
    public function dumpProperties(
85
        string|object|null $class = null,
86
        bool $asCollection = false,
87
        ?int $filter = null,
88
    ): array|Collection {
89 3
        $class = match (true) {
90 3
            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 1
            is_object($class) => $class,
92 1
            default => $this,
93
        };
94
95 3
        $properties = (new \ReflectionClass($class))
96 3
            ->getProperties($filter);
97
98 3
        $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 3
            function (\ReflectionProperty $property) use ($class) {
100 3
                $property->setAccessible(true);
101
102 3
                return [$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

102
                return [$property->getName() => $property->getValue(/** @scrutinizer ignore-type */ $class)];
Loading history...
103
            }
104
        );
105
106 3
        return ! $asCollection
107 2
            ? $collection->all()
108 3
            : $collection;
109
    }
110
111
    /**
112
     * Get the instance's property.
113
     *
114
     * @param string $name
115
     *
116
     * @return mixed
117
     */
118
    public function __get(string $name): mixed
119
    {
120
        return $this->{$name};
121
    }
122
123
    /**
124
     * Set the instance's property.
125
     *
126
     * @param string $name
127
     * @param mixed  $value
128
     */
129 8
    public function __set(string $name, mixed $value): void
130
    {
131 8
        $this->{$name} = $value;
132
    }
133
}
134