Issues (2511)

app/Arr.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees;
21
22
use ArrayObject;
23
use Closure;
24
25
use function array_filter;
26
use function array_map;
27
use function array_merge;
28
use function array_unique;
29
use function array_values;
30
use function uasort;
31
32
/**
33
 * Arrays
34
 *
35
 * @template TKey of array-key
36
 * @template TValue
37
 * @extends  ArrayObject<TKey,TValue>
38
 */
39
class Arr extends ArrayObject
40
{
41
    /**
42
     * @param Arr<TKey,TValue> $arr2
43
     *
44
     * @return self<TKey,TValue>
45
     */
46
    public function concat(Arr $arr2): self
47
    {
48
        $arr1 = array_values(array: $this->getArrayCopy());
49
        $arr2 = array_values(array: $arr2->getArrayCopy());
50
51
        return new self(array: $arr1 + $arr2);
52
    }
53
54
    /**
55
     * @param Closure(TValue):bool $closure
56
     *
57
     * @return self<TKey,TValue>
58
     */
59
    public function filter(Closure $closure): self
60
    {
61
        return new self(array: array_filter(array: $this->getArrayCopy(), callback: $closure));
62
    }
63
64
    /**
65
     * @return self<int,TValue>
66
     */
67
    public function flatten(): self
68
    {
69
        return new self(array: array_merge(...$this->getArrayCopy()));
70
    }
71
72
    /**
73
     * @param null|Closure(TValue):bool $closure
74
     *
75
     * @return TValue|null
0 ignored issues
show
The type Fisharebest\Webtrees\TValue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
76
     */
77
    public function first(Closure|null $closure = null): mixed
78
    {
79
        foreach ($this->getArrayCopy() as $value) {
80
            if ($closure === null || $closure($value)) {
81
                return $value;
82
            }
83
        }
84
85
        return null;
86
    }
87
88
    /**
89
     * @param null|Closure(TValue):bool $closure
90
     *
91
     * @return TValue|null
92
     */
93
    public function last(Closure|null $closure = null): mixed
94
    {
95
        return $this->reverse()->first(closure: $closure);
96
    }
97
98
    /**
99
     * @template T
100
     *
101
     * @param Closure(TValue):T $closure
102
     *
103
     * @return self<TKey,T>
104
     */
105
    public function map(Closure $closure): self
106
    {
107
        return new self(array: array_map(callback: $closure, array: $this->getArrayCopy()));
108
    }
109
110
    /**
111
     * @return self<TKey,TValue>
112
     */
113
    public function reverse(): self
114
    {
115
        return new self(array: array_reverse(array: $this->getArrayCopy()));
116
    }
117
118
    /**
119
     * @param Closure(TValue,TValue):int $closure
120
     *
121
     * @return self<TKey,TValue>
122
     */
123
    public function sort(Closure $closure): self
124
    {
125
        $arr = $this->getArrayCopy();
126
        uasort(array: $arr, callback: $closure);
127
128
        return new self(array: $arr);
129
    }
130
131
    /**
132
     * @param Closure(TKey,TKey):int $closure
133
     *
134
     * @return self<TKey,TValue>
135
     */
136
    public function sortKeys(Closure $closure): self
137
    {
138
        $arr = $this->getArrayCopy();
139
        uksort(array: $arr, callback: $closure);
140
141
        return new self(array: $arr);
142
    }
143
144
    /**
145
     * @return self<TKey,TValue>
146
     */
147
    public function unique(): self
148
    {
149
        return new self(array: array_unique(array: $this->getArrayCopy()));
150
    }
151
}
152