Completed
Push — master ( 74371a...6da8c0 )
by Vladimir
02:21
created

SelectFilter::flatten()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Templating\Twig\Extension;
9
10
class SelectFilter extends AbstractTwigExtension implements TwigFilterInterface
11
{
12 8
    public function __invoke($array, $key, $flatten = true, $distinct = true, $ignore_null = true)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $ignore_null is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
13
    {
14 8
        $results = array();
15
16 8
        foreach ($array as $item)
17
        {
18 8
            if (!is_array($item) && !($item instanceof \ArrayAccess))
19
            {
20
                continue;
21
            }
22
23 8
            if ($ignore_null)
24
            {
25 5
                if (isset($item[$key]))
26
                {
27 5
                    $results[] = $item[$key];
28
                }
29
            }
30
            else
31
            {
32 3
                if (array_key_exists($key, $item) || ($item instanceof \ArrayAccess && $item->offsetExists($key)))
33
                {
34 8
                    $results[] = $item[$key];
35
                }
36
            }
37
        }
38
39 8
        if ($flatten)
40
        {
41 7
            $results = self::flatten($results);
42
43 7
            if ($distinct)
44
            {
45 5
                $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...
46
47 5
                foreach ($results as $key => $result)
48
                {
49 5
                    $distinct[$result] = true;
50
                }
51
52 5
                $results = array_keys($distinct);
53
            }
54
        }
55
56 8
        return $results;
57
    }
58
59
    public static function get()
60
    {
61
        return new \Twig_SimpleFilter('select', new self());
62
    }
63
64 7
    private static function flatten(array $array)
65
    {
66 7
        $return = array();
67
        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...
68 7
        return $return;
69
    }
70
}
71