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

eliashaeussler / cache-warmup / 8473066280

28 Mar 2024 08:06PM UTC coverage: 93.917% (-3.8%) from 97.674%
8473066280

push

github

eliashaeussler
[TASK] Increase verbosity to display parsed sitemaps

1 of 1 new or added line in 1 file covered. (100.0%)

2 existing lines in 1 file now uncovered.

1405 of 1496 relevant lines covered (93.92%)

9.15 hits per line

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

93.75
/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 explode;
29
use function is_array;
30
use function is_int;
31

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

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

54
            $reference = &$reference[$pathSegment];
2✔
55
        }
56

57
        return $reference;
2✔
58
    }
59

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

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

74
            $reference = &$reference[$pathSegment];
1✔
75
        }
76

77
        $reference = $value;
1✔
78
    }
79

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

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

99
            $originalValue = &$subject[$key];
1✔
100

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

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

114
    private static function pathSegmentExists(mixed $subject, string $pathSegment): bool
2✔
115
    {
116
        return is_array($subject) && array_key_exists($pathSegment, $subject);
2✔
117
    }
118
}
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