Remove::removeChild()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 8
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 12
ccs 9
cts 9
cp 1
crap 6
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jasny\DotKey\Internal;
6
7
use Jasny\DotKey\ResolveException;
8
9
/**
10
 * Static class to remove items from subject with path.
11
 * @internal
12
 */
13
final class Remove
14
{
15
    /**
16
     * Get a particular value back from the config array
17
     *
18
     * @param object|array<string,mixed> $subject
19
     * @param string                     $path
20
     * @param string                     $delimiter
21
     * @param bool                       $copy
22
     */
23 30
    public static function apply(&$subject, string $path, string $delimiter, bool $copy): void
24
    {
25 30
        $current =& $subject;
26 30
        $index = Helpers::splitPath($path, $delimiter);
27
28 29
        while (\count($index) > 1) {
29 28
            $key = \array_shift($index);
30
31
            try {
32 28
                $current =& Helpers::descend($current, $key, $exists, false, $copy);
33 1
            } catch (\Error $error) {
34 1
                $msg = "Unable to remove '$path': error at '%s'";
35 1
                throw ResolveException::create($msg, $path, $delimiter, $index, $error);
36
            }
37
38 27
            if (!$exists) {
39 4
                return;
40
            }
41
42 27
            if (!\is_array($current) && !\is_object($current)) {
43 5
                $msg = "Unable to remove '$path': '%s' is of type " . \gettype($current);
44 5
                throw ResolveException::create($msg, $path, $delimiter, $index);
45
            }
46
        }
47
48
        try {
49 19
            if ($copy && is_object($current)) {
50 2
                $current = clone $current;
51
            }
52
53 19
            self::removeChild($current, $index[0]);
54 2
        } catch (\Error $error) {
55 2
            $msg = "Unable to remove '$path': error at '%s'";
56 2
            throw ResolveException::create($msg, $path, $delimiter, array_slice($index, 0, -1), $error);
57
        }
58
    }
59
60
    /**
61
     * Remove item or property from subject.
62
     *
63
     * @param object|array<string,mixed> $subject
64
     * @param string                     $key
65
     */
66 19
    protected static function removeChild(&$subject, string $key): void
67
    {
68 19
        if (\is_array($subject)) {
69 10
            if (\array_key_exists($key, $subject)) {
70 10
                unset($subject[$key]);
71
            }
72 9
        } elseif ($subject instanceof \ArrayAccess) {
73 3
            if ($subject->offsetExists($key)) {
74 3
                unset($subject[$key]);
75
            }
76 6
        } elseif (\property_exists($subject, $key)) {
77 6
            unset($subject->{$key});
78
        }
79
    }
80
}
81