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
08:39
created

InheritanceProcessor::processConfigsExtends()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

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