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

eliashaeussler / cache-warmup / 10495565513

21 Aug 2024 06:38PM UTC coverage: 94.463%. Remained the same
10495565513

Pull #389

github

web-flow
[TASK] Update paambaati/codeclimate-action action to v9

| datasource  | package                      | from   | to     |
| ----------- | ---------------------------- | ------ | ------ |
| github-tags | paambaati/codeclimate-action | v8.0.0 | v9.0.0 |
Pull Request #389: [TASK] Update paambaati/codeclimate-action action to v9

1433 of 1517 relevant lines covered (94.46%)

9.11 hits per line

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

95.24
/src/Helper/ArrayHelper.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Composer package "eliashaeussler/cache-warmup".
7
 *
8
 * Copyright (C) 2020-2024 Elias Häußler <elias@haeussler.dev>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace EliasHaeussler\CacheWarmup\Helper;
25

26
use function array_filter;
27
use function array_key_exists;
28
use function array_map;
29
use function array_values;
30
use function explode;
31
use function is_array;
32
use function is_int;
33

34
/**
35
 * ArrayHelper.
36
 *
37
 * @author Elias Häußler <elias@haeussler.dev>
38
 * @license GPL-3.0-or-later
39
 */
40
final class ArrayHelper
41
{
42
    /**
43
     * @param iterable<string, mixed> $subject
44
     * @param non-empty-string        $delimiter
45
     */
46
    public static function getValueByPath(iterable $subject, string $path, string $delimiter = '/'): mixed
2✔
47
    {
48
        $pathSegments = self::trimExplode($path, $delimiter);
2✔
49
        $reference = &$subject;
2✔
50

51
        foreach ($pathSegments as $pathSegment) {
2✔
52
            if (!self::pathSegmentExists($reference, $pathSegment)) {
2✔
53
                return null;
2✔
54
            }
55

56
            $reference = &$reference[$pathSegment];
2✔
57
        }
58

59
        return $reference;
2✔
60
    }
61

62
    /**
63
     * @param iterable<string, mixed> $subject
64
     * @param non-empty-string        $delimiter
65
     */
66
    public static function setValueByPath(iterable &$subject, string $path, mixed $value, string $delimiter = '/'): void
1✔
67
    {
68
        $pathSegments = self::trimExplode($path, $delimiter);
1✔
69
        $reference = &$subject;
1✔
70

71
        foreach ($pathSegments as $pathSegment) {
1✔
72
            if (!self::pathSegmentExists($reference, $pathSegment)) {
1✔
73
                $reference[$pathSegment] = [];
1✔
74
            }
75

76
            $reference = &$reference[$pathSegment];
1✔
77
        }
78

79
        $reference = $value;
1✔
80
    }
81

82
    /**
83
     * @param array<array-key, mixed> $subject
84
     * @param array<array-key, mixed> $other
85
     */
86
    public static function mergeRecursive(array &$subject, array $other): void
1✔
87
    {
88
        foreach ($other as $key => $value) {
1✔
89
            // Skip merge if key does not exist in subject
90
            if (!array_key_exists($key, $subject)) {
1✔
91
                $subject[$key] = $value;
1✔
92
                continue;
1✔
93
            }
94

95
            // Append value if key is numeric
96
            if (is_int($key)) {
1✔
97
                $subject[] = $value;
1✔
98
                continue;
1✔
99
            }
100

101
            $originalValue = &$subject[$key];
1✔
102

103
            // Overwrite value in subject
104
            if (!is_array($value)) {
1✔
105
                $originalValue = $value;
×
106
                continue;
×
107
            }
108

109
            // Merge arrays
110
            if (is_array($originalValue)) {
1✔
111
                self::mergeRecursive($originalValue, $value);
1✔
112
            }
113
        }
114
    }
115

116
    /**
117
     * @param non-empty-string $delimiter
118
     *
119
     * @return list<non-empty-string>
120
     */
121
    public static function trimExplode(string $subject, string $delimiter = ','): array
3✔
122
    {
123
        return array_values(
3✔
124
            array_filter(
3✔
125
                array_map(
3✔
126
                    trim(...),
3✔
127
                    explode($delimiter, $subject),
3✔
128
                ),
3✔
129
                static fn (string $value) => '' !== $value,
3✔
130
            ),
3✔
131
        );
3✔
132
    }
133

134
    private static function pathSegmentExists(mixed $subject, string $pathSegment): bool
2✔
135
    {
136
        return is_array($subject) && array_key_exists($pathSegment, $subject);
2✔
137
    }
138
}
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

© 2025 Coveralls, Inc