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

eliashaeussler / cache-warmup / 12330828268

14 Dec 2024 02:58PM UTC coverage: 90.251% (-0.06%) from 90.313%
12330828268

push

github

web-flow
Merge pull request #415 from eliashaeussler/renovate/major-phpstan-packages

[TASK] Update PHPStan packages to v2 (major)

3 of 4 new or added lines in 1 file covered. (75.0%)

1472 of 1631 relevant lines covered (90.25%)

8.99 hits per line

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

92.86
/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 (!is_array($reference) || !array_key_exists($pathSegment, $reference)) {
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 (!is_array($reference)) {
1✔
NEW
73
                return;
×
74
            }
75

76
            if (!array_key_exists($pathSegment, $reference)) {
1✔
77
                $reference[$pathSegment] = [];
1✔
78
            }
79

80
            $reference = &$reference[$pathSegment];
1✔
81
        }
82

83
        $reference = $value;
1✔
84
    }
85

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

99
            // Append value if key is numeric
100
            if (is_int($key)) {
1✔
101
                $subject[] = $value;
1✔
102
                continue;
1✔
103
            }
104

105
            $originalValue = &$subject[$key];
1✔
106

107
            // Overwrite value in subject
108
            if (!is_array($value)) {
1✔
109
                $originalValue = $value;
×
110
                continue;
×
111
            }
112

113
            // Merge arrays
114
            if (is_array($originalValue)) {
1✔
115
                self::mergeRecursive($originalValue, $value);
1✔
116
            }
117
        }
118
    }
119

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