Completed
Push — master ( ffd4d6...afdcfd )
by Vladimir
02:48
created

SelectFilter::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * @copyright 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Twig;
9
10
class SelectFilter
11
{
12 3
    public function __invoke($array, $key, $flatten = true, $distinct = true)
13
    {
14 3
        $results = array();
15
16 3
        foreach ($array as $item)
17
        {
18 3
            if (!is_array($item) && !($item instanceof \ArrayAccess))
19
            {
20
                continue;
21
            }
22
23 3
            if (isset($item[$key]))
24
            {
25 3
                $results[] = $item[$key];
26
            }
27
        }
28
29 3
        if ($flatten)
30
        {
31 2
            $results = self::flatten($results);
32
33 2
            if ($distinct)
34
            {
35 1
                $distinct = array();
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $distinct. This often makes code more readable.
Loading history...
36
37 1
                foreach ($results as $key => $result)
38
                {
39 1
                    $distinct[$result] = true;
40
                }
41
42 1
                $results = array_keys($distinct);
43
            }
44
        }
45
46 3
        return $results;
47
    }
48
49 2
    public static function get()
50
    {
51 2
        return new \Twig_SimpleFilter('select', new self());
52
    }
53
54 2
    private static function flatten(array $array)
55
    {
56 2
        $return = array();
57
        array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $a. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
58 2
        return $return;
59
    }
60
}