CollectionUtils::collectProperty()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Dontdrinkandroot\Utils;
4
5
use Traversable;
6
7
/**
8
 * @author Philip Washington Sorst <[email protected]>
9
 */
10
class CollectionUtils
11
{
12
    /**
13
     * @param array|Traversable $collection
14
     * @param callable          $collectFunction
15
     *
16
     * @return array
17
     */
18
    public static function collect($collection, callable $collectFunction): array
19
    {
20
        $results = [];
21
        foreach ($collection as $element) {
22
            $results[] = call_user_func($collectFunction, $element);
23
        }
24
25
        return $results;
26
    }
27
28
    /**
29
     * @param array|Traversable $collection
30
     * @param string          $propertyName
31
     *
32
     * @return array
33
     */
34
    public static function collectProperty($collection, string $propertyName): array
35
    {
36
        $results = [];
37
        foreach ($collection as $element) {
38
            $results[] = $element->{$propertyName};
39
        }
40
41
        return $results;
42
    }
43
}
44