Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Pull Request — master (#237)
by Jérémiah
07:21
created

InheritanceProcessor   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 148
Duplicated Lines 4.05 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 93.9%

Importance

Changes 0
Metric Value
wmc 29
lcom 1
cbo 0
dl 6
loc 148
ccs 77
cts 82
cp 0.939
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 8 1
A removedDecorators() 0 6 2
C processConfigsHeirs() 3 28 7
B processConfigsInherits() 0 20 5
A inheritsTypeConfig() 0 15 2
B flattenInherits() 3 23 4
A checkTypeExists() 0 10 2
A checkCircularReferenceInheritsTypes() 0 9 2
A checkAllowedInheritsTypes() 0 12 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
namespace Overblog\GraphQLBundle\Config\Processor;
4
5
final class InheritanceProcessor implements ProcessorInterface
6
{
7
    const HEIRS_KEY = 'heirs';
8
    const INHERITS_KEY = 'inherits';
9
10
    /**
11
     * {@inheritdoc}
12
     */
13 32
    public static function process(array $configs)
14
    {
15 32
        $configs = self::processConfigsHeirs($configs);
16 32
        $configs = self::processConfigsInherits($configs);
17 29
        $configs = self::removedDecorators($configs);
18
19 29
        return $configs;
20
    }
21
22
    private static function removedDecorators(array $configs)
23
    {
24 29
        return array_filter($configs, function ($config) {
25 29
            return !isset($config['decorator']) || true !== $config['decorator'];
26 29
        });
27
    }
28
29 32
    private static function processConfigsHeirs(array $configs)
30
    {
31 32
        foreach ($configs as $parentName => &$config) {
32 32
            if (!empty($config[self::HEIRS_KEY])) {
33 1 View Code Duplication
                if (!is_array($config[self::HEIRS_KEY])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
34
                    $config[self::HEIRS_KEY] = [$config[self::HEIRS_KEY]];
35
                }
36 1
                foreach ($config[self::HEIRS_KEY] as $heirs) {
37 1
                    if (!isset($configs[$heirs])) {
38
                        throw new \InvalidArgumentException(sprintf(
39
                            'Type %s child of %s not found.',
40
                            json_encode($heirs),
41
                            json_encode($parentName)
42
                        ));
43
                    }
44
45 1
                    if (!isset($configs[$heirs][self::INHERITS_KEY])) {
46 1
                        $configs[$heirs][self::INHERITS_KEY] = [];
47
                    }
48
49 1
                    $configs[$heirs][self::INHERITS_KEY][] = $parentName;
50
                }
51
            }
52 32
            unset($config[self::HEIRS_KEY]);
53
        }
54
55 32
        return $configs;
56
    }
57
58 32
    private static function processConfigsInherits(array $configs)
59
    {
60 32
        foreach ($configs as $name => &$config) {
61 32
            if (!isset($config['type'])) {
62 6
                continue;
63
            }
64
65 27
            $allowedTypes = [$config['type']];
66 27
            if ('object' === $config['type']) {
67 27
                $allowedTypes[] = 'interface';
68
            }
69 27
            $flattenInherits = self::flattenInherits($name, $configs, $allowedTypes);
70 24
            if (empty($flattenInherits)) {
71 24
                continue;
72
            }
73 1
            $config = self::inheritsTypeConfig($name, $flattenInherits, $configs);
74
        }
75
76 29
        return $configs;
77
    }
78
79 1
    private static function inheritsTypeConfig($child, array $parents, array $configs)
80
    {
81 1
        $parentTypes = array_intersect_key($configs, array_flip($parents));
82 1
        $parentTypes = array_reverse($parentTypes);
83 1
        $mergedParentsConfig = call_user_func_array('array_replace_recursive', array_column($parentTypes, 'config'));
84 1
        $childType = $configs[$child];
85
        // unset resolveType field resulting from the merge of a "interface" type
86 1
        if ('object' === $childType['type']) {
87 1
            unset($mergedParentsConfig['resolveType']);
88
        }
89
90 1
        $configs = array_replace_recursive(['config' => $mergedParentsConfig], $childType);
91
92 1
        return $configs;
93
    }
94
95 27
    private static function flattenInherits($name, array $configs, array $allowedTypes, $child = null, array $typesTreated = [])
96
    {
97 27
        self::checkTypeExists($name, $configs, $child);
98 27
        self::checkCircularReferenceInheritsTypes($name, $typesTreated);
99 27
        self::checkAllowedInheritsTypes($name, $configs[$name], $allowedTypes, $child);
100
101
        // flatten
102 27
        $config = $configs[$name];
103 27 View Code Duplication
        if (empty($config[self::INHERITS_KEY]) || !is_array($config[self::INHERITS_KEY])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
104 24
            return [];
105
        }
106 4
        $typesTreated[$name] = true;
107 4
        $flattenInheritsTypes = [];
108 4
        foreach ($config[self::INHERITS_KEY] as $typeToInherit) {
109 4
            $flattenInheritsTypes[] = $typeToInherit;
110 4
            $flattenInheritsTypes = array_merge(
111 4
                $flattenInheritsTypes,
112 4
                self::flattenInherits($typeToInherit, $configs, $allowedTypes, $name, $typesTreated)
113
            );
114
        }
115
116 1
        return $flattenInheritsTypes;
117
    }
118
119 27
    private static function checkTypeExists($name, array $configs, $child)
120
    {
121 27
        if (!isset($configs[$name])) {
122 1
            throw new \InvalidArgumentException(sprintf(
123 1
                'Type %s inherits by %s not found.',
124 1
                json_encode($name),
125 1
                json_encode($child)
126
            ));
127
        }
128 27
    }
129
130 27
    private static function checkCircularReferenceInheritsTypes($name, array $typesTreated)
131
    {
132 27
        if (isset($typesTreated[$name])) {
133 1
            throw new \InvalidArgumentException(sprintf(
134 1
                'Type circular inheritance detected (%s).',
135 1
                implode('->', array_merge(array_keys($typesTreated), [$name]))
136
            ));
137
        }
138 27
    }
139
140 27
    private static function checkAllowedInheritsTypes($name, array $config, array $allowedTypes, $child)
141
    {
142 27
        if (empty($config['decorator']) && isset($config['type']) && !in_array($config['type'], $allowedTypes)) {
143 1
            throw new \InvalidArgumentException(sprintf(
144 1
                'Type %s can\'t inherits %s because %s is not allowed type (%s).',
145 1
                json_encode($name),
146 1
                json_encode($child),
147 1
                json_encode($config['type']),
148 1
                json_encode($allowedTypes)
149
            ));
150
        }
151 27
    }
152
}
153