• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

heimrichhannot / contao-utils-bundle / 15017483614

14 May 2025 09:41AM UTC coverage: 25.302% (-54.2%) from 79.482%
15017483614

Pull #96

github

koertho
backport anonymize utils
Pull Request #96: Backport anonymize utils

6 of 7 new or added lines in 2 files covered. (85.71%)

248 existing lines in 22 files now uncovered.

1488 of 5881 relevant lines covered (25.3%)

1.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/StaticUtil/StaticArrayUtil.php
1
<?php
2

3
namespace HeimrichHannot\UtilsBundle\StaticUtil;
4

5
class StaticArrayUtil extends AbstractStaticUtil
6
{
7
    /**
8
     * Insert a new entry before a specific or multiple keys in array.
9
     * If the keys not exist, the new entry is added to the end of the array.
10
     * Array is passed as reference.
11
     *
12
     * @param array        $array    Array the new entry should inserted to
13
     * @param array|string $keys     The key or keys where the new entry should be added before
14
     * @param string       $newKey   The key of the entry that should be added
15
     * @param mixed        $newValue The value of the entry that should be added
16
     */
17
    public static function insertBeforeKey(array &$array, $keys, string $newKey, $newValue): void
18
    {
UNCOV
19
        if (is_string($keys)) {
×
UNCOV
20
            $keys = [$keys];
×
21
        }
22

UNCOV
23
        if (!is_array($keys)) {
×
UNCOV
24
            throw new \InvalidArgumentException('Parameter $keys must be of type array or string.');
×
25
        }
26

UNCOV
27
        if (array_intersect($keys, array_keys($array))) {
×
UNCOV
28
            $new = [];
×
29

UNCOV
30
            foreach ($array as $k => $value) {
×
UNCOV
31
                if (\in_array($k, $keys)) {
×
UNCOV
32
                    $new[$newKey] = $newValue;
×
33
                }
UNCOV
34
                $new[$k] = $value;
×
35
            }
UNCOV
36
            $array = $new;
×
37
        } else {
UNCOV
38
            $array[$newKey] = $newValue;
×
39
        }
40
    }
41

42
    /**
43
     * Insert a value into an existing array by key name.
44
     *
45
     * Additional options:
46
     * - (bool) strict: Strict behavior for array search. Default false
47
     * - (bool) attachMissingKey: Attach value to the end of the array if the key does not exist. Default: true
48
     * - (int) offset: Add additional offset.
49
     *
50
     * @param array  $array   The target array
51
     * @param string $key     the existing target key in the array
52
     * @param mixed  $value   the new value to be inserted
53
     * @param array{
54
     *     strict?: bool,
55
     *     attachMissingKey?: bool,
56
     *     offset?: int
57
     * } $options Additional options
58
     */
59
    public static function insertAfterKey(array &$array, string $key, $value, string $newKey = null, array $options = []): void
60
    {
UNCOV
61
        $options = array_merge([
×
UNCOV
62
            'strict' => false,
×
UNCOV
63
            'attachMissingKey' => true,
×
UNCOV
64
            'offset' => 0,
×
UNCOV
65
        ], $options);
×
66

UNCOV
67
        $keys = array_keys($array);
×
UNCOV
68
        $index = array_search($key, $keys, $options['strict']);
×
69

UNCOV
70
        if (false === $index && false === $options['attachMissingKey']) {
×
UNCOV
71
            return;
×
72
        }
UNCOV
73
        $pos = false === $index ? count($array) : $index + 1;
×
UNCOV
74
        $pos = $pos + $options['offset'];
×
75

UNCOV
76
        if ($newKey) {
×
UNCOV
77
            $value = [$newKey => $value];
×
78
        } else {
UNCOV
79
            $value = [$value];
×
80
        }
81

UNCOV
82
        $array = array_combine(
×
UNCOV
83
            array_merge(array_slice($keys, 0, $pos), array_keys($value), array_slice($keys, $pos)),
×
UNCOV
84
            array_merge(array_slice($array, 0, $pos), $value, array_slice($array, $pos))
×
UNCOV
85
        );
×
86
    }
87

88
    /**
89
     * Removes a value from an array.
90
     *
91
     * @return bool Returns true if the value has been found and removed, false in other cases
92
     */
93
    public static function removeValue($value, array &$array): bool
94
    {
UNCOV
95
        $position = array_search($value, $array);
×
96

UNCOV
97
        if (false !== $position) {
×
UNCOV
98
            unset($array[$position]);
×
UNCOV
99
            return true;
×
100
        }
101

UNCOV
102
        return false;
×
103
    }
104
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc