This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Pitchart\Collection; |
||
4 | |||
5 | use Pitchart\Collection\Mixin\CallableUnifierTrait; |
||
6 | |||
7 | /** |
||
8 | * @author Julien VITTE <[email protected]> |
||
9 | */ |
||
10 | class Collection extends \ArrayObject implements CollectionInterface, Checkable |
||
11 | { |
||
12 | |||
13 | use CallableUnifierTrait; |
||
14 | |||
15 | /** |
||
16 | * @param iterable $iterable |
||
17 | * @return static |
||
18 | */ |
||
19 | 69 | View Code Duplication | public static function from($iterable) |
20 | { |
||
21 | 69 | if ($iterable instanceof \Iterator) { |
|
22 | 11 | return new static(iterator_to_array($iterable)); |
|
23 | } |
||
24 | 60 | if (is_array($iterable) |
|
25 | || $iterable instanceof \IteratorAggregate |
||
26 | 60 | ) { |
|
27 | 55 | return new static($iterable); |
|
28 | } |
||
29 | |||
30 | 5 | throw new \InvalidArgumentException( |
|
31 | 5 | sprintf( |
|
32 | 5 | 'Argument 1 must be an instance of Traversable or an array, %s given', |
|
33 | 5 | is_object($iterable) ? get_class($iterable) : gettype($iterable) |
|
34 | 5 | ) |
|
35 | 5 | ); |
|
36 | } |
||
37 | |||
38 | /** |
||
39 | * Alias for getArrayCopy() |
||
40 | * |
||
41 | * @return array |
||
42 | * @see getArrayCopy |
||
43 | */ |
||
44 | 24 | public function toArray() |
|
45 | { |
||
46 | 24 | return $this->getArrayCopy(); |
|
47 | } |
||
48 | |||
49 | /** |
||
50 | * Return reindexed items |
||
51 | * |
||
52 | * @return array |
||
53 | */ |
||
54 | 44 | public function values() |
|
55 | { |
||
56 | 44 | return array_values($this->getArrayCopy()); |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * Execute a callback function on each item |
||
61 | * |
||
62 | * @param callable $callable |
||
63 | */ |
||
64 | 2 | public function each(callable $callable) |
|
65 | { |
||
66 | 2 | $function = $this->normalizeAsCallables($callable); |
|
67 | 2 | foreach ($this->getArrayCopy() as $item) { |
|
68 | 2 | $function($item); |
|
69 | 2 | } |
|
70 | 2 | } |
|
71 | |||
72 | /** |
||
73 | * Map a function over a collection |
||
74 | * |
||
75 | * @param callable $callable |
||
76 | * @return static |
||
77 | */ |
||
78 | 14 | public function map(callable $callable) |
|
79 | { |
||
80 | 14 | return new static(array_map($this->normalizeAsCallables($callable), $this->getArrayCopy())); |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param callable $callable |
||
85 | * @return static |
||
86 | */ |
||
87 | 11 | public function filter(callable $callable) |
|
88 | { |
||
89 | 11 | return new static(array_filter($this->getArrayCopy(), $this->normalizeAsCallables($callable))); |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * Alias for filter() |
||
94 | * |
||
95 | * @see filter() |
||
96 | * |
||
97 | * @param callable $callable |
||
98 | * @return static |
||
99 | */ |
||
100 | 3 | public function select(callable $callable) |
|
101 | { |
||
102 | 3 | return self::filter($callable); |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param callable $callable |
||
107 | * @return static |
||
108 | */ |
||
109 | 9 | public function reject(callable $callable) |
|
110 | { |
||
111 | 9 | $function = $this->normalizeAsCallables($callable); |
|
112 | 9 | return new static(array_filter( |
|
113 | 9 | $this->getArrayCopy(), |
|
114 | function ($item) use ($function) { |
||
115 | 9 | return !$function($item); |
|
116 | } |
||
117 | 9 | )); |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * Remove duplicate elements |
||
122 | * |
||
123 | * @return static |
||
124 | */ |
||
125 | 1 | public function distinct() |
|
126 | { |
||
127 | 1 | return new static(array_unique($this->values())); |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * |
||
132 | * @param callable $callable |
||
133 | * @return static |
||
134 | */ |
||
135 | 2 | public function sort(callable $callable) |
|
136 | { |
||
137 | 2 | $sorted = $this->values(); |
|
138 | 2 | usort($sorted, $this->normalizeAsCallables($callable)); |
|
139 | 2 | return new static($sorted); |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param int $offset |
||
144 | * @param int $length |
||
145 | * @param bool $preserveKeys |
||
146 | * @return static |
||
147 | */ |
||
148 | 6 | public function slice($offset, $length = null, $preserveKeys = false) |
|
149 | { |
||
150 | 6 | return new static(array_slice($this->getArrayCopy(), $offset, $length, $preserveKeys)); |
|
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param int $length |
||
155 | * @param bool $preserveKeys |
||
156 | * @return static |
||
157 | */ |
||
158 | 2 | public function take($length, $preserveKeys = false) |
|
159 | { |
||
160 | 2 | return $this->slice(0, $length, $preserveKeys); |
|
161 | } |
||
162 | |||
163 | /** |
||
164 | * @param Collection $collection |
||
165 | * @return static |
||
166 | */ |
||
167 | 2 | public function difference(self $collection) |
|
168 | { |
||
169 | 2 | return new static(array_values(array_diff($this->values(), $collection->values()))); |
|
170 | } |
||
171 | |||
172 | /** |
||
173 | * @param Collection $collection |
||
174 | * @return static |
||
175 | */ |
||
176 | 2 | public function intersection(self $collection) |
|
177 | { |
||
178 | 2 | return new static(array_values(array_intersect($this->values(), $collection->values()))); |
|
179 | } |
||
180 | |||
181 | /** |
||
182 | * @param Collection ...$collections |
||
183 | * @return static |
||
184 | */ |
||
185 | 9 | public function merge(...$collections) |
|
186 | { |
||
187 | $values = array_map(function (CollectionInterface $collection) { |
||
188 | 9 | return $collection->values(); |
|
189 | 9 | }, $collections); |
|
190 | 9 | return new static(array_merge($this->values(), ...$values)); |
|
191 | } |
||
192 | |||
193 | /** |
||
194 | * Group a collection using a \Closure |
||
195 | */ |
||
196 | 1 | public function groupBy(callable $groupBy, $preserveKeys = false) |
|
197 | { |
||
198 | 1 | $function = $this->normalizeAsCallables($groupBy); |
|
199 | 1 | $results = []; |
|
200 | 1 | foreach ($this->values() as $key => $value) { |
|
201 | 1 | $groupKeys = $function($value, $key); |
|
202 | 1 | if (! is_array($groupKeys)) { |
|
203 | 1 | $groupKeys = [$groupKeys]; |
|
204 | 1 | } |
|
205 | 1 | foreach ($groupKeys as $groupKey) { |
|
206 | 1 | if (!in_array(gettype($groupKey), ['string', 'int'])) { |
|
207 | 1 | $groupKey = (int) $groupKey; |
|
208 | 1 | } |
|
209 | 1 | if (! array_key_exists($groupKey, $results)) { |
|
210 | 1 | $results[$groupKey] = new static; |
|
211 | 1 | } |
|
212 | 1 | $results[$groupKey]->offsetSet($preserveKeys ? $key : null, $value); |
|
213 | 1 | } |
|
214 | 1 | } |
|
215 | 1 | return new static($results); |
|
216 | } |
||
217 | |||
218 | /** |
||
219 | * Concatenates collections into a single collection |
||
220 | * |
||
221 | * @return static |
||
222 | */ |
||
223 | 4 | public function concat() |
|
224 | { |
||
225 | 4 | return $this->reduce( |
|
226 | 4 | function (CollectionInterface $accumulator, Collection $item) { |
|
227 | 4 | if ($item instanceof Collection) { |
|
228 | 4 | $accumulator = $accumulator->merge($item); |
|
229 | 4 | } |
|
230 | 4 | return $accumulator; |
|
231 | 4 | }, |
|
232 | 4 | new static([]) |
|
233 | 4 | ); |
|
234 | } |
||
235 | |||
236 | /** |
||
237 | * Map a function over a collection and flatten the result by one-level |
||
238 | * |
||
239 | * @param callable $callable |
||
240 | * @return static |
||
241 | */ |
||
242 | 3 | public function flatMap(callable $callable) |
|
243 | { |
||
244 | 3 | return $this->map($callable)->concat(); |
|
245 | } |
||
246 | |||
247 | /** |
||
248 | * Alias for flatMap() |
||
249 | * |
||
250 | * @see flatMap |
||
251 | */ |
||
252 | 2 | public function mapcat(callable $callable) |
|
253 | { |
||
254 | 2 | return $this->flatMap($callable); |
|
255 | } |
||
256 | |||
257 | /** |
||
258 | * Get all items but the first |
||
259 | * |
||
260 | * @return static |
||
261 | */ |
||
262 | 1 | public function tail() |
|
263 | { |
||
264 | 1 | return new static(array_slice($this->values(), 1)); |
|
265 | } |
||
266 | |||
267 | |||
268 | /** |
||
269 | * Get the first item |
||
270 | * |
||
271 | * @return mixed |
||
272 | */ |
||
273 | 1 | public function head() |
|
274 | { |
||
275 | 1 | $values = $this->values(); |
|
276 | 1 | return array_shift($values); |
|
277 | } |
||
278 | |||
279 | /** |
||
280 | * @param callable $callable |
||
281 | * @param mixed $initial |
||
282 | * @return mixed |
||
283 | */ |
||
284 | 14 | View Code Duplication | public function reduce(callable $callable, $initial) |
0 ignored issues
–
show
|
|||
285 | { |
||
286 | 14 | $accumulator = $initial; |
|
287 | 14 | $function = $this->normalizeAsCallables($callable); |
|
288 | |||
289 | 14 | foreach ($this->getArrayCopy() as $item) { |
|
290 | 14 | $accumulator = $function($accumulator, $item); |
|
291 | 14 | } |
|
292 | 14 | return $accumulator; |
|
293 | } |
||
294 | |||
295 | /** |
||
296 | * Returns true if all items satisfy the callable condition |
||
297 | * |
||
298 | * @param callable $callable |
||
299 | */ |
||
300 | 2 | View Code Duplication | public function every(callable $callable) |
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
301 | { |
||
302 | 2 | $callable = $this->normalizeAsCallables($callable); |
|
303 | 2 | foreach ($this->values() as $item) { |
|
304 | 2 | if (!$callable($item)) { |
|
305 | 1 | return false; |
|
306 | } |
||
307 | 2 | } |
|
308 | 1 | return true; |
|
309 | } |
||
310 | |||
311 | /** |
||
312 | * Returns true if at least one item satisfies the callable condition |
||
313 | * |
||
314 | * @param callable $callable |
||
315 | */ |
||
316 | 5 | View Code Duplication | public function some(callable $callable) |
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
317 | { |
||
318 | 5 | $callable = $this->normalizeAsCallables($callable); |
|
319 | 5 | foreach ($this->values() as $item) { |
|
320 | 5 | if ($callable($item)) { |
|
321 | 3 | return true; |
|
322 | } |
||
323 | 3 | } |
|
324 | 2 | return false; |
|
325 | } |
||
326 | |||
327 | /** |
||
328 | * Returns true no item satisfies the callable condition |
||
329 | * |
||
330 | * @param callable $callable |
||
331 | */ |
||
332 | 2 | public function none(callable $callable) |
|
333 | { |
||
334 | 2 | return !$this->some($callable); |
|
335 | } |
||
336 | } |
||
337 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.