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 (#656)
by Vincent
14:47
created

ArgumentsTransformer::populateObject()   B

Complexity

Conditions 11
Paths 21

Size

Total Lines 53
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 11.0324

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 33
c 1
b 1
f 0
dl 0
loc 53
ccs 29
cts 31
cp 0.9355
rs 7.3166
cc 11
nc 21
nop 4
crap 11.0324

How to fix   Long Method    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
declare(strict_types=1);
4
5
namespace Overblog\GraphQLBundle\Transformer;
6
7
use GraphQL\Type\Definition\EnumType;
8
use GraphQL\Type\Definition\InputObjectType;
9
use GraphQL\Type\Definition\ListOfType;
10
use GraphQL\Type\Definition\NonNull;
11
use GraphQL\Type\Definition\ResolveInfo;
12
use GraphQL\Type\Definition\Type;
13
use Overblog\GraphQLBundle\Error\InvalidArgumentError;
14
use Overblog\GraphQLBundle\Error\InvalidArgumentsError;
15
use Symfony\Component\PropertyAccess\PropertyAccess;
16
use Symfony\Component\PropertyAccess\PropertyAccessor;
17
use Symfony\Component\Validator\ConstraintViolationList;
18
use Symfony\Component\Validator\Validator\ValidatorInterface;
19
use function array_map;
20
use function count;
21
use function is_array;
22
use function is_object;
23
use function sprintf;
24
use function strlen;
25
use function substr;
26
27
class ArgumentsTransformer
28
{
29
    protected PropertyAccessor $accessor;
30
    protected ?ValidatorInterface $validator;
31
    protected array $classesMap;
32
33 4
    public function __construct(ValidatorInterface $validator = null, array $classesMap = [])
34
    {
35 4
        $this->validator = $validator;
36 4
        $this->accessor = PropertyAccess::createPropertyAccessor();
37 4
        $this->classesMap = $classesMap;
38 4
    }
39
40
    /**
41
     * Get the PHP class for a given type.
42
     *
43
     * @return object|false
44
     */
45 4
    private function getTypeClassInstance(string $type)
46
    {
47 4
        $classname = isset($this->classesMap[$type]) ? $this->classesMap[$type]['class'] : false;
48
49 4
        return $classname ? (new \ReflectionClass($classname))->newInstanceWithoutConstructor() : false;
50
    }
51
52
    /**
53
     * Extract given type from Resolve Info.
54
     */
55 4
    private function getType(string $type, ResolveInfo $info): ?Type
56
    {
57 4
        return $info->schema->getType($type);
58
    }
59
60
    /**
61
     * Populate an object based on type with given data.
62
     *
63
     * @param mixed $data
64
     *
65
     * @return mixed
66
     */
67 4
    private function populateObject(Type $type, $data, bool $multiple, ResolveInfo $info)
68
    {
69 4
        if (null === $data) {
70 4
            return null;
71
        }
72
73 4
        if ($type instanceof NonNull) {
74
            $type = $type->getWrappedType();
75
        }
76
77 4
        if ($multiple) {
78 4
            return array_map(function ($data) use ($type, $info) {
79 4
                return $this->populateObject($type, $data, false, $info);
80 4
            }, $data);
81
        }
82
83 4
        if ($type instanceof EnumType) {
84 4
            $instance = $this->getTypeClassInstance($type->name);
85 4
            if ($instance) {
86 2
                $this->accessor->setValue($instance, 'value', $data);
87
88 2
                return $instance;
89
            } else {
90 3
                return $data;
91
            }
92 4
        } elseif ($type instanceof InputObjectType) {
93 4
            $instance = $this->getTypeClassInstance($type->name);
94 4
            if (!$instance) {
95
                return $data;
96
            }
97
98 4
            $fields = $type->getFields();
99
100 4
            foreach ($fields as $name => $field) {
101 4
                $fieldData = $this->accessor->getValue($data, sprintf('[%s]', $name));
102 4
                $fieldType = $field->getType();
103
104 4
                if ($fieldType instanceof NonNull) {
105 4
                    $fieldType = $fieldType->getWrappedType();
106
                }
107
108 4
                if ($fieldType instanceof ListOfType) {
109 4
                    $fieldValue = $this->populateObject($fieldType->getWrappedType(), $fieldData, true, $info);
110
                } else {
111 4
                    $fieldValue = $this->populateObject($fieldType, $fieldData, false, $info);
112
                }
113
114 4
                $this->accessor->setValue($instance, $name, $fieldValue);
115
            }
116
117 4
            return $instance;
118
        } else {
119 4
            return $data;
120
        }
121
    }
122
123
    /**
124
     * Given a GraphQL type and an array of data, populate corresponding object recursively
125
     * using annoted classes.
126
     *
127
     * @param mixed $data
128
     *
129
     * @return mixed
130
     */
131 4
    public function getInstanceAndValidate(string $argType, $data, ResolveInfo $info, string $argName)
132
    {
133 4
        $isRequired = '!' === $argType[strlen($argType) - 1];
134 4
        $isMultiple = '[' === $argType[0];
135 4
        $endIndex = ($isRequired ? 1 : 0) + ($isMultiple ? 1 : 0);
136 4
        $type = substr($argType, $isMultiple ? 1 : 0, $endIndex > 0 ? -$endIndex : strlen($argType));
137
138 4
        $result = $this->populateObject($this->getType($type, $info), $data, $isMultiple, $info);
0 ignored issues
show
Bug introduced by
It seems like $this->getType($type, $info) can also be of type null; however, parameter $type of Overblog\GraphQLBundle\T...ormer::populateObject() does only seem to accept GraphQL\Type\Definition\Type, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

138
        $result = $this->populateObject(/** @scrutinizer ignore-type */ $this->getType($type, $info), $data, $isMultiple, $info);
Loading history...
139
140 4
        if (null !== $this->validator) {
141 4
            $errors = new ConstraintViolationList();
142 4
            if (is_object($result)) {
143 3
                $errors = $this->validator->validate($result);
144
            }
145 4
            if (is_array($result) && $isMultiple) {
146 1
                foreach ($result as $element) {
147 1
                    if (is_object($element)) {
148 1
                        $errors->addAll(
149 1
                            $this->validator->validate($element)
150
                        );
151
                    }
152
                }
153
            }
154
155 4
            if (count($errors) > 0) {
156 2
                throw new InvalidArgumentError($argName, $errors);
157
            }
158
        }
159
160 2
        return $result;
161
    }
162
163
    /**
164
     * Transform a list of arguments into their corresponding php class and validate them.
165
     *
166
     * @param mixed $data
167
     *
168
     * @return array
169
     */
170 4
    public function getArguments(array $mapping, $data, ResolveInfo $info)
171
    {
172 4
        $args = [];
173 4
        $exceptions = [];
174
175 4
        foreach ($mapping as $name => $type) {
176
            try {
177 4
                $value = $this->getInstanceAndValidate($type, $data[$name], $info, $name);
178 2
                $args[] = $value;
179 2
            } catch (InvalidArgumentError $exception) {
180 2
                $exceptions[] = $exception;
181
            }
182
        }
183
184 4
        if (!empty($exceptions)) {
185 2
            throw new InvalidArgumentsError($exceptions);
186
        }
187
188 2
        return $args;
189
    }
190
}
191