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
379:29 queued 376:31
created

InputBuilder::__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
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 10
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\InputType;
9
use GraphQL\Type\Definition\ListOfType;
10
use GraphQL\Type\Definition\ResolveInfo;
11
use GraphQL\Type\Definition\Type;
12
use Symfony\Component\PropertyAccess\PropertyAccess;
13
use Symfony\Component\PropertyAccess\PropertyAccessor;
14
use Symfony\Component\Validator\Validator\ValidatorInterface;
15
16
class InputBuilder
17
{
18
    /**
19
     * @var ValidatorInterface
20
     */
21
    protected $validator;
22
23
    /**
24
     * @var array
25
     */
26
    protected $classesMap;
27
28
    /**
29
     * @var PropertyAccessor
30
     */
31
    protected $accessor;
32
33 1
    public function __construct(ValidatorInterface $validator, $classesMap = [])
34
    {
35 1
        $this->validator = $validator;
36 1
        $this->accessor = PropertyAccess::createPropertyAccessor();
37 1
        $this->classesMap = $classesMap;
38 1
    }
39
40
    /**
41
     * Get the PHP class for a given type.
42
     *
43
     * @param string $type
44
     *
45
     * @return object|false
46
     */
47 1
    private function getTypeClassInstance(string $type)
48
    {
49 1
        $classname = isset($this->classesMap[$type]) ? $this->classesMap[$type]['class'] : false;
50
51 1
        return $classname ? new $classname() : false;
52
    }
53
54
    /**
55
     * Extract given type from Resolve Info.
56
     *
57
     * @param string      $type
58
     * @param ResolveInfo $info
59
     *
60
     * @return Type
61
     */
62 1
    private function getType(string $type, ResolveInfo $info): Type
63
    {
64 1
        return $info->schema->getType($type);
65
    }
66
67
    /**
68
     * Populate an object based on type with given data.
69
     *
70
     * @param Type        $type
71
     * @param mixed       $data
72
     * @param bool        $multiple
73
     * @param ResolveInfo $info
74
     *
75
     * @return mixed
76
     */
77 1
    private function populateObject(Type $type, $data, $multiple = false, ResolveInfo $info)
78
    {
79 1
        if ($multiple) {
80
            return \array_map(function ($data) use ($type, $info) {
81 1
                return $this->populateObject($type, $data, false, $info);
82 1
            }, $data);
83
        }
84
85 1
        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...
86 1
            $instance = $this->getTypeClassInstance($type->name);
87 1
            if ($instance) {
88 1
                $this->accessor->setValue($instance, 'value', $data);
89
90 1
                return $instance;
91
            } else {
92 1
                return $data;
93
            }
94 1
        } elseif ($type instanceof InputType) {
0 ignored issues
show
Bug introduced by
The class GraphQL\Type\Definition\InputType 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...
95 1
            $instance = $this->getTypeClassInstance($type->name);
96 1
            if (!$instance) {
97 1
                return $data;
98
            }
99
100 1
            $fields = $type->getFields();
101
102 1
            foreach ($fields as $name => $field) {
103 1
                $fieldData = $this->accessor->getValue($data, \sprintf('[%s]', $name));
104
105 1
                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...
106 1
                    $fieldValue = $this->populateObject($field->getType()->getWrappedType(), $fieldData, true, $info);
107
                } else {
108 1
                    $fieldValue = $this->populateObject($field->getType(), $fieldData, false, $info);
109
                }
110
111 1
                $this->accessor->setValue($instance, $name, $fieldValue);
112
            }
113
114 1
            return $instance;
115
        } else {
116
            return $data;
117
        }
118
    }
119
120
    /**
121
     * Given a GraphQL type and an array of data, populate corresponding object recursively
122
     * using annoted classes.
123
     *
124
     * @param string      $argType
125
     * @param mixed       $data
126
     * @param ResolveInfo $info
127
     *
128
     * @return mixed
129
     */
130 1
    public function getInstanceAndValidate(string $argType, $data, ResolveInfo $info)
131
    {
132 1
        $isRequired = '!' === $argType[\strlen($argType) - 1];
133 1
        $isMultiple = '[' === $argType[0];
134 1
        $endIndex = ($isRequired ? 1 : 0) + ($isMultiple ? 1 : 0);
135 1
        $type = \substr($argType, $isMultiple ? 1 : 0, $endIndex > 0 ? -$endIndex : \strlen($argType));
136
137 1
        $result = $this->populateObject($this->getType($type, $info), $data, $isMultiple, $info);
138
139 1
        $errors = $this->validator->validate($result);
140 1
        if (\count($errors) > 0) {
141
            throw new \Exception((string) $errors);
142
        } else {
143 1
            return $result;
144
        }
145
    }
146
}
147