RecursiveRenamerHelper   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 62
Duplicated Lines 20.97 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 11
c 2
b 0
f 1
lcom 0
cbo 1
dl 13
loc 62
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A renameKeys() 0 16 4
A renameKeyValue() 13 13 3
A renameMatchedClassKeys() 0 9 4

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: 8:59 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
/**
16
 * Class RecursiveRenamerHelper.
17
 */
18
class RecursiveRenamerHelper
19
{
20
    /**
21
     * @param \NilPortugues\Api\Mapping\Mapping[] $mappings
22
     * @param array                               $array
23
     * @param string                              $typeKey
24
     * @param array                               $replacements
25
     * @param array                               $newArray
26
     */
27
    private static function renameKeys(
28
        array &$mappings,
29
        array &$array,
30
        $typeKey,
31
        array &$replacements,
32
        array &$newArray
33
    ) {
34
        foreach ($array as $key => &$value) {
35
            $key = (!empty($replacements[$key])) ? $replacements[$key] : $key;
36
            $newArray[$key] = $value;
37
38
            if (\is_array($newArray[$key])) {
39
                self::renameKeyValue($mappings, $newArray[$key], $typeKey);
40
            }
41
        }
42
    }
43
44
    /**
45
     * @param \NilPortugues\Api\Mapping\Mapping[] $mappings
46
     * @param array                               $array
47
     * @param string                              $typeKey
48
     */
49 View Code Duplication
    public static function renameKeyValue(array &$mappings, array &$array, $typeKey)
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...
50
    {
51
        if (\array_key_exists(Serializer::CLASS_IDENTIFIER_KEY, $array)) {
52
            $newArray = [];
53
            $type = $array[Serializer::CLASS_IDENTIFIER_KEY];
54
55
            self::renameMatchedClassKeys($mappings, $array, $typeKey, $type, $newArray);
56
57
            if (!empty($newArray)) {
58
                $array = $newArray;
59
            }
60
        }
61
    }
62
63
    /**
64
     * @param \NilPortugues\Api\Mapping\Mapping[] $mappings
65
     * @param array                               $array
66
     * @param string                              $typeKey
67
     * @param string                              $type
68
     * @param array                               $newArray
69
     */
70
    private static function renameMatchedClassKeys(array &$mappings, array &$array, $typeKey, $type, array &$newArray)
71
    {
72
        if (\is_scalar($type) && $type === $typeKey) {
73
            $replacements = $mappings[$typeKey]->getAliasedProperties();
74
            if (!empty($replacements)) {
75
                self::renameKeys($mappings, $array, $typeKey, $replacements, $newArray);
76
            }
77
        }
78
    }
79
}
80