RecursiveFormatterHelper   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 150
Duplicated Lines 12 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 22
c 2
b 0
f 1
lcom 1
cbo 2
dl 18
loc 150
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A namespaceAsArrayKey() 0 7 1
A getIdPropertyAndValues() 0 13 2
A getIdProperties() 0 10 3
A getIdValue() 0 9 2
A formatScalarValues() 8 8 3
A flattenObjectsWithSingleKeyScalars() 10 10 4
A arrayToScalarValue() 0 8 2
A loopScalarValues() 0 12 4
A camelCaseToUnderscore() 0 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * Author: Nil Portugués Calderó <[email protected]>
5
 * Date: 7/24/15
6
 * Time: 9:05 PM.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace NilPortugues\Api\Transformer\Helpers;
12
13
use NilPortugues\Serializer\Serializer;
14
15
final class RecursiveFormatterHelper
16
{
17
    /**
18
     * Given a class name will return its name without the namespace and in under_score to be used as a key in an array.
19
     *
20
     * @param string $key
21
     *
22
     * @return string
23
     */
24
    public static function namespaceAsArrayKey($key)
25
    {
26
        $keys = \explode('\\', $key);
27
        $className = \end($keys);
28
29
        return self::camelCaseToUnderscore($className);
30
    }
31
32
    /**
33
     * @param \NilPortugues\Api\Mapping\Mapping[] $mappings
34
     * @param array                               $value
35
     * @param string                              $type
36
     *
37
     * @return array
38
     */
39
    public static function getIdPropertyAndValues(array &$mappings, array &$value, $type)
40
    {
41
        $values = [];
42
        $idProperties = self::getIdProperties($mappings, $type);
43
44
        foreach ($idProperties as &$propertyName) {
45
            $values[] = self::getIdValue($value[$propertyName]);
46
            $propertyName = \sprintf('{%s}', $propertyName);
47
        }
48
        self::flattenObjectsWithSingleKeyScalars($values);
49
50
        return [$values, $idProperties];
51
    }
52
53
    /**
54
     * @param \NilPortugues\Api\Mapping\Mapping[] $mappings
55
     * @param string                              $type
56
     *
57
     * @return array
58
     */
59
    public static function getIdProperties(array &$mappings, $type)
60
    {
61
        $idProperties = [];
62
63
        if (\is_scalar($type) && !empty($mappings[$type])) {
64
            $idProperties = $mappings[$type]->getIdProperties();
65
        }
66
67
        return $idProperties;
68
    }
69
70
    /**
71
     * @param array $id
72
     *
73
     * @return array
74
     */
75
    public static function getIdValue(array $id)
76
    {
77
        self::formatScalarValues($id);
78
        if (\is_array($id)) {
79
            RecursiveDeleteHelper::deleteKeys($id, [Serializer::CLASS_IDENTIFIER_KEY]);
80
        }
81
82
        return $id;
83
    }
84
85
    /**
86
     * Replaces the Serializer array structure representing scalar values to the actual scalar value using recursion.
87
     *
88
     * @param array $array
89
     */
90 View Code Duplication
    public static function formatScalarValues(array &$array)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        $array = self::arrayToScalarValue($array);
93
94
        if (\is_array($array) && !array_key_exists(Serializer::SCALAR_VALUE, $array)) {
95
            self::loopScalarValues($array, 'formatScalarValues');
96
        }
97
    }
98
99
    /**
100
     * Simplifies the data structure by removing an array level if data is scalar and has one element in array.
101
     *
102
     * @param array $array
103
     */
104 View Code Duplication
    public static function flattenObjectsWithSingleKeyScalars(array &$array)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        if (1 === \count($array) && \is_scalar(\end($array))) {
107
            $array = \array_pop($array);
108
        }
109
110
        if (\is_array($array)) {
111
            self::loopScalarValues($array, 'flattenObjectsWithSingleKeyScalars');
112
        }
113
    }
114
115
    /**
116
     * @param array $array
117
     *
118
     * @return array
119
     */
120
    private static function arrayToScalarValue(array &$array)
121
    {
122
        if (\array_key_exists(Serializer::SCALAR_VALUE, $array)) {
123
            $array = $array[Serializer::SCALAR_VALUE];
124
        }
125
126
        return $array;
127
    }
128
129
    /**
130
     * @param array  $array
131
     * @param string $method
132
     */
133
    private static function loopScalarValues(array &$array, $method)
134
    {
135
        foreach ($array as $key => &$value) {
136
            if (\is_array($value)) {
137
                if (!empty($value)) {
138
                    self::$method($value);
139
                } else {
140
                    unset($array[$key]);
141
                }
142
            }
143
        }
144
    }
145
146
    /**
147
     * Transforms a given string from camelCase to under_score style.
148
     *
149
     * @param string $camel
150
     * @param string $splitter
151
     *
152
     * @return string
153
     */
154
    public static function camelCaseToUnderscore($camel, $splitter = '_')
155
    {
156
        $camel = \preg_replace(
157
            '/(?!^)[[:upper:]][[:lower:]]/',
158
            '$0',
159
            \preg_replace('/(?!^)[[:upper:]]+/', $splitter.'$0', $camel)
160
        );
161
162
        return \strtolower($camel);
163
    }
164
}
165