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

HelpsLoopFunctions::ignoreKeys()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
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
     */
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