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
$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
![]() |
|||||
26 | 9 | default => $this->arrayToProperties($data, $rescue), |
|||
0 ignored issues
–
show
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 The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||||
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
$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
![]() |
|||||
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
$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
![]() |
|||||
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
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
![]() |
|||||
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
$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
![]() |
|||||
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
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
![]() |
|||||
100 | 3 | } |
|||
101 | 3 | ); |
|||
102 | |||||
103 | 3 | return ! $asCollection |
|||
104 | 2 | ? $collection->all() |
|||
105 | 3 | : $collection; |
|||
106 | } |
||||
107 | } |
||||
108 |
This check looks for function or method calls that always return null and whose return value is used.
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.