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 (#333)
by Jérémiah
08:28 queued 05:48
created

BuilderProcessor::processFieldArgumentsBuilders()   B

Complexity

Conditions 9
Paths 30

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 15.3773

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 8
cts 14
cp 0.5714
rs 8.0555
c 0
b 0
f 0
cc 9
nc 30
nop 1
crap 15.3773
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 7
    public static function process(array $configs)
44
    {
45 7
        foreach ($configs as &$config) {
46 7
            if (isset($config['config']['fields']) && is_array($config['config']['fields'])) {
47 7
                $config['config']['fields'] = self::processFieldBuilders($config['config']['fields']);
48
            }
49
        }
50
51 5
        return $configs;
52
    }
53
54 6
    public static function addBuilderClass($name, $type, $builderClass)
55
    {
56 6
        self::checkBuilderClass($builderClass, $type);
57
        self::$builderClassMap[$type][$name] = $builderClass;
58
    }
59
60
    /**
61
     * @param string $builderClass
62
     * @param string $type
63
     */
64 6
    private static function checkBuilderClass($builderClass, $type)
65
    {
66 6
        $interface = MappingInterface::class;
67
68 6
        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 3
        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 2
        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
    }
91
92 2
    private static function processFieldBuilders(array $fields)
93
    {
94 2
        foreach ($fields as &$field) {
95 2
            $fieldBuilderName = null;
96
97 2
            if (isset($field['builder']) && is_string($field['builder'])) {
98 1
                $fieldBuilderName = $field['builder'];
99 1
                unset($field['builder']);
100 1
            } elseif (is_string($field)) {
101
                @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
                    'It will be replaced by the field type short syntax (Field: Type => Field: {type: Type})',
104
                    E_USER_DEPRECATED
105
                );
106
                $fieldBuilderName = $field;
107
            }
108
109 2
            $builderConfig = [];
110 2
            if (isset($field['builderConfig'])) {
111
                if (is_array($field['builderConfig'])) {
112
                    $builderConfig = $field['builderConfig'];
113
                }
114
                unset($field['builderConfig']);
115
            }
116
117 2
            if ($fieldBuilderName) {
118 1
                $buildField = self::getBuilder($fieldBuilderName, self::BUILDER_FIELD_TYPE)->toMappingDefinition($builderConfig);
119
                $field = is_array($field) ? array_merge($buildField, $field) : $buildField;
120
            }
121 1
            if (isset($field['argsBuilder'])) {
122 1
                $field = self::processFieldArgumentsBuilders($field);
123
            }
124
        }
125
126
        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 2
    private static function getBuilder($name, $type)
138
    {
139 2
        static $builders = [];
140 2
        if (isset($builders[$type][$name])) {
141
            return $builders[$type][$name];
142
        }
143
144 2
        $builderClassMap = self::$builderClassMap[$type];
145
146 2
        if (isset($builderClassMap[$name])) {
147
            return $builders[$type][$name] = new $builderClassMap[$name]();
148
        }
149
        // deprecated relay builder name ?
150 2
        $newName = 'Relay::'.rtrim($name, 'Args');
151 2
        if (isset($builderClassMap[$newName])) {
152
            @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
                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
                E_USER_DEPRECATED
155
            );
156
157
            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 1
    private static function processFieldArgumentsBuilders(array $field)
164
    {
165 1
        $argsBuilderName = null;
166
167 1
        if (is_string($field['argsBuilder'])) {
168 1
            $argsBuilderName = $field['argsBuilder'];
169
        } elseif (isset($field['argsBuilder']['builder']) && is_string($field['argsBuilder']['builder'])) {
170
            $argsBuilderName = $field['argsBuilder']['builder'];
171
        }
172
173 1
        $builderConfig = [];
174 1
        if (isset($field['argsBuilder']['config']) && is_array($field['argsBuilder']['config'])) {
175
            $builderConfig = $field['argsBuilder']['config'];
176
        }
177
178 1
        if ($argsBuilderName) {
179 1
            $args = self::getBuilder($argsBuilderName, self::BUILDER_ARGS_TYPE)->toMappingDefinition($builderConfig);
180
            $field['args'] = isset($field['args']) && is_array($field['args']) ? array_merge($args, $field['args']) : $args;
181
        }
182
183
        unset($field['argsBuilder']);
184
185
        return $field;
186
    }
187
}
188