Passed
Push — master ( 3bb5db...959b11 )
by Julien
16:17
created

Transform::_flattenKeys()   F

Complexity

Conditions 18
Paths 579

Size

Total Lines 59
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 19.0524

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 59
ccs 23
cts 27
cp 0.8519
rs 1.2847
cc 18
nc 579
nop 4
crap 19.0524

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) || is_object($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
                $ret = array_merge_recursive($ret, $subRet);
0 ignored issues
show
Bug introduced by
It seems like $subRet can also be of type null; however, parameter $_ of array_merge_recursive() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
                $ret = array_merge_recursive($ret, /** @scrutinizer ignore-type */ $subRet);
Loading history...
85
                
86 1
                if (!isset($ret[$currentKey])) {
87 1
                    $ret[$currentKey] = false;
88
                }
89
            }
90
            else {
91 1
                $ret[$currentKey] = $value;
92
            }
93
        }
94
        
95 1
        return $ret;
96
    }
97
}
98