Passed
Push — main ( 177e33...6b50d8 )
by Michael
03:20
created

HelpsLoopFunctions   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 56
ccs 16
cts 16
cp 1
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A canWalkRecursively() 0 3 1
A canAssignValue() 0 3 2
A assignValue() 0 7 3
A ignoreKeys() 0 9 2
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
     */
18 5
    private function assignValue(int|string $key, mixed $value, mixed $rescue = null): void
19
    {
20 5
        if ($this->canAssignValue($key)) {
21 5
            rescue(
22 5
                fn () => ! empty($this->{$key}) ?: $this->{$key} = $value,
23
                $rescue,
24 5
                config('loop-functions.log') ?? false
25
            );
26
        }
27 5
    }
28
29
    /**
30
     * @return array
31
     */
32 13
    private function ignoreKeys(): array
33
    {
34 13
        $defaults = ['id', 'password'];
35
36 13
        $ignores = config('loop-functions.ignore_keys', $defaults);
37
38 13
        return is_array($ignores)
39 13
            ? $ignores
40 13
            : $defaults;
41
    }
42
43
    /**
44
     * Check if the function can walk recursively over an array.
45
     *
46
     * @param mixed $value
47
     *
48
     * @return bool
49
     */
50 5
    private function canWalkRecursively(mixed $value): bool
51
    {
52 5
        return is_array($value);
53
    }
54
55
    /**
56
     * @param int|string $key
57
     *
58
     * @return bool
59
     */
60 5
    private function canAssignValue(int|string $key): bool
61
    {
62 5
        return is_string($key) && property_exists($this, $key);
63
    }
64
}
65