Passed
Push — main ( db7939...13b0ea )
by Michael
04:38
created

HelpsLoopFunctions::hasDefaultValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\LoopFunctions\Traits;
6
7
trait HelpsLoopFunctions
8
{
9
    /**
10
     * Assign the value to the property or rescue.
11
     *
12
     * @param int|string $key
13
     * @param mixed      $value
14
     * @param mixed|null $rescue
15
     *
16
     * @return void
17
     * @throws \ReflectionException
18
     */
19 16
    private function assignValue(int|string $key, mixed $value, mixed $rescue = null): void
20
    {
21 16
        if ($this->canAssignValue($key)) {
22 16
            rescue(
23 16
                fn () => $this->{$key} = $value,
24
                $rescue,
25 16
                config('loop-functions.log') ?? false
26
            );
27
        }
28
    }
29
30
    /**
31
     * @return array
32
     */
33 16
    private function ignoredPropertyNames(): array
34
    {
35 16
        return config('loop-functions.ignore_keys', ['id', 'password']);
36
    }
37
38
    /**
39
     * @param int|string $key
40
     *
41
     * @return bool
42
     * @throws \ReflectionException
43
     */
44 16
    private function canAssignValue(int|string $key): bool
45
    {
46 16
        return is_string($key)
47 16
            && property_exists($this, $key)
48 16
            && (empty($this->{$key}) || $this->hasDefaultValue($key));
49
    }
50
51
    /**
52
     * @param int|string $key
53
     *
54
     * @return bool
55
     * @throws \ReflectionException
56
     */
57 3
    private function hasDefaultValue(int|string $key): bool
58
    {
59 3
        $default = (new \ReflectionProperty($this, $key))
60 3
            ->getDefaultValue();
61
62 3
        return ! empty($default);
63
    }
64
65
    /**
66
     * @param mixed      $value
67
     *
68
     * @return bool
69
     */
70 7
    private function canWalkRecursively(mixed $value): bool
71
    {
72 7
        return is_array($value) || $value instanceof \ArrayAccess;
73
    }
74
}
75