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

Completed
Pull Request — master (#237)
by Jérémiah
08:38
created

BuilderProcessor::processFieldBuilders()   D

Complexity

Conditions 10
Paths 55

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 22
cts 22
cp 1
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 23
nc 55
nop 1
crap 10

How to fix   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
namespace Overblog\GraphQLBundle\Config\Processor;
4
5
use Overblog\GraphQLBundle\Definition\Builder\MappingInterface;
6
use Overblog\GraphQLBundle\Relay\Connection\BackwardConnectionArgsDefinition;
7
use Overblog\GraphQLBundle\Relay\Connection\ConnectionArgsDefinition;
8
use Overblog\GraphQLBundle\Relay\Connection\ForwardConnectionArgsDefinition;
9
use Overblog\GraphQLBundle\Relay\Mutation\MutationFieldDefinition;
10
use Overblog\GraphQLBundle\Relay\Node\GlobalIdFieldDefinition;
11
use Overblog\GraphQLBundle\Relay\Node\NodeFieldDefinition;
12
use Overblog\GraphQLBundle\Relay\Node\PluralIdentifyingRootFieldDefinition;
13
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
14
15
final class BuilderProcessor implements ProcessorInterface
16
{
17
    const BUILDER_FIELD_TYPE = 'field';
18
    const BUILDER_ARGS_TYPE = 'args';
19
20
    const BUILDER_TYPES = [
21
        self::BUILDER_FIELD_TYPE,
22
        self::BUILDER_ARGS_TYPE,
23
    ];
24
25
    /** @var MappingInterface[] */
26
    private static $builderClassMap = [
27
        self::BUILDER_ARGS_TYPE => [
28
            'Relay::ForwardConnection' => ForwardConnectionArgsDefinition::class,
29
            'Relay::BackwardConnection' => BackwardConnectionArgsDefinition::class,
30
            'Relay::Connection' => ConnectionArgsDefinition::class,
31
        ],
32
        self::BUILDER_FIELD_TYPE => [
33
            'Relay::Mutation' => MutationFieldDefinition::class,
34
            'Relay::GlobalId' => GlobalIdFieldDefinition::class,
35
            'Relay::Node' => NodeFieldDefinition::class,
36
            'Relay::PluralIdentifyingRoot' => PluralIdentifyingRootFieldDefinition::class,
37
        ],
38
    ];
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 31
    public static function process(array $configs)
44
    {
45 31
        foreach ($configs as &$config) {
46 31
            if (isset($config['config']['fields']) && is_array($config['config']['fields'])) {
47 31
                $config['config']['fields'] = self::processFieldBuilders($config['config']['fields']);
48
            }
49
        }
50
51 29
        return $configs;
52
    }
53
54 7
    public static function addBuilderClass($name, $type, $builderClass)
55
    {
56 7
        self::checkBuilderClass($builderClass, $type);
57 1
        self::$builderClassMap[$type][$name] = $builderClass;
58 1
    }
59
60
    /**
61
     * @param string $builderClass
62
     * @param string $type
63
     */
64 7
    private static function checkBuilderClass($builderClass, $type)
65
    {
66 7
        $interface = MappingInterface::class;
67
68 7
        if (!is_string($builderClass)) {
69 3
            throw new \InvalidArgumentException(
70 3
                sprintf('%s builder class should be string, but %s given.', ucfirst($type), gettype($builderClass))
71
            );
72
        }
73
74 4
        if (!class_exists($builderClass)) {
75 1
            throw new \InvalidArgumentException(
76 1
                sprintf('%s builder class "%s" not found.', ucfirst($type), $builderClass)
77
            );
78
        }
79
80 3
        if (!is_subclass_of($builderClass, $interface)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $interface can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
81 2
            throw new \InvalidArgumentException(
82 2
                sprintf(
83 2
                    '%s builder class should implement "%s", but "%s" given.',
84 2
                    ucfirst($type),
85 2
                    $interface,
86 2
                    $builderClass
87
                )
88
            );
89
        }
90 1
    }
91
92 26
    private static function processFieldBuilders(array $fields)
93
    {
94 26
        foreach ($fields as &$field) {
95 26
            $fieldBuilderName = null;
96
97 26
            if (isset($field['builder']) && is_string($field['builder'])) {
98 8
                $fieldBuilderName = $field['builder'];
99 8
                unset($field['builder']);
100 25
            } elseif (is_string($field)) {
101 2
                @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
102
                    'The builder short syntax (Field: Builder => Field: {builder: Builder}) is deprecated as of 0.7 and will be removed in 0.12. '.
103 2
                    'It will be replaced by the field type short syntax (Field: Type => Field: {type: Type})',
104 2
                    E_USER_DEPRECATED
105
                );
106 2
                $fieldBuilderName = $field;
107
            }
108
109 26
            $builderConfig = [];
110 26
            if (isset($field['builderConfig'])) {
111 6
                if (is_array($field['builderConfig'])) {
112 6
                    $builderConfig = $field['builderConfig'];
113
                }
114 6
                unset($field['builderConfig']);
115
            }
116
117 26
            if ($fieldBuilderName) {
118 8
                $buildField = self::getBuilder($fieldBuilderName, self::BUILDER_FIELD_TYPE)->toMappingDefinition($builderConfig);
119 7
                $field = is_array($field) ? array_merge($buildField, $field) : $buildField;
120
            }
121 25
            if (isset($field['argsBuilder'])) {
122 25
                $field = self::processFieldArgumentsBuilders($field);
123
            }
124
        }
125
126 24
        return $fields;
127
    }
128
129
    /**
130
     * @param string $name
131
     * @param string $type
132
     *
133
     * @return MappingInterface
134
     *
135
     * @throws InvalidConfigurationException if builder class not define
136
     */
137 18
    private static function getBuilder($name, $type)
138
    {
139 18
        static $builders = [];
140 18
        if (isset($builders[$type][$name])) {
141 13
            return $builders[$type][$name];
142
        }
143
144 8
        $builderClassMap = self::$builderClassMap[$type];
145
146 8
        if (isset($builderClassMap[$name])) {
147 5
            return $builders[$type][$name] = new $builderClassMap[$name]();
148
        }
149
        // deprecated relay builder name ?
150 3
        $newName = 'Relay::'.rtrim($name, 'Args');
151 3
        if (isset($builderClassMap[$newName])) {
152 1
            @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
153 1
                sprintf('The "%s" %s builder is deprecated as of 0.7 and will be removed in 0.12. Use "%s" instead.', $name, $type, $newName),
154 1
                E_USER_DEPRECATED
155
            );
156
157 1
            return $builders[$type][$newName] = new $builderClassMap[$newName]();
158
        }
159
160 2
        throw new InvalidConfigurationException(sprintf('%s builder "%s" not found.', ucfirst($type), $name));
161
    }
162
163 12
    private static function processFieldArgumentsBuilders(array $field)
164
    {
165 12
        $argsBuilderName = null;
166
167 12
        if (is_string($field['argsBuilder'])) {
168 12
            $argsBuilderName = $field['argsBuilder'];
169 1
        } elseif (isset($field['argsBuilder']['builder']) && is_string($field['argsBuilder']['builder'])) {
170 1
            $argsBuilderName = $field['argsBuilder']['builder'];
171
        }
172
173 12
        $builderConfig = [];
174 12
        if (isset($field['argsBuilder']['config']) && is_array($field['argsBuilder']['config'])) {
175 1
            $builderConfig = $field['argsBuilder']['config'];
176
        }
177
178 12
        if ($argsBuilderName) {
179 12
            $args = self::getBuilder($argsBuilderName, self::BUILDER_ARGS_TYPE)->toMappingDefinition($builderConfig);
180 11
            $field['args'] = isset($field['args']) && is_array($field['args']) ? array_merge($args, $field['args']) : $args;
181
        }
182
183 11
        unset($field['argsBuilder']);
184
185 11
        return $field;
186
    }
187
}
188