LoopFunctions   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 11
Bugs 1 Features 1
Metric Value
wmc 7
eloc 32
c 11
b 1
f 1
dl 0
loc 96
ccs 41
cts 41
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A propertiesFrom() 0 6 2
A arrayToProperties() 0 9 2
A attributesToProperties() 0 9 1
A dumpProperties() 0 25 2
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
     * @return void
20
     */
21 9
    public function propertiesFrom(Model|\ArrayAccess|array|null $data = null, mixed $rescue = null): void
22
    {
23 9
        if ($data) {
24 9
            match (true) {
25 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

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

40
        collect(/** @scrutinizer ignore-type */ $model?->getAttributes())
Loading history...
41 9
            ->except($this->ignoredPropertyNames())
42 9
            ->each(
43 9
                fn ($value, $property) => $this->assignValue(
44 9
                    $property,
45 9
                    $model->{$property},
46 9
                    $rescue
47 9
                )
48 9
            );
49
    }
50
51
    /**
52
     * Map array data to class properties.
53
     *
54
     * @param  array|\ArrayAccess|null  $data
55
     * @param  mixed|null  $rescue
56
     * @return void
57
     */
58 9
    public function arrayToProperties(array|\ArrayAccess|null $data, mixed $rescue = null): void
59
    {
60 9
        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 9
            ->except($this->ignoredPropertyNames())
62 9
            ->each(function ($value, $key) use ($rescue) {
63 9
                $this->assignValue($key, $value, $rescue);
64
65 9
                if ($this->canWalkRecursively($value)) {
66 6
                    $this->propertiesFrom($value, $rescue);
67
                }
68 9
            });
69
    }
70
71
    /**
72
     * Dump class properties as key-valued array.
73
     *
74
     * @param  string|object|null  $class
75
     * @param  int|null  $filter
76
     * @param  bool  $asCollection
77
     * @return array|Collection
78
     *
79
     * @throws \ReflectionException
80
     */
81 3
    public function dumpProperties(
82
        string|object|null $class = null,
83
        bool $asCollection = false,
84
        ?int $filter = null,
85
    ): array|Collection {
86 3
        $class = match (true) {
87 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

87
            is_string($class) => app(/** @scrutinizer ignore-type */ $class),
Loading history...
88 3
            is_object($class) => $class,
89 3
            default => $this,
90 3
        };
91
92 3
        $properties = (new \ReflectionClass($class))
93 3
            ->getProperties($filter);
94
95 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

95
        $collection = collect(/** @scrutinizer ignore-type */ $properties)->mapWithKeys(
Loading history...
96 3
            function (\ReflectionProperty $property) use ($class) {
97 3
                $property->setAccessible(true);
98
99 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

99
                return [$property->getName() => $property->getValue(/** @scrutinizer ignore-type */ $class)];
Loading history...
100 3
            }
101 3
        );
102
103 3
        return ! $asCollection
104 2
            ? $collection->all()
105 3
            : $collection;
106
    }
107
}
108