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 — annotations (#407)
by Vincent
15:03
created

ArgumentsTransformer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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\ResolveInfo;
11
use GraphQL\Type\Definition\Type;
12
use Overblog\GraphQLBundle\Error\InvalidArgumentError;
13
use Overblog\GraphQLBundle\Error\InvalidArgumentsError;
14
use Symfony\Component\PropertyAccess\PropertyAccess;
15
use Symfony\Component\PropertyAccess\PropertyAccessor;
16
use Symfony\Component\Validator\Validator\ValidatorInterface;
17
18
class ArgumentsTransformer
19
{
20
    /**
21
     * @var ValidatorInterface
22
     */
23
    protected $validator;
24
25
    /**
26
     * @var array
27
     */
28
    protected $classesMap;
29
30
    /**
31
     * @var PropertyAccessor
32
     */
33
    protected $accessor;
34
35 2
    public function __construct(ValidatorInterface $validator = null, $classesMap = [])
36
    {
37 2
        $this->validator = $validator;
38 2
        $this->accessor = PropertyAccess::createPropertyAccessor();
39 2
        $this->classesMap = $classesMap;
40 2
    }
41
42
    /**
43
     * Get the PHP class for a given type.
44
     *
45
     * @param string $type
46
     *
47
     * @return object|false
48
     */
49 2
    private function getTypeClassInstance(string $type)
50
    {
51 2
        $classname = isset($this->classesMap[$type]) ? $this->classesMap[$type]['class'] : false;
52
53 2
        return $classname ? new $classname() : false;
54
    }
55
56
    /**
57
     * Extract given type from Resolve Info.
58
     *
59
     * @param string      $type
60
     * @param ResolveInfo $info
61
     *
62
     * @return Type
63
     */
64 2
    private function getType(string $type, ResolveInfo $info): Type
65
    {
66 2
        return $info->schema->getType($type);
67
    }
68
69
    /**
70
     * Populate an object based on type with given data.
71
     *
72
     * @param Type        $type
73
     * @param mixed       $data
74
     * @param bool        $multiple
75
     * @param ResolveInfo $info
76
     *
77
     * @return mixed
78
     */
79 2
    private function populateObject(Type $type, $data, $multiple = false, ResolveInfo $info)
80
    {
81 2
        if (!$data) {
82 2
            return $data;
83
        }
84
85 2
        if ($multiple) {
86
            return \array_map(function ($data) use ($type, $info) {
87 2
                return $this->populateObject($type, $data, false, $info);
88 2
            }, $data);
89
        }
90
91 2
        if ($type instanceof EnumType) {
0 ignored issues
show
Bug introduced by
The class GraphQL\Type\Definition\EnumType does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
92 2
            $instance = $this->getTypeClassInstance($type->name);
93 2
            if ($instance) {
94 1
                $this->accessor->setValue($instance, 'value', $data);
95
96 1
                return $instance;
97
            } else {
98 2
                return $data;
99
            }
100 2
        } elseif ($type instanceof InputObjectType) {
0 ignored issues
show
Bug introduced by
The class GraphQL\Type\Definition\InputObjectType does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
101 2
            $instance = $this->getTypeClassInstance($type->name);
102 2
            if (!$instance) {
103
                return $data;
104
            }
105
106 2
            $fields = $type->getFields();
107
108 2
            foreach ($fields as $name => $field) {
109 2
                $fieldData = $this->accessor->getValue($data, \sprintf('[%s]', $name));
110
111 2
                if ($field->getType() instanceof ListOfType) {
0 ignored issues
show
Bug introduced by
The class GraphQL\Type\Definition\ListOfType does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
112 2
                    $fieldValue = $this->populateObject($field->getType()->getWrappedType(), $fieldData, true, $info);
113
                } else {
114 2
                    $fieldValue = $this->populateObject($field->getType(), $fieldData, false, $info);
115
                }
116
117 2
                $this->accessor->setValue($instance, $name, $fieldValue);
118
            }
119
120 2
            return $instance;
121
        } else {
122 2
            return $data;
123
        }
124
    }
125
126
    /**
127
     * Given a GraphQL type and an array of data, populate corresponding object recursively
128
     * using annoted classes.
129
     *
130
     * @param string      $argType
131
     * @param mixed       $data
132
     * @param ResolveInfo $info
133
     *
134
     * @return mixed
135
     */
136 2
    public function getInstanceAndValidate(string $argType, $data, ResolveInfo $info, string $argName)
137
    {
138 2
        $isRequired = '!' === $argType[\strlen($argType) - 1];
139 2
        $isMultiple = '[' === $argType[0];
140 2
        $endIndex = ($isRequired ? 1 : 0) + ($isMultiple ? 1 : 0);
141 2
        $type = \substr($argType, $isMultiple ? 1 : 0, $endIndex > 0 ? -$endIndex : \strlen($argType));
142
143 2
        $result = $this->populateObject($this->getType($type, $info), $data, $isMultiple, $info);
144 2
        $errors = [];
145 2
        if (\is_object($result) && $this->validator) {
146 2
            $errors = $this->validator->validate($result);
147
        }
148
149 2
        if (\count($errors) > 0) {
150 1
            throw new InvalidArgumentError($argName, $errors);
151
        } else {
152 1
            return $result;
153
        }
154
    }
155
156
    /**
157
     * Transform a list of arguments into their corresponding php class and validate them.
158
     *
159
     * @param array       $mapping
160
     * @param mixed       $data
161
     * @param ResolveInfo $info
162
     *
163
     * @return array
164
     */
165 2
    public function getArguments(array $mapping, $data, ResolveInfo $info)
166
    {
167 2
        $args = [];
168 2
        $exceptions = [];
169
170 2
        foreach ($mapping as $name => $type) {
171
            try {
172 2
                $value = $this->getInstanceAndValidate($type, $data[$name], $info, $name);
173 1
                $args[] = $value;
174 1
            } catch (InvalidArgumentError $exception) {
175 2
                $exceptions[] = $exception;
176
            }
177
        }
178
179 2
        if (!empty($exceptions)) {
180 1
            throw new InvalidArgumentsError($exceptions);
181
        }
182
183 1
        return $args;
184
    }
185
}
186