Completed
Push — master ( 89bfcf...4ab803 )
by Vladimir
02:41
created

ZipFilter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 88%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 57
rs 10
ccs 22
cts 25
cp 0.88

3 Methods

Rating   Name   Duplication   Size   Complexity  
C __invoke() 0 44 7
A get() 0 4 1
A safe_get() 0 4 2
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 ZipFilter implements StakxTwigFilter
11
{
12 10
    public function __invoke(array $array1, array $array2, $glue = '', $strict = false)
13
    {
14 10
        $result = array();
15 10
        $arr1_length = count($array1);
16 10
        $arr2_length = count($array2);
17
18 10
        for ($i = 0; $i < $arr1_length; $i++)
19
        {
20 10
            if ($i >= $arr2_length)
21
            {
22 2
                break;
23
            }
24
25 10
            $rhs = self::safe_get($array1, $i);
26 10
            $lhs = self::safe_get($array2, $i);
27
28 10
            if (empty($rhs))
29
            {
30
                $result[] = $lhs;
31
                continue;
32
            }
33
            elseif (empty($lhs))
34
            {
35 2
                $result[] = $rhs;
36 2
                continue;
37
            }
38
39 10
            $result[] = self::safe_get($array1, $i) . $glue . self::safe_get($array2, $i);
40
        }
41
42 10
        if (!$strict)
43
        {
44 6
            if ($arr2_length > $arr1_length)
45
            {
46 2
                $result = array_merge($result, array_slice($array2, $arr1_length));
47
            }
48
            else
49
            {
50 4
                $result = array_merge($result, array_slice($array1, $arr2_length));
51
            }
52
        }
53
54 10
        return $result;
55
    }
56
57 2
    public static function get()
58
    {
59 2
        return new \Twig_SimpleFilter('zip', new self());
60
    }
61
62 10
    private static function safe_get(array &$array, $key, $default = '')
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not 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 seeker becomes databaseConnectionSeeker.

Loading history...
63
    {
64 10
        return (isset($array[$key]) ? (string)$array[$key] : $default);
65
    }
66
}
67