Passed
Push — master ( e5b7b4...ec2be8 )
by Julien
14:08
created

Transform::flattenKeys()   F

Complexity

Conditions 18
Paths 963

Size

Total Lines 61
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 18.9454

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 18
eloc 29
c 1
b 1
f 0
nc 963
nop 4
dl 0
loc 61
ccs 24
cts 28
cp 0.8571
crap 18.9454
rs 0.7513

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of the Zemit Framework.
4
 *
5
 * (c) Zemit Team <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE.txt
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Zemit\Utils;
12
13
/**
14
 * Class Slug
15
 *
16
 * @author Julien Turbide <[email protected]>
17
 * @copyright Zemit Team <[email protected]>
18
 *
19
 * @since 1.0
20
 * @version 1.0
21
 *
22
 * @package Zemit\Utils
23
 */
24
class Transform
25
{
26
    /**
27
     * Here to parse the columns parameter into some kind of flatten array with
28
     * the key path separated by dot "my.path" and the value true, false or a callback function
29
     * including the ExposeBuilder object
30
     *
31
     * @param mixed $array
32
     * @param string|null $context
33
     * @param string|null $delimiter
34
     *
35
     * @return array|null
36
     */
37 1
    final public static function flattenKeys($array = null, ?string $delimiter = '.', ?bool $lowerKey = true, ?string $context = null) : ?array
38
    {
39
        // nothing passed
40 1
        if (!isset($array)) {
41
            return null;
42
        }
43
        
44 1
        $ret = [];
45
        
46 1
        if (!is_array($array)) {
47 1
            $array = [$array];
48
        }
49
        
50 1
        foreach ($array as $key => $value) {
51
            
52
            // handle our special attribute
53 1
            if (is_bool($key)) {
54
                $value = $key;
55
                $key = null;
56
            }
57
            
58
            // flip value to key
59 1
            if (is_int($key)) {
60 1
                if (is_string($value)) {
61 1
                    $key = $value;
62 1
                    $value = true;
63
                }
64
                else {
65 1
                    $key = null;
66
                }
67
            }
68
            
69
            // force lower case key
70 1
            if (is_string($key)) {
71 1
                $key = trim($lowerKey ? mb_strtolower($key) : $key);
72
            }
73
            
74
            //
75 1
            if (is_string($value) && empty($value)) {
76
                $value = true;
77
            }
78
            
79
            // set the key
80 1
            $currentKey = (!empty($context) ? $context . (!empty($key) ? $delimiter : null) : null) . $key;
81
            
82 1
            if (is_array($value) || is_object($value)) {
83 1
                $subRet = self::flattenKeys(is_callable($value)? $value() : $value, $delimiter, $lowerKey, $currentKey);
84 1
                if (is_array($subRet)) {
85 1
                    $ret = array_merge_recursive($ret, $subRet);
86
                }
87
                
88 1
                if (!isset($ret[$currentKey])) {
89 1
                    $ret[$currentKey] = false;
90
                }
91
            }
92
            else {
93 1
                $ret[$currentKey] = $value;
94
            }
95
        }
96
        
97 1
        return $ret;
98
    }
99
}
100